Summary of Redis interview questions (2022 version)

title address
Summary of Java Virtual Machine Interview Questions (2022 Edition) https://blog.csdn.net/qq_37924396/article/details/125881033
Summary of Java Collection Interview Questions (2022 Edition) https://blog.csdn.net/qq_37924396/article/details/126058839
Summary of Mysql database interview questions (2022 version) https://blog.csdn.net/qq_37924396/article/details/125901358
Summary of Spring Interview Questions (2022 Edition) https://blog.csdn.net/qq_37924396/article/details/126354473
Summary of Redis interview questions (2022 version) https://blog.csdn.net/qq_37924396/article/details/126111149
Summary of Java Concurrency Interview Questions (2022 Edition) https://blog.csdn.net/qq_37924396/article/details/125984564
Summary of Distributed Interview Questions (2022 Edition) https://blog.csdn.net/qq_37924396/article/details/126256455

1. Common test points

1. Why is Redis so fast?

First: Redis is completely based on memory. Most of the requests are pure memory operations, which are very fast and the data is stored in memory. Similar to HashMap, the advantage of HashMap is that the time complexity of search and operation is O(1).
Second: The data structure is simple, and the data operation is also simple.
Third: Single thread is used to avoid unnecessary context switching and competition conditions. There is no CPU switching caused by multi-threading, and there is no need to consider various locks. There is no lock release operation, and there is no deadlock problem. Performance consumption.
Fourth: Use the multiplexed IO model, non-blocking IO.

ref: Four major points, understand where Redis is fast

2. Why use single thread?

Official Answer
Since Redis is a memory-based operation, the CPU will not be the bottleneck for Redis, but most likely the size of the machine's memory or network bandwidth. Since single-threading is easy to implement, and the CPU will not become a bottleneck, it is logical to adopt a single-threaded solution.

Detailed reason
1) The performance consumption of various locks is not required

The data structure of Redis is not all simple Key-Value, but also complex structures such as List and Hash. These structures may perform very fine-grained operations, such as adding an element after a long list, and adding an element to the hash. Or delete an object. These operations may require a lot of locks, resulting in a greatly increased synchronization overhead.

2) Single-threaded multi-process cluster solution

The power of single thread is actually very powerful, and the efficiency of each core is also very high. Naturally, multithreading can have a higher performance limit than single threading, but in today's computing environment, even the upper limit of single-machine multithreading is often not enough. Yes, what needs to be further explored is the multi-server clustering solution, and the multi-threading technology in these solutions is still not available.

So a single-threaded, multi-process cluster is a fashionable solution.

3. Cache avalanche, cache breakdown, cache penetration

cache avalanche

  • What is a cache avalanche?
    If a large-scale key failure occurs at a certain moment, it will cause a large number of requests to be hit on the database, resulting in huge pressure on the database. If it is under high concurrency, it may cause the database to Downtime. At this time, if the operation and maintenance restarts the database immediately, there will be new traffic immediately killing the database. This is cache avalanche.

  • Solution
    1) Hot data does not expire
    2) Randomly disperse expiration time
    3) Set multi-level cache

cache breakdown

  • What is cache breakdown?
    Cache breakdown is somewhat similar to cache avalanche. Cache avalanche is a large-scale key failure, while cache breakdown is a key failure of a hot spot. If large concurrent centralized requests are made to it, a large number of requests will be read. The cache did not read the data, resulting in high concurrent access to the database, causing a sharp increase in database pressure. This phenomenon is called cache breakdown

  • Solution
    1. Mutual exclusion lock After the cache expires, use a mutex or queue to control the number of threads that read data and write to the cache. For example, a certain key only allows one thread to query data and write to the cache, while other threads wait. This method will block other threads, and the throughput of the system will drop at this time.
    The hotspot data cache will never expire.
    2. The hotspot data cache will never expire.
    3. Fuse downgrade

cache penetration

  • What is cache penetration?
    Cache penetration means that the data requested by the user does not exist in the cache, that is, there is no hit, and it does not exist in the database at the same time, causing the user to query the database every time the data is requested. If a malicious attacker keeps requesting data that does not exist in the system, a large number of requests will fall on the database in a short period of time, causing excessive pressure on the database, and even causing the database to fail and crash.

  • Solution
    1. Store the invalid key in Redis:
    When the data cannot be found in Redis or the database, we will save the key in Redis, set value = "null", and set its The expiration time is very short, and when there is a request to query this key later, null will be returned directly, so there is no need to query the database again. But this way of handling is problematic. If the non-existent Key value passed in is random every time, it is meaningless to store it in Redis.
    2. Use Bloom filter:
    If the Bloom filter determines that a certain key does not exist in the Bloom filter, then it must not exist. If it determines that a certain key exists, then it is likely to exist (there is a certain misjudgment Rate). So we can add a Bloom filter before the cache, store all the keys in the database in the Bloom filter, go to the Bloom filter to check whether the key exists before querying Redis, and return directly if it does not exist , not allowing it to access the database, thus avoiding the query pressure on the underlying storage system.

4. Database and cache double write consistency

  • Delete the cache first and then write to the DB
    to generate dirty data. If there is dirty data, it means that if there is no update, the data obtained by the query will be old data).
    For example, two concurrent operations, one is an update operation and the other is a query operation. After the update operation deletes the cache, the query operation does not hit the cache. The old data is first read out and put into the cache, and then the update operation updates the database. Therefore, the data in the cache is still old data, causing the data in the cache to be dirty, and it will continue to be dirty like this.

  • Write to the DB first and then delete the cache.
    The probability of dirty data is small, but there will be consistency problems; if the update operation is performed at the same time and the query operation is hit, the data obtained by the query is old data. But it will not affect subsequent queries.
    For example, one is a read operation, but it does not hit the cache, and then fetches data from the database. At this time, a write operation occurs. After the database is written, the cache is invalidated, and then the previous read operation puts the old data in. So it will cause dirty data.

Solution
1) Set expiration time for cache to achieve final consistency;
2) Use middleware such as Cannel to monitor binlog for asynchronous update;
3) Ensure consistency through 2PC or Paxos protocol.

5. Redis persistence

  • RDB

RDB is a snapshot storage persistence method. Specifically, the memory data of Redis at a certain moment is saved to a file on the hard disk. The default saved file name is dump.rdb, and dump.rdb will be reloaded when the Redis server starts The data of the file is restored to the memory.
Enabling the RDB Persistence Mode
It is very simple to enable the RDB persistence mode. The client can send the save or bgsave command to the Redis server to let the server generate an RDB file, or specify the trigger RDB condition through the server configuration file.

Several advantages of RDB
Compared with the AOF method, it is faster to restore data through rdb files.
rdb files are very compact and suitable for data backup.
Data backup through RDB has little impact on the performance of the Redis server because it is generated using subprocesses.

Several disadvantages of RDB
If the server is down, the use of RDB will cause data loss within a certain period of time. For example, if we set the synchronization once every 10 minutes or once every 5 minutes to reach 1000 writes, then if the trigger is not reached If the conditional server crashes, the data for this time period will be lost.
Using the save command will cause the server to block, and the subsequent request can only be received after the data synchronization is completed.
When using the bgsave command to forks a child process, if the amount of data is too large, the forks process will also be blocked. In addition, the forks child process will consume memory.

  • AOF

The AOF persistence method will record every write operation command from the client to the server, and append and save these write operations to the end of the aof file with the suffix of the Redis protocol. When the Redis server restarts, the command of the aof file will be loaded and run. In order to achieve the purpose of data recovery.

Advantages of AOF
AOF only appends log files, so it has less impact on server performance, is faster than RDB, and consumes less memory.

Disadvantages of AOF
The log file generated by the AOF method is too large. Even if it is rewritten by AFO, the file size is still large.
Restoring data is slower than RDB.

6. Five data types

  • 1. String is the most basic type of redis, which can be understood as exactly the same type as memcached, and a key corresponds to a value. value is not only a string, but also a number. The string type is binary safe, which means that the string type of redis can contain any data, such as jpg pictures or serialized objects. The value of string type can store up to 512M.
  • 2. Hash is a collection of key-value (key-value). The hash of redis is a mapping table of string key and value. Hash is especially suitable for storing objects. Commonly used commands: hget, hset, hgetall, etc.
  • 3. The List list is a simple list of strings, sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list Common commands: lpush, rpush, lpop, rpop, lrange (get list fragments), etc.
    Application scenarios: list has many application scenarios, and it is also one of the most important data structures of Redis. For example, twitter's follow list and follower list can be implemented with list structure.
    Data structure: list is a linked list, which can be used as a message queue. Redis provides the push and pop operations of List, and also provides an API to operate a certain segment, which can directly query or delete the elements of a certain segment.
    Implementation method: The implementation of redis list is a doubly linked list, which can support reverse search and traversal, which is more convenient to operate, but brings additional memory overhead.
  • 4. Set is an unordered collection of string types. Collections are implemented through hashtables. The elements in the set are out of order and there is no repetition.
    Commonly used commands: sdd, spop, smembers, sunion, etc.
    Application scenario: The function provided by redis set is a list like list. The special feature is that set is automatically deduplicated, and set provides a function to judge whether a member is in a set collection.
  • 5. Like set, Zset is a collection of string type elements, and duplicate elements are not allowed. Commonly used commands: zadd, zrange, zrem, zcard, etc.
    Usage scenario: The sorted set can sort the members by providing an additional priority (score) parameter by the user, and it is inserted in order, that is, automatically sorted. When you need an ordered and non-repeating collection list, you can choose the sorted set structure. Compared with set, sorted set is associated with a parameter score of double type weight, so that the elements in the set can be arranged in an orderly manner according to the score. Redis uses the score to sort the members in the set from small to large.
    Implementation method: Redis sorted set internally uses HashMap and skip list (skipList) to ensure the storage and order of data. The HashMap stores the mapping from members to score, while the skip list stores all members. The sorting basis is For the score stored in the HashMap, the structure of the jump table can be used to obtain relatively high search efficiency, and the implementation is relatively simple.
    Me: I have summarized a picture before, about the application scenarios of data types, if you are interested, you can go to my Nuggets to see. .
    Summary of data type application scenarios
    insert image description here

7. Six Elimination Strategies

Redis has six elimination strategies
insert image description here

8. What are the commonly used connection pools of Redis

There are two commonly used: Jedis and Lettuce.

  • Jedis: The default Redis client in Spring Boot 1.5.x is implemented by directly connecting to Redis Server. If it is not thread-safe in a multi-threaded environment, then use the connection pool to add physical connections to each jedis instance;
  • Lettuce: The default Redis client after Spring Boot 2.x, implemented based on Netty, connection instances can be accessed concurrently by multiple threads, and connection instances can be added as needed if one connection instance is not enough.

ref: how spring cache and springboot use redis connection pool

2. Cluster

1. Redis master-slave replication

Master-slave replication can be divided into two methods: full synchronization and incremental synchronization according to needs.

  • Full synchronization
    Redis full replication generally occurs in the initial stage of the slave. At this time, the slave needs to copy all the data on the master. The specific steps are as follows: 1) The slave connects to the master and
    insert image description here
    sends the SYNC command;
    2) The master executes after receiving the SYNC command The BGSAVE command produces RDB files, and uses the buffer to record all write commands executed afterwards;
    3) After the master executes BGSAVE, it sends snapshot files to all slaves, and continues to record the executed write commands during the sending process;
    4) The slave receives After reaching the snapshot, discard all the old data and load the received data;
    5) After the master snapshot is sent, it will start sending buffer write commands to the slave;
    6) The slave finishes loading the snapshot and starts to accept commands Request, execute the write command from the master buffer;
    7) After the slave completes the above data initialization, it can begin to accept the user's read request.

  • Incremental synchronization Incremental
    replication is actually the process of synchronizing the write operation of the master to the slave when the slave is initialized and starts to work normally. The process of incremental replication is mainly that the master sends the same write command to the slave every time it executes a write command, and the slave accepts and executes the write command, thereby maintaining the consistency of the master and slave.

