search
HomeJavajavaTutorialjava web instance analysis

Text

In actual work projects, cache has become a key component of high-concurrency and high-performance architecture. So why can Redis be used as a cache? First of all, there are two main characteristics of caching:

  • Being in memory/CPU with good access performance in a layered system,

  • Cache data Saturated, with a good data elimination mechanism

Because Redis naturally has these two characteristics, Redis is based on memory operations, and it has a complete data elimination mechanism, which is very suitable as a cache component. .

Among them, based on memory operation, the capacity can be 32-96GB, and the average operation time is 100ns, and the operation efficiency is high. Moreover, there are many data elimination mechanisms. After Redis 4.0, there are 8 types, which makes Redis applicable to many scenarios as a cache.

So why does Redis cache need a data elimination mechanism? What are the 8 data elimination mechanisms?

Data elimination mechanism

The Redis cache is implemented based on memory, so its cache capacity is limited. When the cache is full, how should Redis handle it?

Redis When the cache is full, Redis needs a cache data elimination mechanism to select and delete some data through certain elimination rules so that the cache service can be used again. So what elimination strategies does Redis use to delete data?

After Redis 4.0, there are 6 2 Redis cache elimination strategies, including three major categories:

  • No data elimination

    • noeviction, no data elimination is performed. When the cache is full, Redis does not provide services and directly returns an error.

  • In the key-value pair that sets the expiration time,

    • volatile-random, in the key that sets the expiration time Randomly delete

    • volatile-ttl from the value pairs. The key-value pairs that set the expiration time will be deleted based on the expiration time. The earlier the expiration date, the earlier it will be deleted.

    • volatile-lru, based on the LRU (Least Recently Used) algorithm to filter key-value pairs with an expiration time, and filter data based on the least recently used principle

    • volatile-lfu uses the LFU (Least Frequently Used) algorithm to select the key-value pairs with expiration time set, and filter the data by using the key-value pairs with the least frequency.

  • Among all key-value pairs,

    • allkeys-random, randomly selected from all key-value pairs And delete the data

    • allkeys-lru, use the LRU algorithm to filter in all data

    • ##allkeys-lfu, use the LFU algorithm to filter in all data Filter

java web instance analysis

##Note

: LRU (Least Recently Used) algorithm, LRU maintains a two-way linked list. The head and tail of the linked list represent the MRU end and the LRU end respectively, representing the most recently used data and the most recently least commonly used data respectively. When the LRU algorithm is actually implemented, it is necessary to use a linked list to manage all cached data, which will bring additional space overhead. Moreover, when data is accessed, the data needs to be moved to the MRU on the linked list. If a large amount of data is accessed, many linked list move operations will occur, which will be very time-consuming and reduce Redis cache performance.

