What are the pros and cons of Redis?
advantages
- The read and write performance of Redis is excellent. The read speed of Redis is 110000 times /s, and the write speed is 81000 times /s.
- Supports data persistence, AOF and RDB.
- Support for transactions, all operations in Redis are atomic, and Redis also supports atomicity when several operations are combined.
- In addition to the string value, the system supports hash, set, zset, and list data structures.
- Supports master/slave replication. The master automatically synchronizes data to the slave, and reads and writes can be separated.
disadvantages
- Limited by the physical memory, the database capacity cannot be used for high-performance read and write of massive data. Therefore, Redis is mainly applicable to high-performance operations and calculations with small amounts of data.
- Redis does not have automatic fault tolerance and recovery functions. If a slave host breaks down, some front-end read and write requests fail. The recovery can be performed only after the server restarts or the front-end IP address is manually switched.
- Before the host breaks down, some data cannot be synchronized to the slave server in time. After the IP address is changed, data inconsistency occurs, which reduces system availability.
- Redis does not support online capacity expansion. When the cluster capacity reaches the upper limit, online expansion becomes complicated.
Why is Redis so fast?
- Completely memory based, most requests are purely memory operations, very fast.
- The data structure is simple and the reading speed is fast. Such as SDS, double – ended list, compressed list, skip list.
- Network requests are processed using a single thread to avoid unnecessary context switches and race conditions.
Since Redis 4.0, asynchronous threads have been used to handle some time-consuming operations. For example, asynchronous threads implement lazy deletes (to solve the problem of large KEY deletes blocking the main thread), asynchronous AOF (to solve the problem of slow fsync execution when disk IO is tight), and so on.
- Use multiplex I/O multiplexing model, non-blocking IO.
- Different from using the underlying model, the underlying implementation and the application protocol used to communicate with the client, Redis directly built the VM mechanism itself, because the general system calls system functions, will waste a certain amount of time to move and request.
How many databases are there in Redis?
The Redis server creates 16 databases by default, which can be changed by configuring the Database option. By default, the target database of the Redis client is database 0.
Redis data structure?
- Basic data types: string, list, set, zset, and hash
- Advanced data structures: Bitmap, HyperLogLog, GEO
Redis Module: BloomFilter, RedisSearch, Redis-ML, JSON. Stream functionality (added in 5.0)
Redis usage scenarios
- Data cache
- Session cache
- Timeliness data
- Access to the frequency
- counter
- Social list
- Record user decision information
- Intersection, union, and difference sets
- Hot lists and leaderboards
- The latest development of
- The message queue
- A distributed lock
Redis persistence
Redis provides two ways to persist data to disk. When Redis is enabled, the AOF file is loaded first. When AOF is disabled, the RDB file is loaded.
RDB persistent (full), fork a child process, memory for a specified interval of time snapshot of the data set to write to a temporary file, after success, then replace the previous file, binary compression storage. It is triggered by configuring the save parameter, save (blocking), bgsave, shutdown, and flushall commands.
-
Advantages: Flexible backup frequency and period, suitable for cold backup, data set is easy to recover, persistent child processes do not affect the I/O of the main process.
-
Disadvantages: Large window for missing data, when the data set is large, fork child process can cause the entire server to be out of service for hundreds of milliseconds, or even 1 second
You are not advised to enable the RDB function on the active Redis node. This will block for a period of time, especially when the data volume is large
AOF persists (increments), appending the executed write command to the aOF_BUf buffer of the server state in protocol format, and then synchronizing the data to the disk according to the corresponding policy. As the file gets larger, the AOF file needs to be rewritten periodically (by the fork child process). Appendonly Yes Enable AOF.
To load the AOF file, you need to create a pseudo client first, and then send the commands to the Redis server one by one, and then the server executes the corresponding commands in full.
-
Write policy: configure appendsync to synchronize data every second (everysec), wait until the buffer is full (no), and synchronize data every time a change occurs (always).
-
Rewrite mechanism: Set the rewrite timing to auto-aof-rewrite-min-size and auto-aof-rewrite-percentage. Multiple write commands are merged into one. Data that has timed out will not be written to the file. Commands executed by the parent process during the rewrite are sent in batches through the pipeline to the child process, which overwrites and plays back. Only a few commands accumulate in the parent process after the child exits, and the parent process only needs to play back a few commands.
-
Advantages: Higher data security, no disk addressing overhead in Append mode, very high write performance, and no damage to saved data.
-
Disadvantages: AOF files are usually larger than RDB files, large data set recovery is slower than RDB, AOF is more complex based on command log /merge/ playback is prone to bugs
Starting with Redis4.0, rDB-Aof hybrid persistence is allowed (enabled by default in 5.0). The rewrite mechanism is optimized. After rewriting, the first half of the new AOF file is full data in RDB format and the second half is incremental data in AOF format. When the Redis instance is restarted, the memory is rebuilt using RDB persistent files, and the AOF is used to replay recent operations to fully restore the state before the restart.
Redis Data expiration policy
- Periodic deletion: When the expiration time of a key is set, a timer is created to delete the key immediately when the expiration time comes. CPU unfriendly.
- Lazy delete: let a key expire, but every time you retrieve it from key space, check if it has expired and delete it if it has; If it is not expired, the key is returned.
- Periodic deletion: In a specified period of time, iterate through each database in the server several times, randomly check the expiration time of some keys from the database expires dictionary, and delete the expired keys.
The Expires dictionary holds the expiration time data for all keys that are set to expire, where key is a pointer to a key in the key space and value is the expiration time represented by the millisecond precision UNIX timestamp for that key. Both lazy expiration and periodic expiration policies are used in Redis.
Redis data elimination strategy
When the Redis memory data set size rises to a certain size, the data elimination strategy is implemented. Redis offers the following eight data elimination strategies:
- The global key space is selectively removed
- Noeviction: New write operations will report an error. (the default)
- Allkeys-lru: removes the oldest unused key in key space. (This is the most commonly used)
- Allkeys-random: Randomly removes a key from the key space.
- Allkeys-lfu: Remove the least frequently used key (4.0) in key space
- Set the expiration time of the key space to selectively remove
- Volatile -lru: Removes the longest unused key from the key space with expiration time set.
- Volatile -random: Randomly removes a key from the key space with an expiration time set.
- Volatile – TTL: In the key space with an expiration time, the key with an earlier expiration time is removed first.
- Volatile -lfu: Removes the least frequently used key (4.0) from the key space with the expiration time set
LRU: Not a strict IMPLEMENTATION of LRU, by sampling a small number of keys and then retrieving the best fit (the one with the longest unaccessed time) among the sampled keys.
MySQL has 2000W of data, Redis only has 20W of data, how to ensure that the data in Redis is hot data? Select the Allkeys-LRU policy. If you are in Redis 4.0, consider using volatile- LFU
If a large number of keys need to be set to expire at the same time, what should I pay attention to?
If the expiration time of a large number of keys is too concentrated, it is generally necessary to add a random value to the expiration time to spread the expiration time. Increase the Hz parameter.
Redis transactions
A Redis transaction is the one-time, sequential, exclusive execution of a sequence of commands in a queue. There is no concept of isolation levels. If any command in the transaction fails, the remaining commands are still executed.
Open a transaction with the MULTI command. Any command executed after this statement is considered to be an operation within the transaction. Finally, all operations within the transaction can be committed/rolled back by executing EXEC/DISCARD. After the transaction is started, all statements sent to the Redis Server will be temporarily stored in the Server. If a communication failure occurs between the client and the server before the transaction is opened and the network is disconnected, all subsequent statements to be executed will not be executed by the server.
How to implement the Redis CAS operation?
In Redis transactions, the WATCH command can be used to provide CAS functionality.
Redis Primary/secondary synchronization
The psync command implements full replication and incremental replication. Incremental replication is implemented through the primary server ID, the replication offset, and the primary server replication backlog buffer.
Redis sentry
Redis cluster
Redis Cluster uses hash slot of data fragments to store and read data. Adding and deleting nodes from the Redis Cluster requires manual slot allocation. The master node performs read and write operations in the Redis cluster.
Redis implements distributed locking
Frequent Interview questions
How to implement message queuing using Redis?
Generally, the list structure is used as a queue, with Rpush producing messages and LPOP consuming messages. When lPOP has no messages, sleep properly for a while and try again. You can also use blpop, which blocks when there is no message until it arrives. A 1:N message queue can be implemented using the PUB/SUB topic subscriber pattern. But in the case of a consumer going offline, the production message is lost.
Delay queue: use sortedset, take timestamp as score, call zadd as key to produce the message, consumers use ZrangebyScore directive to get N seconds before the data polling for processing.
reference
- Redis Design and Implementation by Huang Jianhong
- www.processon.com/view/5f7fc4…
- Svip. Iocoder. Cn/Redis/Intel…