Redis common interview questions

content

1. What is Redis? What are the advantages of Redis?

2. Why is Redis so fast?

3. What data types does Redis have?

4. Zset data type usage scenarios?

5. What is the maximum length of data that Value of type String can hold?

6. What is the persistence mechanism of Redis? Advantages and disadvantages of each?

6. How to choose a suitable persistence method?

7. How to expand Redis persistent data and cache?

8. Does Redis transaction support isolation?

9. Does Redis transaction guarantee atomicity and does it support rollback?

10. Do you know the cache penetration, cache breakdown and cache avalanche of Redis? Heavy

11. How to ensure the consistency of redis and database data?

12. How does Redis design a distributed lock? How to optimize lock performance?


1. What is Redis? What are the advantages of Redis?

  1. Redis (Remote Dictionary Server), the remote dictionary service, is an open source log-type, key-value database written in ANSI?C language, supporting network, memory-based and persistent, and providing APIs in multiple languages. is a NoSQL database, also known as a structured database
  2. advantage:
  • Fast speed: Because 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); pure memory operation, Redis has excellent performance, and can process more than 100,000 reads per second Write operations, the fastest known Key-Value database.
  • Support rich data types: support String, List, Set, Sorted Set, Hash, and of course several Geospatial, Hyperloglog, Bitmap
  • Rich features:
    • Subscribe to publish Pub/Sub features
    • Key expiration policy
    • transactions, counts, etc.
  • Persistent storage: Redis provides persistent storage solutions for RDB and AOF data, which solves the biggest worry of in-memory databases. If Redis hangs, the data will disappear

2. Why is Redis so fast?

  1. The multiplexing io blocking mechanism is adopted
  2. Simple data structure and time saving operation
  3. Runs in memory, so it's fast
  4. Single thread is used to avoid unnecessary context switching and competition conditions, and it is safe; there is no CPU consumption due to switching caused by multi-process or multi-threading, and there is no need to consider various lock issues. There is no lock release operation, no Performance consumption due to possible deadlocks

3. What data types does Redis have?

  • String
  • List
  • Set
  • Hash
  • Zset
  • Geospatial
  • Hyperloglog
  • Bitmap

4. Zset data type usage scenarios?

  1. Commonly used for rankings, video like rankings, following rankings

5. What is the maximum length of data that Value of type String can hold?

  1. The maximum data length that the String type Value in Redis can hold is 512M

6. What is the persistence mechanism of Redis? Advantages and disadvantages of each?

  1. Redis persistence mechanism has RDB and AOF
  2. RDB persistence mechanism:
  • Write the snapshot of the data set in memory to the disk at the specified time interval. When he recovers, the snapshot file is directly read into the memory. Redis will create (fork) a child process separately for persistence, and the data will be written first. To a temporary file, after the persistence process is over, use this temporary file to replace the last persistent file. During the whole process, the main process does not perform any IO operations, which ensures extremely high performance. Performance, if large-scale data recovery is required, and the integrity of data recovery is not very sensitive, the RDB method is more advanced than the AOF method. The disadvantage of RDB is that the data after the last persistence may be lost. Our default It is RDB. Generally, you do not need to modify this configuration.
  • Trigger mechanism:
    • When the Save rule is satisfied, the rdb file will be automatically triggered
    • Execute the command flushall command, which will also automatically trigger the rdb file
    • Exiting redis will also generate rdb files
    • Remarks will automatically generate a dump.rdb file
  • How to repair rdb file:
    • Just put the rdb file in our redis startup directory, and when redis starts, it will automatically check dump.rdb to restore the data in it (the config get dir command can view the redis startup directory)
  • advantage:
    • Suitable for large-scale data recovery (if you have low data integrity requirements)
  • shortcoming:
    • It takes a certain time interval to process the operation. If redis crashes unexpectedly, the last modified data will be gone.
    • When fork process, it will occupy a certain content space
  1. AOF persistence mechanism:
  • Record each write operation in the form of a log, and record all the instructions in the execution process of Redis (read operations are not recorded), only append files but not rewrite files, and redis will read the file where it starts to rebuild. Data, in other words, if redis restarts, the written instructions will be executed from front to back according to the content of the log file to complete the data recovery.
  • For example, if the aof file is damaged, we can use the redis-check-aof --fix appendonly.aof command to fix it
  • advantage:
    • Every modification is synchronized, which makes the file integrity better
    • Synchronization every second by default, may lose a second of data
    • If you choose not to synchronize, the efficiency is the highest
  • shortcoming:
    • Compared with data files, aof is much larger than rdb, and the repair speed is slower than that of rdb
    • Aof is also slower than rdb, so redis defaults to rdb

PS: Note: If the aof file is larger than the configured 64m, it will fork a new process to rewrite our file

6. How to choose a suitable persistence method?

  1. In general, if you want to achieve data security comparable to PostgreSQL, you should use both persistence features
  2. If you care a lot about your data, but can still tolerate data loss within minutes, then you can just use RDB persistence

7. How to expand Redis persistent data and cache?

  1. If Redis is used as a cache, use consistent hashing to achieve dynamic expansion and contraction
  2. If Redis is used as a persistent storage, a fixed keys-to-nodes mapping must be used, and the number of nodes cannot be changed once it is determined. Otherwise (that is, when the Redis node needs to change dynamically), a system that can rebalance data at runtime must be used, and currently only Redis cluster can do this

8. Does Redis transaction support isolation?

  1. Redis is a single-process program, and it guarantees that the transaction will not be interrupted while executing the transaction, and the transaction can run until all commands in the transaction queue are executed. Therefore, Redis transactions are always isolated

9. Does Redis transaction guarantee atomicity and does it support rollback?

  1. In Redis, a single command is executed atomically, but the transaction does not guarantee atomicity and there is no rollback. Any command in the transaction fails to execute, the rest of the commands will still be executed

10. Do you know the cache penetration, cache breakdown and cache avalanche of Redis? Heavy

  1. cache penetration
  • Concept: The concept of cache penetration is very simple. The user wants to query a piece of data and finds that there is no data in the redis memory, that is, the cache is not hit, so he queries the persistent layer database and finds that there is no data, so this query fails, when there are many users , the cache did not hit, so they all requested the database of the persistence layer, which would put a lot of pressure on the database of the persistence layer, which is equivalent to cache penetration.
  • solution:
    • Bloom filter: Bloom filter is a data structure that stores all possible query parameters in hash form, and checks them at the control layer. If they do not match, they are discarded, thus avoiding the query pressure on the underlying storage system.
    • Cache empty objects: When the storage layer misses, even the returned empty object will be cached, and an expiration time will be set at the same time, and later access to the data will be obtained from the cache, protecting the back-end data source. But there are two problems with this method: 1. If the null value can be cached, it means that the cache needs more space to store the correct key, because there will be a lot of null value keys. 2. Even if the null value is expired, there will still be inconsistencies between the data of the cache layer and the storage layer for a period of time, which will affect the business that needs to maintain consistency
  1. cache breakdown
  • Concept: The key with expiration time is set. This key is a hot data and carries high concurrency. When the key expires, the continuous large concurrency will break down the cache and directly request the database. A large number of requests may be kill the database
  • solution:
    • Set the key to never expire, or when it is about to expire, reset the key through another asynchronous thread
    • add mutex
  1. Cache Avalanche
  • Concept: Since the original cache is invalid and the new cache has not yet arrived (for example: we set the cache with the same expiration time, and a large area of ​​cache expires at the same time), all requests that should have accessed the cache have to query the database. , and cause huge pressure on database CPU and memory, which will seriously cause database downtime. Thus forming a series of chain reactions, causing the entire system to collapse
  • Solution:
    • Try to avoid setting the same expiration time for a large amount of data. If the business layer requires some data to expire at the same time, you can add a small random number to the expiration time when setting the expiration time, such as random 1-3 minute. In this way, a large amount of data will not expire at the same time, and it will also ensure that these data will expire at a similar time and still meet business needs.
    • Cache avalanches can be dealt with through service downgrades. There are different access methods for different data: for non-core data, suspend the acquisition of these data from the cache, but directly return the predefined, null value or error information; for core data, still go to the cache, the cache cannot be found, Continue reading through the database
    • If the instance is down, you need to rely on Redis to cache the high-availability cluster. By finding that the node is down, switch the node to another node

11. How to ensure the consistency of redis and database data?

  1. First of all, to understand this problem, let's analyze and consider this problem from two situations?
  2. First, the first case: if we delete the cache first, and then write to the database, in the case of high concurrency, when the first thread deletes the cache and has not had time to write to the database, the second thread reads the data. At that time, because the cache deleted the data, it will read the database, but at this time, because it has not been written, the old data is read. After reading, the data will be stored in the cache (at this time the first thread has written the new modified value to the cache), so that the value in the cache will be overwritten with the data before the modification (ie the old data). ).
  • Solution: For the first case, the write operation is usually required to be infrequent.
    • First operate the cache, but do not delete the cache, modify the cache to a special value (that is, a value unrelated to the business: such as -1000, etc.), when the client reads the cache and finds that it is a special value, it sleeps for a while (When sleeping again, delete the cache and write the database), and check Redis again. A possible problem with this solution: special values ​​may have an obligation to intrude. The sleep time may occur multiple times, which has a certain impact on performance.
    • Delayed double deletion: delete the cache first, then write to the database, sleep for a while, and delete the cache again. The possible problem with this solution: If the write operation is frequent, there is still the possibility of dirty data.
  1. Write the database first, then delete the cache: If the cache deletion fails after the database is written, the data will be inconsistent.
  • Solution:
    • Set an expiration time for the cache. Problem: The cached data will not be updated within the expiration time
    • Set the hotspot data cache to never expire, but set a logical expiration time in the value, and start a background thread to scan these keys, and delete the logically expired cache.

12. How does Redis design a distributed lock? How to optimize lock performance?

  1. First understand several common commands of Redis:
  • SETNX key value: When the key exists, set the key to value and return 1, if the key exists, return 0.
  • EXPIRE key 10: Set the expiration time of the current key
  • DEL key: Exclusion key
  • GETSET key value: If the key does not exist, it will return null, and then the value of the key will be set to value. If the key exists, the value of the original key will be returned, and then the new value will be set, that is, overwriting.
  1. The simplest distributed lock: use SETNX and DEL two commands. Possible problem: if the process that acquired the lock fails, then it never unlocks. Then the lock will be locked.
  • Optimization: Set an expiration time for the lock (this may still cause a deadlock problem)
  • Optimization: Set the content of the lock to the expiration time. When SETNX fails to acquire the lock, compare this time with the current time. If the lock expires, delete the lock first, and then re-lock it. (There may be problems in this way. In the case of high concurrency, there will be cases where multiple processes get locks at the same time)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324115889&siteId=291194637