Memcache and Redis are the most commonly used KV caches in the layered architecture of the Internet. Many students will struggle with the selection of memcache or Redis.
Voice-over: Rude practices are discouraged, such as “Memcache offers a subset of what Redis offers, so don’t think too much about redis.”
While Redis came later than Memcache, and indeed has more features, it’s probably more important for a tech person to know why than who to choose.
When do you prefer redis?
Business needs determine the technology selection, when the business has such characteristics, redis will be more suitable.
Complex data structure
Redis is used when value is a complex data structure such as hash, list, set, or ordered set, because MC cannot meet these requirements.
Typical scenario, user order list, user message, post comment list, etc.
persistence
The MC could not meet the requirements of persistence and had to choose Redis.
However, the caveat here is, is redis persistence really used correctly?
Never use Redis as a database:
Redis periodic snapshots do not guarantee data loss
Redis’ AOF reduces efficiency and does not support large data volumes
Don’t expect Redis to do a better job of solidified storage than mysql. Different tools do what they are good at and use Redis as a database. This design is probably wrong.
What are the advantages and disadvantages of enabling the curing function in cache scenarios?
In the cache scenario, data is stored in the database and cached in Redis. In this case, if the curing function is enabled:
The advantage is that redis hangs and then restarts, the memory can quickly recover hot data, there is no instantaneous pressure on the database, there is no cache warm up process.
The disadvantage is that when Redis is suspended, if data is modified in the database, the data in the database and redis may be inconsistent after the Redis is restarted.
Therefore, for read-only scenarios, or to allow some inconsistent business scenarios, try turning on redis solidification.
Natural high availability
Redis naturally supports clustering, which enables active replication and read-write separation.
Redis also provides the Sentinel cluster management tool, which enables master/slave service monitoring and automatic failover, all of which are transparent to the client without any program changes or manual intervention.
In order to achieve high availability of memcache, secondary development is required, such as double-read and double-write on the client side, or cluster synchronization on the server side.
However, the caveat here is that for most business scenarios, does caching really need to be highly available?
Cache scenarios, in many cases, allow cache misses
When the cache is down, many times data can be read from DB
So, the business scenario needs to be dissected carefully. Is high availability really a major requirement for caching?
Voice-over: In instant messaging, users have high availability requirements because of their online status.
The contents stored are large
The value storage capacity of memcache can be up to 1 MB. If the value storage capacity is large, you can only use Redis.
When do you prefer memcache?
Pure KV, the amount of data is very large, the concurrency is very large business, memcache may be more suitable.
This starts with the underlying implementation mechanism difference between MC and Redis.
Memory allocation
Memcache uses a pre-allocated memory pool to manage memory, saving memory allocation time.
Redis is a temporary space request that can cause fragmentation.
From this point, the MC will be faster.
Virtual memory usage
Memcache stores all data in physical memory.
Redis has its own VM mechanism, which can theoretically store more data than physical memory. When the data is too much, swap will be triggered, flushing cold data to disk.
From this point, the MC is faster when there is a large amount of data.
A network model
Memcache uses a non-blocking IO reuse model, and Redis uses a non-blocking IO reuse model.
However, because Redis also provides some sorting and aggregation functions other than KV storage, complex CPU calculations will block the whole IO scheduling when performing these functions.
At this point, MCS are faster because redis offers more functionality.
Threading model
Memcache uses multiple threads, the main thread listens, and worker child threads receive requests and perform reads and writes. Lock conflicts may exist during this process.
Redis uses a single thread, although there is no lock conflict, but it is difficult to take advantage of multi-core characteristics to improve overall throughput.
From this point, MC will be faster.
Two final points
- Code readability, code quality
I have seen the code of MC and Redis, in terms of readability, Redis is the most clean software I have ever seen, even none of them. Perhaps simplicity is the original intention of Redis design. Compiling Redis doesn’t even need configure or rely on third-party libraries.
Memcache, on the other hand, is probably thinking too much about scalability, multi-system compatibility, the code is not clean, and it looks like a lot of effort.
For example, the network IO part, redis source code 1-2 files will be done, MC use libevent, a FD pass through the past, and pipe and thread transfer, especially easy to put people around dizzy.
Voiceover: In theory, MC only supports KV, and redis supports so many features, MC performance should be much, much, much higher, but it’s not, really probably due to the quality of the code.
- Horizontal scaling support
Both MC and Redis, the server cluster does not naturally support horizontal scaling and requires sharding on the client side, which is not actually friendly to the caller. It would be nice if the server cluster could support horizontal scaling.
I have specially sorted out the above technologies. There are many technologies that can not be explained clearly by a few words, so I simply recorded some videos with my friends. The answers to many questions are simple, but the thinking and logic behind them are not simple. If you want to learn (Java engineering, high performance and distributed, simple. Micro services, Spring, MyBatis, Netty source code analysis) friends can add my Java architecture group: 725633148, group ali Daniel live explain technology, as well as Java large Internet technology video free to share to you.