Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the reasons for using Redis for caching

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about the reasons for using Redis for caching. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The explanation given to Redis and Baidu Encyclopedia is

"Redis (Remote Dictionary Server), the remote dictionary service, is an open source log, Key-Value database that is written in ANSI C, supports network, memory-based and persistent, and provides API in multiple languages."

"Redis is a high-performance key-value database. The emergence of redis largely compensates for the deficiency of key/value storage such as memcached, and can play a good complementary role to the relational database in some situations. It provides clients such as Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang, which is very convenient to use."

So, what are the advantages of Redis?

Redis supports data persistence

The read and write performance of Redis is excellent.

Rich features: can be used for caching, messages, set expiration time by key, and will be deleted automatically after expiration

Transactions are supported, and all operations of Redis are atomic, while Redis also supports atomic execution of several merged operations.

Redis has rich data structures, such as hash, set, zset, list and so on, in addition to value of string type.

Correspondingly, Redis also has its shortcomings.

Database capacity is limited by physical memory and cannot be used for high-performance reading and writing of massive data, so the scenarios suitable for Redis are mainly limited to high-performance operations and operations with a small amount of data.

Redis does not have automatic fault tolerance and recovery features, and the downtime of the host slave will cause the read and write requests of the frontend to fail. You need to wait for the machine to restart or manually switch the IP of the frontend to recover.

If the host is down and some data can not be synchronized to the slave in time before the downtime, the problem of data inconsistency will be introduced after switching IP, which reduces the availability of the system.

It is difficult for Redis to support online expansion.

Redis is single-threaded, and a single server cannot make full use of the CPU of multi-core servers.

Why should we use Redis?

In terms of high concurrency:

Direct operation of the cache can withstand far more requests than directly accessing the database, so we can consider transferring some of the data in the database to the cache, so that part of the user's requests will go directly to the cache without going through the database.

In terms of high performance:

The user accesses some data in the database for the first time. This process will be slow because it is read from the hard disk. The data accessed by the user is stored in the cache, and the next time the data is accessed, it can be obtained directly from the cache. Operation caching is the direct manipulation of memory, so the speed is quite fast. If the corresponding data in the database changes, synchronously change the corresponding data in the cache

Why use Redis instead of others, such as map or guava that comes with Java

Caching is divided into local cache and distributed cache. Take the map or guava that comes with Java as an example to implement the local cache. The main features are lightweight and fast. The life cycle ends with the destruction of jvm. In the case of multiple instances, each instance needs to keep a cache, and the cache is not consistent.

Using a distributed cache such as redis or memcached, in the case of multiple instances, each instance shares a share of the cached data, and the cache is consistent. The disadvantage is the need to maintain the high availability of redis or memcached services, and the overall program architecture is more complex.

Why is Redis so fast?

Based entirely on memory, the vast majority of requests are purely memory operations, which is very fast. The data is stored in memory

The data structure is simple, and the data operation is also simple.

The use of single thread avoids unnecessary context switching and competition conditions, and there is no CPU consumption caused by multi-process or multi-thread switching.

Non-blocking IO using multi-channel Icano multiplexing model

Thank you for reading! This is the end of this article on "what are the reasons for using Redis for caching". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report