Home  >  Article  >  Database  >  Super detailed analysis of the Redis configuration file redis.conf

Super detailed analysis of the Redis configuration file redis.conf

coldplay.xixi
coldplay.xixiforward
2021-02-22 09:46:254985browse

Super detailed analysis of the Redis configuration file redis.conf

Recommended (free): redis tutorial

Article Directory

  • 1. Thirty commonly used configurations
  • 2. Redis memory elimination strategy
    • 2.1 Set timeout for data
    • 2.2 Use LRU algorithm to dynamically delete unused data
  • 3. Custom configuration of Redis

Keep in mind when developing under Linux: The software is installed under /opt by default. Never directly change the configuration file with factory default settings. The correct way is toback up a copy before operating.

The Redis configuration file is located in the Redis installation directory. The file name is reids.conf. Here are thirty commonly used configurations. The article comes with the English translation of the redis.conf file. .

1. Thirty commonly used configurations


The first ten configurations

  • daemonize no
    Redis does not run as a daemon process by default. It can be modified to yes to enable the daemon process.

  • pidfile /var/run/redis/pid
    When Redis runs as a daemon, Redis will save the pid by default Write /var/run/redis.pid file, you can specify the path through pidfile.

  • port 6379
    Specify the listening port of Redis.

  • bind 127.0.0.1
    The host address bound by Redis.

  • timeout 300
    Set how long the client will be idle before closing the connection. If it is 0, it means turning off this function.

  • loglevel verbose
    Specify the logging level. Redis supports four levels: debug, verbose (default), notice, warning.

  • logfile stdout
    Logging mode, the default is standard output, if Redis is configured as a daemon process, and the logging here If the mode is standard output, the log will be sent to /dev/null

  • ##databases 16 Settings The number of databases. The default number is 0. You can use the
    select command to specify the database id on the connection.

  • ##save

    Specify how many update operations there are within a certain period of time, and then the Data is synchronized to data files and can be matched with multiple conditions. Three conditions are provided in the Redis configuration file: save 900 1; save 300 10; save 60 10000

  • rdbcompression

    yes Specify whether to compress the data when storing it in the local database. The default is yes. Redis uses LZF (compression algorithm) compression. If you want to save CPU time, you can turn off this option, but it will cause the database file to become huge.


The middle ten configurations

    ##dbfilename
  • dump.rdb

    Specify the local database file name, the default value is dump.rdb

  • dir
  • ./

    Specify the local database Storage directory

  • slaveof
  • Society When this machine serves slav, set the master service The IP address and port will automatically synchronize data from the master when Redis starts.

  • masterauth
  • When the master service is password protected, the password for the slav service to connect to the master.

  • requirepass
  • foobared

    Set the connection password for Redis. If a connection password is configured, the client needs to pass# when connecting to Redis. ##AUTH<password>The command provides a password, which is turned off by default.

    maxclients
  • 128
  • Set the maximum number of client connections at the same time. The default is unlimited. Redis can open client connections at the same time. The number is the maximum number of file descriptors that the Redis process can open. If maxclients is set to 0, it means there is no limit. When the number of client connections reaches the limit, Redis will close the new connection and return the max number of clients reached error message to the client.
    ##maxmemory

  • Specifies the maximum memory limit of Redis. Redis will load data into memory when it starts. After reaching the maximum memory, Redis will first try to clear expired or expiring Keys. After this method is processed, the maximum memory setting is still reached and write operations will not be possible, but read operations can still be performed. Redis' new vm mechanism stores the key in memory and the value in the swap area.
    appendonly

    no
  • Specifies whether to log after an update operation. Redis is one-stop by default. Writing data to disk, if not turned on, may result in data loss for a period of time during a power outage.

  • appendfilename appendonly.aof
    Specify the update log file name, the default is appendonly.aof.

  • appendsync everysec
    Specify the update log conditions. There are three options:
    ①no: Indicates waiting for the operating system to complete The data cache is synchronized to the disk (fast),
    ②always: means manually calling fsync() to write the data to the disk after each update of the operating system (slow, safe),
    ③everysec: means wonderful synchronization once (efficiency compromise) , is the default value)

last ten

  • ##vm-enable no Specify whether to enable the virtual memory mechanism. The default value is no. The VM mechanism stores data in pages. Redis swaps the pages with less access, that is, cold data, to the disk. The pages with more access are automatically swapped out by the disk. in memory.
  • vm-swap-file /tmp/redis.swap Virtual memory file path, the default value is /tmp/redis.swap, not more than one Redis instance sharing.
  • vm-max-memory 0 Store all data larger than vm-max-memory in virtual memory, regardless of vm-max-memory settings Small, all index data is stored in memory (Redis index data is keys), that is to say, when vm-max-memory is set to 0, all values ​​actually exist on the disk. The default value is 0
  • vm-page-size 32 The Redis swap file is divided into many pages, and an object can be saved on multiple pages. However, a page cannot be shared by multiple objects. vm-page-size is set according to the size of the stored data. If many small objects are stored, the page size is best set to 32 or 64 bytes; if very large objects are stored, You can use a larger page. If you are not sure, use the default value.
  • vm-pages 134217728 Set the number of pages in the swap file, because the page table (a bitmap indicating that the page is free or used) is placed In memory, every 8 pages on disk will consume 1 byte of memory.
  • vm-max-threads 4 Set the number of threads to access the swap file. Do not exceed the number of cores of the machine. If set to 0, then all pairs The operations of the swap file are all serial and may cause a long delay. The default value is 4.
  • glueoutputbuf yes Set whether to combine smaller packets into one packet and send it when responding to the client. The default is on.
  • hash-max-zipmap-entries 64/hash-max-zipmap-value 512 Specifies that a special hash algorithm is used when a certain number is exceeded or the largest element exceeds a certain threshold.
  • activerehashing yes Specifies whether to activate rehashing, the default is on.
  • include /path/to/local.conf Specifies the inclusion of other configuration files. You can use the same configuration file between multiple Redis instances on the same host. configuration files, and each instance has its own specific configuration file.

2. Redis’ memory elimination strategy

As an excellent cache middleware, Redis often stores a large amount of data, even if cluster deployment is used to dynamically When expanding capacity, memory should also be cleared immediately to maintain system performance.

2.1 Set the timeout for data

  • expire key time (in seconds) This is the most commonly used method
  • setex(String Key, int seconds, String value) String-unique method
In addition to the string's own unique method of setting the expiration time, other Methods all need to rely on the expire method to set the time.

If no time is set, the cache will never expire.
If you set an expiration time and later want the cache to never expire, use
persist key

2.2 Use the LRU algorithm to dynamically delete unused data

A page replacement algorithm for memory management. Data blocks (memory blocks) that are in the memory but are not used are called LRU. The operating system will remove them from the memory to make room for loading based on which data belongs to the LRU. additional data.

  1. volatile-lru    Among the data with set timeout period, delete the least commonly used data

  2. allkeys-lru    Query the least commonly used data among all keys and delete them. This is the most widely used strategy.

  3. volatile-random Randomly delete data that has been set with a timeout

  4. allkeys-random Query all keys, and then randomly delete

  5. volatile-ttl Query all data with a set timeout, and then sort them to collect the data of state-owned enterprises. Delete

  6. noeviction If set to this attribute, the deletion operation will not be performed, and an error will be returned if the memory overflows

  7. volatile-lfu    Remove the least frequently used key from all keys configured with a timeout

  8. allkeys-lfu Delete the least frequently used key from all keys

3. Custom configuration Redis

Enter the corresponding installation directory /usr/local/redis and modify the redis.conf configuration file.

As a beginner, Redis generally needs to modify the following three items:

  • Change daemonize no to daemonize yes, that is, it will be started as a daemon process instead.
  • Comment out bind 127.0.01 to allow machines other than the local machine to access the Redis service.
  • Use requirepass Set password, which ensures service security/in rare cases remote access is not possible without setting a password.

Redis adopts a single-process multi-thread mode. When the daemonize option in redis.conf is set to yes, it means that the daemon process mode is enabled. In this mode, redis will run in the background and write the process pid number to the file set by the redis.conf option pidfile. At this time, redis will always run unless the process is manually killed. But when the daemonize option is set to no, the current interface will enter the redis command line interface. Forced exit by exit or closing the connection tool (putty, xshell, etc.) will cause the redis process to exit. Most applications developed on the server side run in the background.

More related learning:redis

The above is the detailed content of Super detailed analysis of the Redis configuration file redis.conf. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete