Why is Redis more popular than Memcached?

Author: Kaito link: kaito-kidd.com/2020/06/28/redis-vs-memcached/

Preface

We all know that both Redis and Memcached are memory databases, and their access speed is very fast. But in our development process, how do we choose between these two memory databases? What are their advantages and disadvantages?

Why is Redis more popular than Memcached now?

In this article, we will compare the differences between these two in-memory databases from various aspects, so that you can make the choice that best meets your business needs when you use them.

To analyze the difference between them, mainly compare the following aspects:

  • Thread model

  • data structure

  • Elimination strategy

  • Pipelines and affairs

  • Endurance

  • High availability

  • Clustering

Thread model

To talk about performance, we must analyze their service model.

Memcached uses a multithreaded model to process requests, and is based on IO multiplexing technology. After the main thread receives the request, it is distributed to the child threads for processing.

The good advantage of this is that when a request is time-consuming to process, it will not affect the processing of other requests.

Of course, the disadvantage is that CPU multi-thread switching must have performance loss. At the same time, multi-threads must lock when accessing shared resources, which will reduce performance to a certain extent.

Redis also uses IO multiplexing technology, but it uses a single-threaded model for processing requests, from receiving requests to processing data in one thread. I recommend reading this "Is Redis single-threaded or multi-threaded?" " .

This means that with Redis, once a request takes a long time to process, the entire Redis will be blocked until the request is processed and returned before the next request can be processed. When using Redis, you must avoid complex time-consuming operations.

The advantage of single thread is that it reduces the consumption of CPU context switching, and there is no lock competition for multi-threaded access to resources, but the disadvantage is that the performance of CPU multi-core cannot be used.

Since Redis is an in-memory database, its access speed is very fast, so its performance bottleneck is not the CPU, but the memory and network bandwidth. This is the main reason why the author adopts the single-threaded model. At the same time, single thread is very friendly to program development and it is also very convenient to debug. Developing multi-threaded programs will inevitably increase the difficulty of debugging.

Therefore, when our business uses relatively large key data, the access performance of Memcached is better than Redis. If the key data is relatively small, the difference between the two is not big.

Strictly speaking, Redis's single thread refers to the thread that processes the request. It has other threads working, for example, other threads are used to asynchronously process time-consuming tasks.

Redis 6.0 has further improved multi-threading, using multi-threads when receiving requests and sending requests, further improving processing performance.

data structure

Memcached supports a single data structure, only supporting string type operations. And the size limit for value must be less than 1MB, and the expiration time cannot exceed 30 days.

The data structure supported by Redis is very rich. In addition to the commonly used data types string, list, hash, set, and zset, you can also use the geo and hyperLogLog data types.

When using Memcached, we can only serialize the data and write it to Memcached. Then read the data from Memcached, and then deserialize it to the format we need, only "all storage and entire retrieval".

Redis can use different operation methods for different data structures, which is very flexible.

  • list: You can easily build a linked list or use it as a queue

  • Hash: flexibly manipulate the fields we need to perform "all storage and zero access", "zero storage and all access" and "zero storage and zero access"

  • set: construct a non-repeated set, and conveniently perform subtraction and union operations

  • zset: build a leaderboard, or a list with weights

  • geo: used for map-related services to identify the coordinates of two locations and calculate their distance

  • hyperLogLog: Use very little memory to calculate UV

In short, Redis has made great achievements in the field of in-memory databases in recent years because it provides such a rich data structure, which has provided great convenience for our business development. Pay attention to the public number Java technology stack for more detailed usage tutorials of data types.

Elimination strategy

Memcached must set the memory upper limit of the entire instance. When the data reaches the upper limit, the LRU elimination mechanism is triggered, and the data that is not commonly used is eliminated first.

But its data elimination mechanism has some problems: the data just written may be eliminated first, this problem is mainly caused by its own memory management design mechanism.

Redis has no limit. The upper limit of memory must be set. If the memory is used enough, Redis can use enough memory. It is recommended to read "What to do if Redis memory is full"

At the same time, Redis provides a variety of elimination strategies:

  • Volatile-lru: Eliminate the expired key according to the LRU mechanism

  • allkeys-lru: Eliminate all keys according to the LRU mechanism

  • volatile-random: randomly eliminate keys from expired keys

  • allkeys-random: randomly eliminate keys from all keys

  • volatile-ttl: Prioritize the elimination of the key that will expire recently

  • volatile-lfu: Eliminate all keys according to the LFU mechanism

  • allkeys-lfu: Eliminate the expired keys according to the LFU mechanism

We can use different data elimination strategies for business scenarios.

Pipelines and affairs

Redis also supports the pipeline function. The client sends multiple commands to the server in one package, and the server processes the commands sent by the client in turn. This can reduce the number of network IOs going back and forth and provide high access performance.

In addition, it also supports transactions. The transaction mentioned here is not a strict transaction model like MySQL, which is unique to Redis.

General transactions will be used in conjunction with the pipeline. The client sends multiple commands to the server in one package, and identifies that these commands must be executed in strict order and cannot be interrupted by other clients. Before executing the transaction at the same time, the client can tell the server that a key will perform related operations later. If the client changes the key before operating the key, then the current client is executing these commands Will give up the entire transaction operation to ensure consistency.

Endurance

Memcached does not support data persistence. If the Memcached service goes down, all data on this node will be lost.

Redis supports persisting data on disk, and provides two methods: RDB and AOF:

  • RDB: Snapshot the data in the entire instance to disk, and make full persistence

  • AOF: Persist every write command to disk, incrementally persist

Redis uses these two methods to cooperate with each other to complete data integrity protection and minimize data loss caused by service downtime.

High availability

Memcached does not have a master-slave replication architecture and can only be deployed on a single node. If the node goes down, all data on that node will be lost. The business needs to be compatible with this situation. When a node is unavailable, data is written to other nodes to reduce the impact on the business.

Redis has a master-slave replication architecture. Two nodes form a master-slave architecture. The slave can synchronize the master's data in real time and improve the availability of the entire Redis service.

At the same time, Redis also provides sentinel nodes. When the master node is down, the slave node is actively promoted to the master node and continues to provide services. How to integrate Redis Sentry with Spring Boot and other series of tutorials can be read by following the public account Java technology stack search.

The two nodes of master and slave can also provide read-write separation function to further improve the performance of program access.

Clustering

Both Memcached and Redis consist of multiple nodes forming a cluster to provide external services, but their mechanisms are also different.

The clustering of Memcached uses a consistent hash algorithm to send data to a specified node on the client side. When a node goes down, other nodes will share the request of this node.

In Redis clustering, each node maintains a part of the virtual slot. Through the hash calculation of the key, the key is mapped to a specific virtual slot, and this slot is then mapped to a specific Redis node.

At the same time, each Redis node contains at least one slave node to form a master-slave architecture, further improving the high availability capability of each node.

When adding or offline nodes, you need to manually trigger the data migration and re-map the hash slot.

The official Redis clustering solution is Redis cluster, which uses a decentralized design. In addition, there are also third-party clustering solutions that use a centralized design proxy method, such as Codis and Twemproxy.

to sum up

A comparative analysis from the above aspects is summarized in the following table.

On the whole, Redis provides a very rich function, and the performance is basically the same as that of Memcached, which is why it has taken the lead in memory databases in recent years.

If your business requires support from various data structures and requires high availability of data, then Redis is more appropriate.

If your business is very simple, just simple set/get, and the memory usage is not high, then simple Memcached is sufficient.

If this article can bring you a small increase in work efficiency, you may wish to read it and forward it to encourage me to write a better article!

Recommended recent hot articles:

1. 6 ways to get IntelliJ IDEA activation code for free!

2. I used Java 8 to write a piece of logic, and my colleagues couldn't understand it directly. You can try it. .

3. Sling Tomcat, Undertow performance is very explosive! !

4. The Chinese open sourced a super easy-to-use Redis client, which is so fragrant! !

5. The latest release of "Java Development Manual (Songshan Edition)", download it quickly!

Feel good, don't forget to like + forward!

Feel good, don’t forget to like + forward!

Finally, pay attention to the WeChat official account of the stack leader: Java technology stack, reply: Welfare, you can get a free copy of the latest Java interview questions I have compiled for 2020. It is really complete (including answers), without any routine.

Guess you like

Origin blog.csdn.net/youanyyou/article/details/108526701