Little knowledge about Redis

1.Redis can be used in the following scenarios:

  1. Caching: Redis accepts and processes cache data requests and stores the data in memory, thereby increasing the speed of data retrieval.

  2. Distributed locks: Redis provides distributed lock capabilities, which can easily solve thread safety issues.

  3. Message queue: Redis provides a message queue function, which can be used in asynchronous processing and other scenarios.

  4. Counter: Redis can use self-increment and self-decrement operations as counters, which can quickly implement counter functions.

  5. Online applications: Redis has excellent performance and can store hot data, and is often used in online applications.

2. Cache penetration, cache breakdown and cache avalanche.

  1. Cache penetration: refers to querying a data that must not exist. Since there is no relevant data in the cache, the database will be accessed every time the request is made, causing excessive pressure on the database. The solution is to add a Bloom filter when querying the request to filter out non-existent data.

  2. Cache breakdown: refers to the failure of a certain hotspot data under high concurrency conditions, resulting in a large number of requests to access the database and increasing the pressure on the database. The solution is to set a short expiration time, use distributed locks, etc.

  3. Cache avalanche: refers to a large number of keys in the cache that expire at the same time, resulting in a large number of requests to access the database and increasing the pressure on the database. The solution is to set different expiration times, data backup, etc.

3. Cache consistency problem

  1. What is cache consistency problem

When using caching technology, since the cache is an intermediate layer between the application and the database, in order to improve data access efficiency, the data stored in the cache may be inconsistent with the data stored in the database.

For example, when a data update operation occurs, the updated data should be updated to both the cache and the database. However, if only the data in the database is updated and the data in the cache is not updated, the data in the cache will be inconsistent with the database. The data in the cache is inconsistent, causing cache consistency issues.

  1. Causes of cache consistency issues

Cache consistency problems are mainly caused by the following reasons:

(1) Cache and database synchronization delay.

When the data in the database is updated, the data in the cache is not updated immediately, but there is a certain synchronization delay, which will cause the data in the cache to be inconsistent with the data in the database.

(2) The data in the cache is incorrectly updated or deleted.

When the data in the cache is incorrectly updated or deleted, it will cause the data in the cache to be inconsistent with the data in the database.

(3) Data inconsistency caused by concurrent updates.

When multiple clients concurrently update the same data, data inconsistency is likely to occur because the data in the cache may be read and updated by multiple clients at the same time.

     2.Solution to cache consistency problem

To solve the cache consistency problem, the following two solutions are usually adopted:

(1) Use cache update strategy.

When using caching technology, you need to adopt a cache update strategy to synchronize updated data to the cache in a timely manner to ensure that the data in the cache is consistent with the data in the database.

Common update strategies include "read-write mode", "read-after-write mode", "failure mode", etc.

(2) Use cache locking mechanism.

Using the cache locking mechanism can prevent data inconsistencies caused by concurrent updates. When multiple clients concurrently update the same data, you can use the cache locking mechanism to lock the read and write permissions of the data so that other clients cannot read and write the data at the same time, thereby avoiding data inconsistency.

4. Persistence Strategy

  1. What is persistence strategy

When using Redis, data needs to be persisted to disk to prevent data loss. Redis provides two persistence strategies: RDB persistence and AOF persistence.

(1) RDB persistence

RDB persistence saves data in Redis to disk in the form of snapshots within a specified time interval. When data needs to be recovered, the data can be recovered from the disk by loading the RDB file. RDB persistence is a compact, high-performance persistence method that is suitable for scenarios where large amounts of data are updated or data is backed up regularly.

(2) AOF persistence

AOF persistence records every write command in Redis in a log file. When data needs to be restored, the data can be reconstructed by re-executing the commands in the log file. AOF persistence provides more granular data recovery capabilities, but requires recording the operation of each command, so it requires more disk space and IO operations compared to RDB persistence.

  1. How to achieve persistence

(1) RDB persistence implementation

RDB persistence is implemented by setting the save command in the Redis.conf configuration file. The save command has two parameters. The first parameter represents the time interval in seconds, and the second parameter represents the execution number, which means that a persistence operation is performed after executing the specified number of write commands within the specified time interval.

For example, setting save 900 1 means that if at least one write operation is performed every 900 seconds, the data will be persisted once.

(2) AOF persistence implementation

AOF persistence is implemented by setting the appendonly parameter to yes in the Redis.conf configuration file. After setting, each write command will be appended to the end of the AOF file.

There are two synchronization methods for AOF files: synchronizing to disk for every write operation (always) and synchronizing to disk once per second (everysec). Among them, the always synchronization method provides the greatest data security, but will have an impact on performance. Therefore, the everysec method is usually used to minimize the performance impact while ensuring a certain level of security.

Guess you like

Origin blog.csdn.net/Flying_Fish_roe/article/details/134823138