ref: Summary of Redis master-slave replication principle

2. Redis sentinel mode

insert image description here

When the main server is interrupted, a slave server can be upgraded to the main server to continue to provide services, but this process requires manual operation. To this end, Redis 2.8 provides sentry tools to realize automated system monitoring and fault recovery functions.
The role of the sentinel is to monitor the running status of the Redis system. Its functions include the following two.
(1) Monitor whether the master server and the slave server are running normally.
(2) When the master server fails, the slave server is automatically converted to the master server.
How the Sentinel works:
Each Sentinel (Sentinel) process sends a PING command to the Master master server, Slave slave server and other Sentinel (Sentinel) processes in the entire cluster once a second.
If an instance (instance) is longer than the value specified by the down-after-milliseconds option from the last valid reply to the PING command, the instance will be marked as subjectively offline (SDOWN) by the Sentinel process. If a Master master
server Marked as subjective offline (SDOWN), all Sentinel (sentinel) processes that are monitoring the Master main server must confirm that the Master main server has indeed entered the subjective offline state once per second. When there are enough Sentinel (sentinel
) ) process (greater than or equal to the value specified in the configuration file) confirms that the Master main server has entered the subjective offline state (SDOWN) within the specified time range, and the Master main server will be marked as objective offline (ODOWN). Under normal circumstances
, Each Sentinel process sends INFO commands to all Master servers and Slave servers in the cluster every 10 seconds.
When the Master main server is marked as objectively offline (ODOWN) by the Sentinel (sentinel) process, the frequency that the Sentinel (sentinel) process sends the INFO command to all Slave slave servers of the offline Master main server will be changed from once every 10 seconds to every once a second.
If there are not enough Sentinel (sentinel) processes to agree that the Master main server is offline, the objective offline status of the Master main server will be removed. If the Master main server sends a PING command to the Sentinel (sentinel) process again and returns a valid reply, the subjective offline status of the Master main server will be removed.
Advantages and disadvantages of the sentry mode
Advantages:
the sentry mode is based on the master-slave mode, and all the advantages of the master-slave mode are available in the sentry mode.
The master-slave can be switched automatically, the system is more robust and the availability is higher.
Disadvantages:
It is difficult for Redis to support online expansion, and online expansion will become very complicated when the cluster capacity reaches the upper limit.

3. Redis high availability cluster

insert image description here
The sentinel mode of redis can basically achieve high availability and separation of read and write, but in this mode, each redis server stores the same data, which is a waste of memory, so the cluster mode is added to redis3.0 to implement redis Distributed storage means that different content is stored on each redis node.
Redis-Cluster adopts a non-central structure, and its characteristics are as follows:
all redis nodes are interconnected with each other (PING-PONG mechanism), and internal binary protocol is used to optimize transmission speed and bandwidth.
The fail of a node takes effect only when more than half of the nodes in the cluster detect the failure.
The client is directly connected to the redis node without an intermediate proxy layer. The client does not need to connect to all nodes in the cluster, but only to any available node in the cluster.

3. Common commands

1. key pattern query corresponding key

(1) redis allows fuzzy query key with 3 wildcard characters *,?, []
(2) randomkey: return random key  
(3) type key: return the type of key storage
(4) exists key: determine whether a key exists
( 5) del key: delete key
(6) rename key newkey: rename
(7) renamenx key newkey: if newkey does not exist, the modification is successful
(8) move key 1: move key to 1 database
(9) ttl key: query key Life cycle (seconds) -2 means that the key does not exist, -1 means that no valid time is set and will not expire
(10) expire key integer value: set the life cycle of the key in seconds
(11) pexpire key integer value: set The life cycle of the key is in milliseconds
(12) pttl key: query the life cycle of the key (milliseconds)
(13) perisist key: set the specified key to be permanently valid
(14) flushdb clears the current library
(15) flushall clears all libraries

