Why use Redis for caching

Introduction to Redis

Simply put, redis is a database, but unlike traditional databases, redis data is stored in memory, so the read and write speed is very fast, so redis is widely used in the cache direction. In addition, redis is often used for distributed locks. Redis provides a variety of data types to support different business scenarios. In addition, redis supports transactions, persistence, LUA scripts, LRU-driven events, and multiple cluster solutions.

Why do you need Redis for caching?

This problem is mainly viewed from two points: "high performance" and "high concurrency".

high performance:

Suppose the user accesses some data in the database for the first time. This process will be slower because it is read from the hard disk. The data accessed by the user is stored in the data cache, so that the data can be directly obtained from the cache when the data is accessed next time. Operating the cache is to directly manipulate the memory, so the speed is quite fast. If the corresponding data in the database is changed, the corresponding data in the cache can be changed synchronously!

High concurrency:

Direct operation of the cache can withstand requests far greater than direct access to the database, so we can consider transferring part 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.

Why use redis instead of map/guava for caching?

 Cache is divided into local cache and distributed cache. Taking Java as an example, using the built-in map or guava to achieve local caching, the main feature is light weight and fast speed, the life cycle ends with the destruction of the JVM, and in the case of multiple instances, each instance is Need to keep a copy of the cache, the cache is not consistent.

Using redis or memcached is called distributed cache. In the case of multiple instances, each instance shares a copy of 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 complicated.

Guess you like

Origin blog.csdn.net/crossroads10/article/details/100547916
Recommended