1. Why are REIDS so fast
1. Redis mainly operates in memory, single thread
2, the use of I/O multiplexing, call the underlying epoll/kqueue/ EVport function, complexity is O(1) level, with select function bottom, complexity is O(1)
Principle of Redis multiplexing
A single thread (file event dispatcher) manages multiple I/O streams, queues incoming I/O streams, and the file event dispatcher, in turn, fetches them from the queue and forwards them to different event handlers
Second, redis data structure and the underlying implementation
Redis data structures mainly include String, hash, list, set, sorted set
Sorted set’s bottom-store data structure
We use linked lists, skip lists
3. How to query fixed prefix keys from massive data
Scan 0 hon* count 10, where count does not necessarily return a specific number of results. For example, 10 May return 4 data items
How does Redis implement distributed lock
Distributed lock needs to solve the following problems: 1, mutual exclusion 2, security (obtain the lock can only be destroyed by their own) 3, deadlock 4, fault tolerance
How to solve the problem of SETNX long-term effectiveness
- setnx key value
There is a command called setnx key Value in Redis. The operation is atomic and creates an assignment if it does not exist. The time complexity is O(1) and returns 1 on success and 0 on failure
- EXPIRE Key seconds Specifies the lifetime of a key. When a key expires, it will be automatically deleted
The combination is not atomic, and implementing distributed locks will always be exclusive
- SET key value [EX seconds][PX milliseconds][NX|XX]
EX seconds Sets the expiration key time to second
PX milliseconds sets the expired key to xx milliseconds
NX sets the key only if it does not exist, equivalent to setnx
XX sets the key only when the key exists
For example, set lockTarget Hello EX 10 NX
RedisService redisService = StringUtils.getBean(RedisService.class); String result = redisservice. set(lockKey, requestId, SET_IF_NOT_EXIST,SET_WITH_EXPIRE_TIME,expireTime);if("OK".equals(result)){
// Execute an exclusive resource
doOcuppiedWork();
}
Copy the code
A large number of key expiration considerations
Add a random time value to the expiration time of the key
How to use Redis to time asynchronous queues
You can use a list structure, with Rpush producing messages and LPOP consuming messages
Can use BLPOP key [key…] Timeout blocks until there is a message on the queue, which has the disadvantage of providing only one consumer consumption
Redis pub/ SUB can be used. The disadvantage is that messages are stateless and cannot be guaranteed to be reachable
How does Redis persist
RDB(snapshot) persistence saves a full snapshot of data at a point in time
There are policies under redis conf
Save 900 1 save 300 10 save 60 10000 ### Stop-write-on-bgsave-error yes ### rdbCompression noCopy the code
RDB files are generated in two ways
- SAVE command: block the Redis server process until the RDB file is created
- BGSAVE command :fork a child process to create the RDB file without blocking the server process
Automatic trigger for generating RDB persistence
- Redis.conf triggers SAVE timer (bgSave)
- The primary node automatically triggers the master/slave replication
- Perform debug reload
- Shutdown is executed and AOF persistence is not enabled
BGSAVE principle
Disadvantages of RDB persistence
- Full memory data synchronization. A large amount of data deteriorates PERFORMANCE due to I/O
- Data from the current to the last snapshot may be lost due to a redis failure
Persistent AOF (Append – only – the file)
- Log all instructions that change the state of the database except for queries
- Append to save to AOF file as Append (increment)
Advantages and disadvantages of AOF and RDB
- Advantages of RDB: Full data snapshot, small file size, quick recovery
- Disadvantages of RDB: Data cannot be saved after the last snapshot
- Advantages of AOF: High readability, suitable for saving incremental data, data is not easy to lose
- Disadvantages of AOF: Large file size and long recovery time
After REdis4.0, rDB-AOF hybrid persistence mode is adopted. Bgsave is used for mirror full persistence and AOF is used for incremental persistence
7. Redis synchronization mechanism
Master slave synchronization principle
Master-salve Final consistency
Fully synchronous flow
1. Full synchronization process
The slave sends the sync command to the master. The master starts a background thread to generate an RDB file. The master caches the incremental commands received during the snapshot file generation and sends the RDB file to the slave. The master then sends the increment command to the slave
2. Incremental synchronization
The master receives the user’s command and determines whether to extend the command to the SALve. If the write operation is required, the read operation is not required. Then the master appends the command to the AOF file, propagates the operation to other slaves, writes the operation to the cache, and sends the cached data to the Salve
redis sentinal
Resolve the primary/secondary switchover problem after the master is down
Redis cluster principle
- Key idea: Scatter keys across nodes
Consistent hash algorithm
2 ^ 32 ring mapping, if the server will have less hash ring data skew problem, introduce virtual nodes to solve this problem