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

Super detailed analysis of the Redis configuration file redis.conf

Feb 22, 2021 am 09:46 AM
redis

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

See all articles