Home > Article > Backend Development > What is the difference between Redis and Memcached?
Redis also has key-value pair storage, which can also be stored in memory, and also supports persistent storage. Moreover, redis clusters, distributed deployment, and mirror synchronization are all natively supported, which is more convenient than memcached (memcached requires its own It is very troublesome to write a consistent hash algorithm to determine which memcached node the value corresponding to a certain key is stored on.) So what is the use of memcached?
Why not just use redis instead of memcache?
(I think there are still many PHP tutorials, the textbooks all talk about memcached, and there are still relatively few mentions of redis. I only found out about such a thing by reading other people's blogs.)
Redis also has key-value pair storage, which can also be stored in memory, and also supports persistent storage. Moreover, redis clusters, distributed deployment, and mirror synchronization are all natively supported, which is more convenient than memcached (memcached requires its own It is very troublesome to write a consistent hash algorithm to determine which memcached node the value corresponding to a certain key is stored on.) So what is the use of memcached?
Why not just use redis instead of memcache?
(I think there are still many PHP tutorials, the textbooks all talk about memcached, and there are still relatively few mentions of redis. I only found out about such a thing by reading other people's blogs.)
It depends!
Memcached
is a multi-threaded non-blocking IO reuse network model; Redis
uses a single-threaded IO reuse model
Memcached
Use a pre-allocated memory pool; Redis
Use on-site memory application to store data
Memcached
are completely independent of each other; Redis
plans to build support for clustering into the server
Memcached
Can use multiple cores; Redis
Only single core
In short, if you use simple key-value
storage, Memcached
has higher memory utilization. When you need support for more data types other than key/value
, it is more appropriate to use Redis
. Hope it helps you
Redis is replacing Memcached, but you have to know that Redis is a new thing. For a huge system, it will take time to completely replace it.
Currently Weibo uses Redis cluster.
Thanks for the invitation~~
I was out and about without internet connection before. Now I will give you some of my personal opinions and some reference materials. If you find it useful, please take it. If you make any mistakes, you are welcome to support me. If you think it is good, please give it a thumbs up~~~
First of all, let me explain a point: existence is reasonable, and some people use it to prove that it has its own value.
Memcached and redis are very similar: they are both in-memory databases. Data is stored in memory and directly accessed through tcp. The advantages are fast speed and high concurrency. The disadvantages are limited data types and weak query functions. They are generally used for cache.
So the questioner said that redis can do all the things memcached does, so why do people still use memcached? That’s because the two of them are not completely interchangeable. They also have their own strengths and weaknesses:
Advantages of Memcached:
Memcached can take advantage of multi-core, and the throughput of a single instance is extremely high, which can reach hundreds of thousands of QPS (depending on the byte size of key and value and the performance of the server hardware, the peak QPS in daily environment is about 4-6w about).
Suitable for maximum load capacity and effectively decompressing the server.
Supports direct configuration as session handle.
There are relatively few pitfalls in configuration and maintenance.
Limitations of Memcached:
The data structure is very simple and single, and only supports simple key/value data structures, unlike Redis which can support rich data types.
Unable to be persisted, data cannot be backed up and can only be used for caching, and all data will be lost after restarting.
Data synchronization cannot be performed, and data in MC cannot be migrated to other MC instances.
Memcached memory allocation uses the Slab
Allocation mechanism to manage memory. Large differences in value size distribution will cause memory utilization to decrease, and cause problems such as kicking out even when utilization is low. Users need to pay attention to value design.
The memcached server does not natively support horizontal expansion. A cache distribution strategy must be written on the client to implement distributed caching. Since data synchronization cannot be performed, a single machine failure in the production environment may affect some business operations. .
Advantages of Redis:
supports a variety of data structures, such as string (string),
list (double linked list), dict (hash table), set (set), zset (sorted set), hyperloglog (cardinality estimation) etc.
Supports persistence operations, and can persist aof and rdb data to disk for data backup or data recovery, which is a better way to prevent data loss.
Supports data replication through Replication. Through the master-slave mechanism, data can be replicated synchronously in real time. It supports multi-level replication and incremental replication. The master-slave mechanism is an important means for Redis to perform HA.
Single-threaded request, all commands are executed serially, and there is no need to consider data consistency issues in concurrent situations.
Supports pub/sub message subscription mechanism, which can be used for message subscription and notification.
Supports simple transaction requirements, but there are few use cases in the industry and it is not mature.
Limitations of Redis:
Redis can only use a single thread, and its performance is limited by CPU performance. Therefore, a single instance CPU can only reach a maximum of 5-6wQPS per second (depending on the data structure, data size and server hardware performance. QPS in daily environment The peak is around 1-2w).
Supports simple transaction requirements, but there are few usage scenarios in the industry and it is not mature, which has both advantages and disadvantages.
Redis consumes more memory for string types. You can use dict (hash table) to compress storage to reduce memory consumption.
In my opinion, Redis has the characteristics of a database in many aspects, or it is a database system, while Memcached is just a simple K/V cache.
And whether to use redis or memcached depends on the needs of the subject, because if it is just caching, memcached is enough to meet most needs. The emergence of redis only provides a better choice, but it does not mean that Redis can completely replace memcached. Again, it depends on your needs.
In terms of technology, redis is still newer than memcached, but when people mature, they say that memcached should be better. Moreover, the trend is now starting to switch to mongodb. Because of the database characteristics of redis, mongodb is superior.
Memcached still accounts for the majority of the caching strategies of many companies, followed by redis, and finally mongodb. I found that there is no such thing. It still takes time for the latest technology to be applied by the company team, and there are still many old technical strategies. The company team is using it because the technology is mature and more stable. This is why memcached is mentioned more than redis.
Look at demand, look at mastery
In fact, the performance of the two is similar, but the performance of redis is slightly better.
Redis replacing memcached is the trend.