Among them, LRU and LFU are implemented based on the lru and refcount attributes of redisObject, the object structure of Redis:
typedef struct redisObject {
    unsigned type:4;
    unsigned encoding:4;
    // 对象最后一次被访问的时间
    unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
                            * LFU data (least significant 8 bits frequency
    // 引用计数                        * and most significant 16 bits access time). */
    int refcount;
    void *ptr;
} robj;

Redis’ LRU will use redisObject’s lru to record the last access time , randomly select the number configured with the parameter maxmemory-samples as a candidate set, and select the data with the smallest lru attribute value among them to eliminate them.

In actual projects, how to choose the data elimination mechanism?

    Prioritize the allkeys-lru algorithm to keep the most recently accessed data in the cache to improve application access performance.
  • The top data uses the volatile-lru algorithm. The top data does not set a cache expiration time. Other data sets an expiration time and is filtered based on LRU rules.
  • After understanding the Redis cache elimination mechanism, let’s see how many modes Redis has as a cache?

Redis cache mode

Redis cache mode can be divided into read-only cache and read-write cache based on whether to receive write requests:

Read-only cache: only handles read operations, all All update operations are performed in the database, so there is no risk of data loss.

    Cache Aside mode

##Read and write cache, read and write operations are performed in the cache, appear Downtime failure will lead to data loss. Cache write-back data to the database is divided into two types: synchronous and asynchronous: java web instance analysis

Synchronous: access performance is low, and it focuses more on ensuring data reliability
  • Read-Throug mode
    • Write-Through mode
    • Asynchronous: There is a risk of data loss, its focus To provide low latency access
    • Write-Behind Mode

java web instance analysis

Cache Aside Mode

Query The data is first read from the cache. If it does not exist in the cache, the data is read from the database. After the data is obtained, it is updated into the cache. However, when updating data, the data in the database will be updated first. Then invalidate the cached data.

Moreover, the Cache Aside mode will have concurrency risks: the read operation will not hit the cache, and then the database will be queried to fetch the data. The data has been queried but has not been put into the cache. At the same time, an update write operation will invalidate the cache. , and then the read operation loads the query data into the cache, resulting in cached dirty data.

Read/Write-Throug mode

Both query data and update data directly access the cache service, the cache service updates the data to the database synchronously. The probability of dirty data is low, but it relies heavily on cache and has greater requirements for the stability of the cache service. However, synchronous updates will lead to poor performance.

Write Behind mode

Both query data and update data access the cache service directly, but the cache service uses an asynchronous way to update the data to the database (through asynchronous tasks) Speed It is fast and the efficiency will be very high, but the consistency of the data is relatively poor, there may be data loss, and the implementation logic is also relatively complex.

In actual project development, the cache mode is selected according to the actual business scenario requirements. After understanding the above, why do we need to use redis cache in our application?

Using Redis cache in applications can improve system performance and concurrency, mainly reflected in

  • High performance: based on memory query, KV structure, simple logical operation

  • High concurrency: Mysql can only support about 2,000 requests per second, and Redis can easily exceed 1W per second. Allowing more than 80% of queries to go through the cache and less than 20% of the queries to go through the database can greatly improve the system throughput

Although using Redis cache can greatly improve the performance of the system, using Without caching, some problems will arise, such as bidirectional inconsistency between the cache and the database, cache avalanche, etc. How to solve these problems?

Common problems with using cache

When using cache, there will be some problems, mainly reflected in:

  • The cache is inconsistent with the double write of the database

  • Cache avalanche: Redis cache cannot handle a large number of application requests, and the transfer to the database layer causes a surge in pressure on the database layer;

  • Cache penetration: access The data does not exist in the Redis cache and the database, causing a large number of accesses to penetrate the cache and transfer directly to the database, causing a surge in pressure on the database layer;

  • Cache breakdown: the cache cannot handle high-frequency hot spots Data, resulting in direct high-frequency access to the database, resulting in a surge in pressure on the database layer;

The cache is inconsistent with the database data

Read-only cache (Cache Aside mode)

For read-only cache (Cache Aside mode), read operations all occur in the cache, and data inconsistency only occurs in delete operations (new The operation will not happen, because new additions will only be processed in the database). When a deletion operation occurs, the cache will mark the data as invalid and update the database. Therefore, during the process of updating the database and deleting cached values, regardless of the execution order of the two operations, as long as one operation fails, data inconsistency will occur.

The above is the detailed content of java web instance analysis. 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
Web Speech API开发者指南:它是什么以及如何工作Web Speech API开发者指南:它是什么以及如何工作Apr 11, 2023 pm 07:22 PM

​译者 | 李睿审校 | 孙淑娟Web Speech API是一种Web技术,允许用户将语音数据合并到应用程序中。它可以通过浏览器将语音转换为文本,反之亦然。Web Speech API于2012年由W3C社区引入。而在十年之后,这个API仍在开发中,这是因为浏览器兼容性有限。该API既支持短时输入片段,例如一个口头命令,也支持长时连续的输入。广泛的听写能力使它非常适合与Applause应用程序集成,而简短的输入很适合语言翻译。语音识别对可访问性产生了巨大的影响。残疾用户可以使用语音更轻松地浏览

如何使用Docker部署Java Web应用程序如何使用Docker部署Java Web应用程序Apr 25, 2023 pm 08:28 PM

docker部署javaweb系统1.在root目录下创建一个路径test/appmkdirtest&&cdtest&&mkdirapp&&cdapp2.将apache-tomcat-7.0.29.tar.gz及jdk-7u25-linux-x64.tar.gz拷贝到app目录下3.解压两个tar.gz文件tar-zxvfapache-tomcat-7.0.29.tar.gztar-zxvfjdk-7u25-linux-x64.tar.gz4.对解

web端是什么意思web端是什么意思Apr 17, 2019 pm 04:01 PM

web端指的是电脑端的网页版。在网页设计中我们称web为网页,它表现为三种形式,分别是超文本(hypertext)、超媒体(hypermedia)和超文本传输协议(HTTP)。

web前端和后端开发有什么区别web前端和后端开发有什么区别Jan 29, 2023 am 10:27 AM

区别:1、前端指的是用户可见的界面,后端是指用户看不见的东西,考虑的是底层业务逻辑的实现,平台的稳定性与性能等。2、前端开发用到的技术包括html5、css3、js、jquery、Bootstrap、Node.js、Vue等;而后端开发用到的是java、php、Http协议等服务器技术。3、从应用范围来看,前端开发不仅被常人所知,且应用场景也要比后端广泛的太多太多。

Python轻量级Web框架:Bottle库!Python轻量级Web框架:Bottle库!Apr 13, 2023 pm 02:10 PM

和它本身的轻便一样,Bottle库的使用也十分简单。相信在看到本文前,读者对python也已经有了简单的了解。那么究竟何种神秘的操作,才能用百行代码完成一个服务器的功能?让我们拭目以待。1. Bottle库安装1)使用pip安装2)下载Bottle文件https://github.com/bottlepy/bottle/blob/master/bottle.py2.“HelloWorld!”所谓万事功成先HelloWorld,从这个简单的示例中,了解Bottle的基本机制。先上代码:首先我们从b

web前端打包工具有哪些web前端打包工具有哪些Aug 23, 2022 pm 05:31 PM

web前端打包工具有:1、Webpack,是一个模块化管理工具和打包工具可以将不同模块的文件打包整合在一起,并且保证它们之间的引用正确,执行有序;2、Grunt,一个前端打包构建工具;3、Gulp,用代码方式来写打包脚本;4、Rollup,ES6模块化打包工具;5、Parcel,一款速度极快、零配置的web应用程序打包器;6、equireJS,是一个JS文件和模块加载器。

web是前端还是后端web是前端还是后端Aug 24, 2022 pm 04:10 PM

web有前端,也有后端。web前端也被称为“客户端”,是关于用户可以看到和体验的网站的视觉方面,即用户所看到的一切Web浏览器展示的内容,涉及用户可以看到,触摸和体验的一切。web后端也称为“服务器端”,是用户在浏览器中无法查看和交互的所有内容,web后端负责存储和组织数据,并确保web前端的所有内容都能正常工作。web后端与前端通信,发送和接收信息以显示为网页。

深入探讨“高并发大流量”访问的解决思路和方案深入探讨“高并发大流量”访问的解决思路和方案May 11, 2022 pm 02:18 PM

怎么解决高并发大流量问题?下面本篇文章就来给大家分享下高并发大流量web解决思路及方案,希望对大家有所帮助!

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
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment