The preface

Many people like to compare Memcached to Redis, and with that in mind, I’d like to share some of my own insights. First of all, before discussing something, we must have a basic understanding of them to provide us with a good theoretical basis:

Memcached: a fully open source, high-performance, distributed memory system;

Redis: an open source, key-value, memory-based NoSQL database that supports persistence;

Conceptually, Memcached and Redis are not identical twins. There is only one connection between Redis and Memcached: memory. From the beginning, the goals of Memcached and Memcached were very different. Memcached was a high-performance memory service. Redis pursues not only memory operation, but also data persistence. In that sense,

Memcached is more focused and efficient, whereas Redis is more morally, intellectually and physically holistic.

Below, we can take a look at a picture, let’s understand them more intuitively:

If this seems a bit complicated, it doesn’t matter. After discarding MySQL and MongoDB, try again:



Now that you have a good idea of the difference between Redis and Memcached, let’s move on to the next stop.

Comparison between network I/OS

Memcached is a multithreaded, non-blocking IO reuse network model

Redis uses a single-threaded IO reuse model

Based on the above content can be summarized as follows:

The Memcached performance of multithreaded non-blocking IO is even better under the pressure of high concurrency scenarios.

Memory management mechanism

Memcached and Redis are both C implementations, but both have abandoned the malloc/free function in favor of implementing the memory model themselves. The Memcached memory mode is officially defined as “Slab Allocation”. The flowchart is as follows:

Chunk is the smallest unit Memcached uses to store data. It’s like a box with our lunch in it. “Memcache” is designed to minimize the problem of slab and page fragments. “stick” and “page” are larger boxes used to hold chunks of different sizes. The size of chunks is determined by Factor. 3. Chunks of different sizes are eventually managed in a directory for easy access, called slab_class. Data access process: The client will find Slab of appropriate size in the slab_class and find Chunk in a certain way to ensure that data will enter a more appropriate “box”, thus reducing the waste of memory.

The processing of Redis in this aspect is relatively simple, and the general form is as follows:



Redis allocates each data block according to its data type and size. The metadata of this data block (such as the size of the data block) is stored in the head of the memory block. Real_ptr is the pointer returned by Redis when malloc is called. Redis stores the size of the memory block in the header. Size occupies a known size of memory, which is of type size_t, and returns ret_ptr. When memory needs to be freed, reT_ptr is passed to the memory manager. With ret_ptr, the program can easily calculate the value of real_ptr and pass real_pTR to free memory.

In general: Memcached uses pre-allocated memory pools to manage memory using slabs and chunks of different sizes. Items are stored in appropriate chunks according to their sizes. Memory pools save the cost of applying for or releasing memory and reduce the generation of memory fragments

Redis use field application memory to store data, and rarely use the free – the list to optimizing the allocation of memory, will exist in a certain extent, memory fragments, Redis command parameters according to storage, the data with expiration time alone in together, and call them temporary data, is a temporary data will never be eliminated, Even if there is not enough physical memory, swap will not discard any non-temporary data (although it will try to discard some temporary data), so Redis is more suitable for storage than cache.

Based on the above two aspects, it can be summarized as follows:

1. Redis uses more detailed memory space than Memcahced. Memcached is used to carry data in a “box”, no matter how big the box is, it will inevitably be empty.

2. Memcached is the perfect solution to memory fragmentation.

3. Memcached also has a slot mechanism, which gives priority to memory reuse by using discarded memory.

4. Redis does not store all the data in memory, but only all the keys in memory. There will be a certain probability of an IO operation when reading.

Data consistency assurance

Redis provides a concept of “transaction”, although this is a fake transaction, because Redis is a single process operation, so Redis transaction is only a set of operations in order to operate, without inserting any other commands in between, so as to ensure data consistency, but this way can easily cause operation blocking. Memcached provides cas operations similar to optimistic locking, which quickly returns success or failure without affecting other data operations.

In this regard, Memcached is faster and more secure than Redis.

The cluster form

Memcached itself does not support clustering. All clustering is done on the client side. This is the famous Memcached Hash.



As shown in the picture:

1. The application first invokes a Memcached client (such as the famous Java client XMemcached). The Memcached client selects the Memcached node using the Hash algorithm.

2. To the user, all distributed nodes are transparent and invisible, and the whole cluster is operated as if it were a node.

Redis is more “normal” than Memcached, with no central node and linearly scalable functionality. Distributed storage architecture of Redis Cluster, nodes communicate with each other through binary protocol, and nodes communicate with clients through ASCII protocol. In terms of data placement strategy, Redis Cluster divides the number value field of the whole key into 4096 hash slots, and each node can store one or more slots.

There are a few things to note: 1. From a purely clustering perspective, Redis seems better because Memcached’s distribution is client-side, and different clients can produce completely different results, such as a C client and a Java client. 2, Redis cluster, there is also a big problem, that is, slot transfer, if there is a damaged node Redis, the need to migrate out of the data slot will be a disaster level of existence.

BUT, the scariest of all, in an existing distributed solution, provides a Twemproxy middleware that solves both of these problems well, so Redis and Memcached are equal in this regard.

conclusion

For those of you who don’t want to read a long story, and for those who have already seen it, this is our key point: 1. Memcached is a priority if you don’t want persistence to be fast and efficient.

Tomcat cluster deployment:

The first advantage is that the Tomcat cluster aims to unify Session access, which in most cases does not require persistence, so you can sacrifice persistence for better performance. Another advantage is that the length of user sessions is within a range in most cases, so we can control the Chunk size by modifying the factor self-growth factor to minimize memory waste. If interested in this part of content, you can refer to: coding.imooc.com/class/186.h…

Real-time configuration and data without persistence requirements

In real work scenarios, we often have the need to warm up the server configuration and want to modify the configuration information online to achieve different effects, such as: Functional switches (Fuse switches for Hystrix, time-limited switches for popular items, etc.), zero-configuration switching between test/preproduction/production environments for software systems that require real-time reading but don’t have persistence requirements are good for Memcached.

Storing JSON strings

In the last year or two, many kids like to store an object as a JSON string, which is Memcached more efficiently than Redis. Or to put it another way, Redis String is a miniature version of Memcached, so Memcached does everything Redis String does better.

2. In the case of pursuing multi-type support and relatively high persistence requirements, Redis is preferred; Redis is supported by multiple data types that Memcahced can’t match, and these types can turn rotten into magic in many cases, greatly reducing the redundant code of the business. As a NoSQL database, Redis provides two persistence mechanisms to ensure business continuity. In this case, Redis is dead to Memcached. Third, Redis has supported programmatic reading and writing of data such as Lua scripts since version 3.1, which greatly improves the scalability of Redis. The following are common Redis application scenarios:

***- Fetch the latest N data operation, such as: can put the latest 50 comment ID in the List collection

  • Ranking applications, take the TOP N operation, the previous operation is weighted by time, this is weighted by a certain condition, such as sorting counter applications by the TOP number of times
  • Store relationships: such as social relationships, such as tags, etc
  • To get the values of all data for a period of time, use set, such as the user ID or client IP that was accessed for a period of time
  • To build a queue system, List can build stacks and queues, and zset can build priority queues

And finally, if you think the above example is too complicated, here’s the ultimate secret:

You can use Memcached for everything you used to do with Redis strings in exchange for better performance. In addition, Redis is preferred;