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 to
back 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
selectcommand 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
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
maxclientsSet the connection password for Redis. If a connection password is configured, the client needs to pass# when connecting to Redis. ##AUTH<password></password>
The command provides a password, which is turned off by default. 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.
no
appendonly 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
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.volatile-lru Among the data with set timeout period, delete the least commonly used data
allkeys-lru Query the least commonly used data among all keys and delete them. This is the most widely used strategy.
volatile-random Randomly delete data that has been set with a timeout
allkeys-random Query all keys, and then randomly delete
volatile-ttl Query all data with a set timeout, and then sort them to collect the data of state-owned enterprises. Delete
noeviction If set to this attribute, the deletion operation will not be performed, and an error will be returned if the memory overflows
volatile-lfu Remove the least frequently used key from all keys configured with a timeout
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 daemonizeyes
, 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!

Compared with other databases, Redis has the following unique advantages: 1) extremely fast speed, and read and write operations are usually at the microsecond level; 2) supports rich data structures and operations; 3) flexible usage scenarios such as caches, counters and publish subscriptions. When choosing Redis or other databases, it depends on the specific needs and scenarios. Redis performs well in high-performance and low-latency applications.

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version