Redis is a brief introduction to

  1. Redis is a high performance (NOSQL) key-value database,Redis is an open source using ANSI C language, network support, memory based and persistent logging, key-value database, and provides a variety of language API. As of March 15, 2010, the development of Redis is hosted by VMware. Development of Redis has been sponsored by Pivotal since May 2013. (Vmware is funding the development and maintenance of the Redis project)

  2. Redis and other key-value caching products have the following three characteristics:

    • Redis supports data persistence, saving data in memory to disk, which can be reloaded for use upon restart.
    • Redis not only supports simple key-value type data, but also provides the storage of list, set, zset, hash and other data structures.
    • Redis supports data backup, clustering and other high availability functions.
  3. Features:

    • High performance – Redis can read 110,000 times /s and write 81,000 times /s.
    • Rich data types – Redis supports String, List, Hash, Set, and Ordered Set data type operations.
    • Atomic – All operations in Redis are atomic, meaning that they either succeed or fail at all. Individual operations are atomic. Multiple operations also support transactions, namely atomicity, wrapped in MULTI and EXEC instructions.
    • Rich features – Redis also supports publish/subscribe, notifications, key expiration, and more.
  4. Redis summary

    • Redis The size of a single key is 512 MB
    • Redis support multiple types of data structures (string, list, hash. Set. Zset)
    • Redis is single-threaded atomicity
    • Redis can persist because it uses RDB and AOF mechanisms
    • Redis supports clustering and Redis supports libraries (0-15) up to 16 libraries
    • Redis can also do message queues such as chat room IM