2. String type operations

(1) set key value [ex seconds] [px milliseconds] [nx/xx]  
If ex and px are written at the same time, the later validity period shall prevail
nx: if the key does not exist, create
xx: if the key exists, modify it Its value
(2) get key: value
(3) mset key1 value1 key2 value2 set multiple values ​​at one time
(4) mget key1 key2: get multiple values ​​at one time
(5) setrange key offset value: offset the offset of the string If the byte is changed to value,
if the offset > the length of the string, the character will be automatically filled with 0x00
(6) append key value: append the value to the original value of the key
(7) getrange key start stop: get the [start, value in the range of stop]
For the subscript of the string, the left number starts from 0, and the right number starts from -1.
Note: when start>length, an empty string is returned.
When stop>=length, it is intercepted to the end of the string.
If start If the position is on the right of stop, return an empty string
(8) getset key nrevalue: get and return the old value, and set the new value
(9) incr key: self-increment, return the new value, if incr is not an int value then Return error, incr is a key that does not exist, then set the key to 1
(10) incrby key 2: jump 2 auto-increment
(11) incrbyfloat by 0.7: auto-increment floating point number 
(12) setbit key offset value: Set the offset corresponding to the value on the binary, and return the old value on the bit
Note: If the offset is too large, it will be filled with 0 in the middle.
The maximum offset is
2^32-1, and the maximum value can be released The character string is 512M
(13) bitop operation destkey key1 [key2…] Do opecation on key1 key2 and save the result on destkey
opecation can be AND OR NOT XOR
(14) strlen key: take the length of the value of the specified key
( 15) setex key time value: set the value corresponding to the key, and set the validity period to time seconds

3. Linked list operation

The list type of Redis is actually a doubly linked list in which each sub-element is a string type, and the maximum length of the linked list is 2^32. A list can be used as both a stack and a queue.
The pop operation of list also has a blocking version, mainly to avoid polling
(1) lpush key value: insert the value into the head of the linked list
(2) rpush key value: insert the value into the end of the linked list
(3) lpop key: return and Delete the head element of the linked list
(4) rpop key: return and delete the tail element of the linked list
(5) lrange key start stop: return the element in [start, stop] in the linked list
(6) lrem key count value: delete the value from the linked list , delete the absolute value of count and end count > 0 Delete count < 0 from the header Delete count=0 from the end Delete all (
7) ltrim key start stop: cut the link corresponding to the key, cut [start, stop] One paragraph and reassign the reformation to the key
(8) lindex key index: return the value on the index index
(9) llen key: calculate the number of elements in the linked list
(10) linsert key after|before search value: search for the search in the key linked list , and insert value before | after the search value
(11) rpoplpush source dest: take out the end of source, put it in the head of dest, and return the unit value Application
    scenario: task + bak double-linked list completes the security queue
insert image description here
Business logic: rpoplpush task bak
receives the return value and performs business processing.
If successful, rpop bak clears the task. If unsuccessful, next time the task is fetched from the bak table.
(12) brpop, blpop key timeout: wait for the tail/head element timeout of the pop-up key In order to wait for the timeout, if the timeout is 0, it will wait forever. Application scenario: long polling ajax, which can be used in online chat

4.hashes type and operation

Redis hash is a mapping table of string type fields and values, and its addition and deletion operations are O(1) (average). Hash is especially suitable for storing objects. Storing an object in the hash type will take up less memory, and the entire object can be accessed conveniently.
(1) hset myhash field value: set the field of myhash to value
(2) hsetnx myhash field value: set the field of myhash to value if it does not exist
(3) hmset myhash field1 value1 field2 value2: set multiple fields at the same time
(4 ) hget myhash field: get the specified hash field
(5) hmget myhash field1 field2: get multiple fields at a time
(6) hincrby myhash field 5: add the specified value to the hash field
(7) hexists myhash field: test the specified Whether the field exists
(8) hlen myhash: return the number of hash fields
(9) hdel myhash field: delete the specified field
(10) hkeys myhash: return all hash fields
(11) hvals myhash: return all hash values
​​(12 )hgetall myhash: Get all field and value 
configurations in a hash:
hash_max_zipmap_entries 64 #Configure fields up to 64
hash_max_zipmap_value 512 #Configure value up to 512 bytes

