Difference between Memcache and Redis
-
The Memcache code hierarchy is similar to Hash
- Simple data types are supported
- Persistent storage of data is not supported
- Master/slave is not supported
- Sharding is not supported
-
Redis
- Data type richness
- Supports persistent storage on data disks
- Support the master-slave
- Support the shard
Why is Redis so fast (100000+QPS (number of queries per second))
- Completely based on memory, most requests are pure memory operations, high execution efficiency
- The data structure is simple and the manipulation of the data is simple
- Using single thread, single thread can also handle high concurrency requests, want to multi-core can also start multi-instance
- Use multiplex I/O multiplexing model, non-blocking IO
Redis USES multiplex I/O functions: epoll/kqueue/evport/select
- Different I/O multiplexing functions are selected for different operating systems
- I/O multiplexing functions with O(1) time complexity are preferred as the underlying implementation
- Use O(n) select as a guarantee
- Listen for I/O events based on the React design mode
Redis data type
- String
- Hash, for storing objects
- Sort by insertion order
- Set, an unordered non-repeating Set
- Sorted Set, Sorted by score from lowest to highest
- HyperLogLog for counting, used to support geo-location information storage
Redis data type Underlying data type base
- string
- The list
- The dictionary
- Jump table
- The integer set
- List of compression
- object
Redis retrieves keys with a fixed prefix from a large number of keys
SCAN cursor [MATCH pattern] [COUNT COUNT]
Example: Scan 0 match key1 count 10*
- A cursor based iterator needs to continue the previous iteration based on the previous cursor
- Start a new iteration with 0 as cursor until the command returns cursor 0 to complete a traversal
- There is no guarantee that each execution will return a given number of elements, and fuzzy queries are supported
- The number of returns at a time is not controllable and can only meet the count parameter with a high probability
How to implement distributed locks through Redis
-
Distributed locks need to be solved
- Mutual exclusivity
- security
- A deadlock
- Fault tolerance
-
Use setnx key Value
- If the key does not exist, it is created and assigned
- If the time complexity is O(1), 1 is returned if the time complexity is set successfully, and 0 is returned if the time complexity fails
-
How to solve the problem of setNX long-term effectiveness
- Set the lifetime of a key. When a key expires, it is automatically deleted
- expire key secondsDisadvantages: atomicity can not be satisfied
-
Using the set key value [EX seconds] [PX milliseconds] [NX | XX]
- Example:example:set key1 123456 ex 10 nx
- EX seconds: Set key expiration time to second (second)
- PX milliseconds: sets the expiration time of the key to milliseconds.
- NX: Sets the key only when the key does not exist
- XX: Sets a key only when the key already exists
- Return OK if the set operation completes successfully, nil otherwise
A large number of keys in Redis expire at the same time
- When a cluster expires, it is time consuming to clear a large number of keys, resulting in a temporary lag
- Solution: Assign a random value to each key when setting its expiration time
How to use Redis for asynchronous queues
-
Using a List as a queue, Rpush produces messages and LPOP consumes messages
- Disadvantages: direct consumption without waiting for values in the queue
- Remedy: LPOP retries can be invoked by introducing sleep mechanisms at the application level
-
With BLPOP, block until the queue has a message or times out
- Cons: can only be used by one consumer
-
Pub/SUB: Theme subscriber mode
- Disadvantages: Messages are published stateless and cannot be guaranteed to be reachable
How does Redis persist
-
RDB (Snapshot) Persistence: Saves a snapshot of full data at a point in time
- Advantages: Full data snapshot, small file size, quick recovery
- Disadvantages:
- Full synchronization of memory data. A large amount of data severely affects performance due to I/O
- Data from the current snapshot to the last snapshot may be lost due to a Redis failure
-
AOF(append-only-file) persistence: Saves the write status
- Record all commands that change the state of the database except for queries, append them to an AOF file (increment)
- Advantages: High readability, suitable for saving incremental data, data is not easy to lose
- Disadvantages: Large file size and long recovery time
- Record all commands that change the state of the database except for queries, append them to an AOF file (increment)
AOF log rewrite process:
- Call fork() to create a child process
- The child process writes the new AOF to a temporary file, independent of the original AOF file
- The main process keeps writing new changes to both memory and the original AOF
- The master process receives the completion signal of the child’s rewriting of the AOF and synchronizes incremental changes to the new AOF
- Replace the old AOF file with the new AOF file
- Rdb-aof hybrid persistence mode
Redis primary/secondary replication
Generally, the master service provides write operations and the slave service provides read operations
-
Benefits of master-slave replication
- Data redundancy to achieve hot backup of data
- Fault recovery to avoid service unavailability caused by single point of failure
- Read/write separation and load balancing. Primary nodes load read and write, secondary nodes are responsible for reading, improve server concurrency
- The high availability foundation is the foundation of the sentinel mechanism and cluster implementation
-
Principle of master-slave replication www.cnblogs.com/kismetv/p/9…