Transferred from: https://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651961272&idx=1&sn= 79ad515b013b0ffc33324db86ba0f834&chksm= Bd2d02648a5a8b728db094312f55574ec521b30e3de8aacf1d2d948a3ac24dbf30e835089fa7&scene=21#wechat_redirect
Memcache and Redis are the most commonly used kv caches in the Internet tiering architecture. Many students in the selection of the time will struggle, in the end is the choice of memcache or Redis.
VoiceOver: Do not encourage rude practices, such as "Memcache provides a subset of the functionality that Redis provides, and without thinking too much, choosing Redis is the right thing to do."
Although Redis is a bit late than memcache and functionally richer, it is probably more important for a technical person to understand why the "why" than "choose who".
When are you inclined to choose Redis?
Business requirements determine the technology selection, when the business has such characteristics, the choice of Redis will be more appropriate.
Complex data structures
When value is a complex data structure such as a hash, a list, a collection, and an ordered collection, Redis is chosen because the MC cannot meet these requirements.
Most typical scenario, user order list, user message, post comment list, etc.
Persistence of
MC does not meet the need for persistence, only Redis is selected.
However, it is to be reminded that the use of the Redis persistence function?
Never use Redis as a database :
(1) Regular snapshots of Redis do not guarantee data loss
(2) Redis aof reduces efficiency and does not support too large data volumes
Do not expect Redis to do a cured storage is better than MySQL, different tools do their own good things, Redis as a database, such a design probably is wrong.
What are the pros and cons of caching the scene and turning on the curing function?
If you just cache the scene, the data is stored in the database, and the Redis exists, if the curing function is turned on:
Advantages Yes, Redis hangs and restarts, memory can quickly recover hot data , not instantaneous pressure on the database, there is no cache preheating process.
Disadvantages Yes, in the process of redis hanging, if there is data modification in the database, the database may not be consistent with Redis data after Redis reboot .
Therefore, read-only scenarios, or allow some inconsistent business scenarios, can try to turn on Redis's curing function.
Naturally High Availability
Redis Natural Support cluster function, can achieve active replication, read and write separation.
Redis official also provides sentinel Cluster management tool, can realize the master-slave service monitoring, automatic fault transfer, all of which is transparent to the client, no program changes, no manual intervention.
In Memcache, to achieve high availability, two development is required, such as a client's dual read-write, or server-side cluster synchronization.
However, it is to be reminded that in most business scenarios, does the cache really need to be highly available?
(1) Caching scenarios, many times, is allowed cache miss
(2) cache hangs, many times can read data through the DB
So, need to carefully analyze the business scenario, high availability, is really the main demand for the cache?
Voice-over: In the Instant messaging business, users ' online presence has high-availability requirements.
Store content is larger
The value of memcache is stored at a maximum of 1M, and if the stored value is large, only redis can be used.
When do you tend to memcache?
Pure kv, the volume of data is very large, concurrency is very large business, using memcache may be more suitable .
This should start from the difference between MC and Redis's underlying implementation mechanism.
Memory allocation
Memcache manages memory using a pre-allocated memory pool , saving memory allocation time.
Redis is a temporary application space that can lead to fragmentation.
From this point, theMC will be faster .
Virtual Memory usage
The Memcache stores all the data in physical memory.
Redis has its own VM mechanism, which theoretically can store more data than physical memory, and when the data is over, it will trigger swap and flash the cold data to disk.
From this point, theMC will be faster when the data is large .
Network model
Memcache uses a non-blocking IO multiplexing model, Redis is also using a non-blocking IO multiplexing model.
However, because Redis also provides some sort of non-KV storage, aggregation functions, when performing these functions, the complex CPU computation, will block the entire IO schedule.
From this point, the MC is faster because Redis provides more functionality .
Threading model
Memcache uses multi-threading, the main thread listens, the worker sub-thread accepts the request, executes the read and write, there may be a lock conflict in the process.
Redis uses a single thread, although there is no lock conflict, but it is difficult to take advantage of multicore features to improve overall throughput.
From this point, theMC will be faster .
Finally, say two.
Code readability, Code quality
Looking at MC and Redis code, in terms of readability, Redis is the most refreshing software I've ever seen, not even one of them, perhaps the original purpose of Redis design, the compilation of Redis even do not need to configure, do not need to rely on third-party libraries, a make is done.
And Memcache, may be considered too much extensibility, multi-system compatibility, code is not refreshing, looks laborious.
For example, the part of the network IO, Redis source code 1-2 files will be done, MC used libevent, a FD passed over, and pipe and thread transmission, especially easy to people around Halo.
VoiceOver: In theory, MC only supports KV, and Redis supports so many functions, MC performance should be much higher, very much, but the actual is not so, it really may be related to code quality.
Support for horizontal Scaling
Either MC or Redis, the server cluster does not have a natural support level extension and needs to be fragmented on the client side, which is actually not friendly to the caller . It would be a bit more perfect if the server cluster could support horizontal scaling.
Related articles:
"Choose Redis or Memcache", what does the interviewer want to examine?
Said a lot, I hope you have a new understanding of Redis and memcache, even a little bit.
Choose Redis or Memcache, the source how to say