5. Set structure operation

Features: disorder, determinism, uniqueness

(1) sadd key value1 value2: add elements to the set
(2) smembers key: get all the elements in the set
(3) srem key value: delete an element in the set
(4) spop key: return and delete a random element in the set Element (you can take a lottery, and you will not draw someone repeatedly)   
(5) srandmember key: randomly select an element
(6) sismember key value: judge whether the set has a certain value
(7) scard key: return the number of elements in the set
(8) move source dest value: move the value of source to the dest collection
(9) sinter key1 key2 key3: find the intersection of key1 key2 key3
(10) sunion key1 key2: find the union of key1 key2
(11) sdiff key1 key2 : Find the difference of key1 and key2
(12) sinterstore res key1 key2: Find the intersection of key1 and key2 and save it in res

6. Ordered Sets

(1) zadd key score1 value1: Add element
(2) zrange key start stop [withscore]: After sorting the collection, return the elements with the rank [start, stop] by default. Withscores is to print out the score (3
) zrank key member: Query the ranking of members (starting from 0 in ascending order)
(4) zrangebyscore key min max [withscores] limit offset N: After sorting the set (in ascending order), take the elements whose score is within [min, max] and skip the offset , take out N
(5) zrevrank key member: query member ranking (starting from 0 in descending order)
(6) zremrangebyscore key min max: delete elements according to score, delete score between [min, max]
(7) zrem key value1 value2: delete elements in the set
(8) zremrangebyrank key start end: delete elements by ranking, delete the rank between [start, end]
(9) zcard key: return the number of set elements
(10) zcount key min max: returns the number of elements in the interval [min, max]
(11) zinterstore dest numkeys key1[key2…] [WEIGHTS weight1 [weight2…]] [AGGREGATE SUM|MIN|MAX]

7. Server related commands

(1) ping: determine whether the connection is alive
(2) echo: print some content on the command line
(3) select: select the database
(4) quit: exit the connection
(5) dbsize: return the number of keys in the current database
(6) info : Obtain server information and statistics
(7) monitor: Dump received requests in real time
(8) config get configuration item: obtain server configuration information config set configuration item value: set configuration item information
(9) flushdb: delete the current selection All keys in the database
(10) flushall: delete all keys in all databases
(11) time: display server time, timestamp (seconds), microseconds
(12) bgrewriteaof: save rdb snapshots in the background
(13) bgsave: Save rdb snapshots in the background
(14) save: save rdb snapshots
(15) lastsave: last save time
(16) shutdown [save/nosave]
Note: If you accidentally run flushall, immediately shutdown nosave, shut down the server, and then manually edit aof file, delete the flushall-related lines in the file, and then start the server to rewind the original data. If after flushall, the system happens to bgwriteaof, then aof will be cleared and the data will be lost.
(17) showlog: display slow queries

4. Distributed lock

1. Redis implements distributed reentrant lock method

11

5. Use of Redistemplate

1. The difference between RedisTemplate and StringRedisTemplate

1. The relationship between the two is that StringRedisTemplate inherits RedisTemplate.
2. The data of the two are not common; that is to say, StringRedisTemplate can only manage the data in StringRedisTemplate, and RedisTemplate can only manage the data in RedisTemplate.
3. There are two serialization strategies adopted by SDR by default, one is the serialization strategy of String, and the other is the serialization strategy of JDK.
StringRedisTemplate adopts the serialization strategy of String by default, and the saved key and value are serialized and saved using this strategy.
RedisTemplate adopts the JDK serialization strategy by default, and the saved key and value are serialized and saved using this strategy.

ref: The difference between redisTemplate and stringRedisTemplate

Guess you like

Origin blog.csdn.net/qq_37924396/article/details/126111149