In enterprise development: Can be used as a database, cache (hot data (data that is often queried, but not often modified or deleted), messaging middleware and most other functions.

Redis installation

Official: redis. IO/Chinese website: redis.cn/

Redis is C language development, install Redis will need to download the official website to compile the source code, compile depends on the GCC environment, if there is no GCC environment, you need to install GCC



Install GCC

Installing GCC is a simple matter of making sure root is logged in and Linux can connect to the Internet

yum -y install gcc automake autoconf libtool make
Copy the code

Error: /var/run/yum.pid was locked when running yum. Pid is XXXX and another program is running

rm -f /var/run/yum.pid
Copy the code
  1. To install Redis, you can upload the downloaded installation package to Linux using Filezilla and decompress it. Alternatively, you can run a command to download the downloaded installation package online and decompress it to a specified directory. Create a Redis installation directory, usually /usr/local/redis

    Command 1: wgetDownload. Redis. IO/releases/re…

    Command 2: tar ZXVF redis-4.0.1.tar.gz

    Command 3: CD redis

    Command 4: make or make MALLOC=libc



    Command 5: make PREFIX=/usr/local/redis install

    (Install compiled files) Install to indicated directory:

    Note: PREFIX must be capitalized and a redis directory will be automatically created for us and the result installed

    A bin directory is generated when you specify a directory for installation. However, no bin directory is generated when you decompress the make directory directly. Both redis-cli and redis-server are in the bin directory. Directly install redis-cli and redis-server in the SRC directory

Go to the decompressed redis directory, switch to SRC, and copy redis-server and redis-cli to /usr/local/redis

cp redis-server redis-cli /usr/local/redis/
Copy the code

Copy redis-conf from the decompressed redis directory to //usr/local/redis

cp redis.conf /usr/local/redis/
Copy the code

Redis start

Start Redis on the server. It is not recommended to use redis. After closing the interface, redis will stop

Start the Redis client command: Redis -cli -h IP address -p Port Exit the client command: Ctrl+C

Linux configuration Redis

1. Go to /usr/local/redis to open the redis-conf configuration file

vim redis.conf
Copy the code

Find daemonize no and change it to yes 2. Comment it out

The bind 127.0.01Copy the code

3. You can also set the redis database password

Requirepass Sets the passwordCopy the code

Daemonize yes is used to enable the daemon process, so that the server will not be shut down after it is shut down. The comment bind 127.0.0.1 is for future access from any IP address



Redis uses the mode of single process and multiple threads. If daemonize in redis.conf is set to yes, the daemon mode is enabled. In this mode, Redis runs in the background and writes the process PID number to the file set by the redis. Conf option pidfile. Redis will run until the process is killed manually. If daemonize is set to No, the redis command line interface is displayed. Exit Forcibly exits the redis process or closes the connection tool (such as putty and Xshell). Most applications developed by the server are run in the background mode

Conf configuration file in detail

Conf configuration items are described as follows:

  1. Redis does not run as a daemon by default. You can modify this configuration item by using yes to enable daemonize no
  2. Pid is written to the /var/run/redis.pid file by default when Redis is running as a daemon. You can specify pidfile /var/run/redis.pid with pidfile
  3. The default port is 6379. Why choose 6379 as the default port, because 6379 is the number corresponding to MERZ on the phone button, and MERZ is derived from the Italian singer Alessia MERZ’s name port 6379
  4. Host ADDRESS bind 127.0.0.1 5. Close the connection after the client is idle. If the value is 0, timeout 300 is disabled
  5. Redis supports four log levels: debug, verbose, notice, and warning. The default value is verbose loglevel verbose
  6. The log mode is standard output by default. If Redis is configured to run in daemon mode and the log mode is configured as standard output, the log will be sent to /dev/null logfile stdout
  7. Set the number of databases. The default database is 0. You can use the SELECT command to specify the database ID on the connection
  8. The default Redis configuration file provides three conditions: Save 900 1 Save 300 10 Save 60 10000 indicates 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes), and 10000 changes in 60 seconds respectively.
  9. Redis uses LZF compression. If you want to save CPU time, you can turn this option off, but it will cause database files to become large Rdbcompression yes
  10. Specifies the name of the local database file. The default value is dump. RDB dbfilename dump. RDB
  11. Specify the local database directory dir./
  12. If the Slav service is used, set the IP address and port number of the master service. When Redis starts, it will automatically synchronize data from the master to slaveof
  13. If password protection is enabled for the master service, the Slav service connects to the Master using the masterauth password
  14. Set the Redis connection password. If the password is configured, the client needs to provide the password by using the AUTH command when connecting to Redis. Requirepass foobared is disabled by default
  15. The maximum number of client connections that Redis can open at a time is the maximum number of file descriptors that the Redis process can open at the same time. If maxClients 0 is set, there is no limit. When the number of clients reached the limit, Redis will close new connections and return Max Number of clients reached error message maxClients 128
  16. Specifies the maximum memory limit for Redis. When Redis starts up, it loads data into memory. When the maximum memory is reached, Redis tries to clear expired or expiring keys first. The new VM mechanism of Redis will store the Key in memory and the Value in swap maxMemory
  17. Specifies whether to log after each update operation. Redis writes data to disk asynchronously by default. If not enabled, data may be lost for a period of time in the event of a power outage. Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. The default is no appendonly No
  18. Appendone.aof appendfilename appendone.aof specifies the update log filename
  19. Three values are available: no: data is cached by the operating system and synchronized to disk (fast) always: data is written to disk (slow and safe) manually by calling fsync() after each update operation everysec: Represents synchronization once per second (compromise, default) appendfSync everysec
  20. Specifies whether to enable the virtual memory mechanism. The default value is no. For a brief introduction, the VM mechanism stores data in paging mode, and Redis swaps cold data on less visited pages to disk. Frequently visited pages are automatically swapped out from disk to memory (I will analyze Redis VM mechanism in detail in a later article) VM-enabled No
  21. The default value is/TMP /redis.swap. Multiple Redis instances cannot share vm-swap-file/TMP /redis.swap
  22. All data larger than VM-max-memory is stored in virtual memory. No matter how small the VM-max-memory setting is, all index data is stored in memory (Redis index data) Keys), which means that when vM-max-memory is set to 0, all values actually exist on disk. The default value is 0 vM-max-memory 0
  23. The Redis swap file is divided into many pages. An object can be stored on multiple pages, but a page cannot be shared by multiple objects. Vm-page-size is set according to the size of the stored data. The page size is best set to 32 or 64bytes; If you store large objects, you can use a larger page, or if in doubt, use the default vM-page-size 32
  24. Set the number of pages in the swap file. Since the page table (a bitmap indicating that a page is free or used) is placed in memory, every 8 pages on disk consumes 1byte of memory. vm-pages 134217728
  25. Set the number of threads that can access the swap file. It is best not to exceed the number of cores on the machine. If set to 0, all operations on the swap file are serial and may cause a long delay. The default value is 4 VM-max-threads 4
  26. Glueoutputbuf Yes Specifies whether to combine smaller packets into one packet when replying to clients. By default, glueOutputBuf Yes is enabled
  27. Hash-max-zipmap-entries 64 hash-max-zipmap-value 512 Specifies that a special hash algorithm is used when the number of elements exceeds a certain threshold or the maximum number exceeds a certain threshold
  28. Activerehashing Yes specifies whether rehash hash is enabled. The default is enable
  29. Include additional configuration files. You can use the same configuration file among multiple instances of Redis on the same host, and each instance has its own specific configuration file include /path/to/local.conf

After the configuration, run the following command to enable the configuration to take effect

./redis-server redis.conf
Copy the code



Then start Redis using the server

./redis-cli
Copy the code

Check whether the startup is successful. Ping PONG is returned