search
HomeDatabaseRedisHow to solve common problems of caching data based on Redis

1. Cache penetration

1.1 Problem description

Cache penetration is to request a non-existent key on the client/browser side. This key does not exist in redis. There is no data source in the database. Every time a request for this key cannot be obtained from the cache, the data source will be requested.

If you use a non-existent user ID to access user information, it is neither in redis nor in the database. Multiple requests may overwhelm the data source

How to solve common problems of caching data based on Redis

1.2 Solution

A data that must not exist in the cache and cannot be queried. Since the cache is passively written when there is a miss, the cache does not exist. For fault tolerance considerations, the data that cannot be queried will not be Caching in redis will cause the database to be requested every time data that does not exist is requested, which loses the meaning of caching.

(1) If the data returned by a query is empty (regardless of whether the data does not exist), we will still cache the empty result (null) and set the expiration time of the empty result to be very short, with the longest No more than five minutes

(2) Set the accessible list (whitelist): use the bitmaps type to define an accessible list, the list id is used as the offset of the bitmaps, each access and the id in the bitmap Compare, if the access id is not in the bitmaps, intercept and disallow access.

(3) Use Bloom filter

(4) Conduct real-time data monitoring and find that when the hit rate of Redis decreases rapidly, check the access objects and access data, and set up a blacklist.

2. Cache breakdown

2.1 Problem description

When the user requests data for an existing key, the data for the key in redis has become outdated. If a large number of concurrent requests find that the cache has expired, the data source will be requested to load the data and cached in redis. At this time, the large number of concurrency may overwhelm the database service.

2.2 Solution

When the data of a certain key is requested in large numbers, this key is hot data and needs to be considered to avoid the "breakdown" problem.

(1) Pre-set popular data: Before redis peak access, store some popular data into redis in advance, and increase the duration of these popular data keys

(2) Real-time adjustment : On-site monitoring of which data is popular and real-time adjustment of key expiration length

(3) Use lock:

  • is when the cache expires (the value taken out is judged to be Empty), instead of loading the db immediately.

  • First use some operations of the cache tool with a successful operation return value (such as Redis's SETNX) to set a mutex key

  • When When the operation returns successfully, perform the load db operation again, restore the cache, and finally delete the mutex key;

  • When the operation returns failure, it proves that there is a thread loading db, and the current thread sleeps for a while Time to retry the entire get cache method.

How to solve common problems of caching data based on Redis

3. Cache avalanche

3.1 Problem description

The corresponding data exists, but the key data Has expired (the redis cache will expire and this key will be automatically deleted). At this time, a large number of concurrent requests access different keys, that is, a large number of different keys are accessed at the same time. At this time, the key is in the expiration stage, and the database will be requested. A large number of concurrent requests It will overwhelm the database server. This situation is called cache avalanche. The difference from cache breakdown is that the former is a key.

How to solve common problems of caching data based on Redis

3.2 Solution

The avalanche effect when the cache fails has a terrible impact on the underlying system!

(1) Build a multi-level cache architecture:

  • nginx cache redis cache other caches (ehcache, etc.)

(2) Use locks or queues:

  • Use locks or queues to ensure that there will not be a large number of threads accessing the database at once Read and write are performed continuously to avoid a large number of concurrent requests from falling on the underlying storage system in the event of a failure. Not applicable to high concurrency situations

(3) Set the expiration flag to update the cache:

  • Record whether the cached data has expired ( Set the advance amount), if it expires, it will trigger a notification to another thread to update the cache of the actual key in the background.

(4) Spread the cache expiration time:

  • For example, we can based on the original expiration time Add a random value, such as 1-5 minutes random, so that the repetition rate of each cache's expiration time will be reduced, making it difficult to cause collective failure events.

The above is the detailed content of How to solve common problems of caching data based on Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

redis error什么意思redis error什么意思Jun 17, 2019 am 11:07 AM

redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools