This article extensively references the difference between Redis and memcached.
If you simply compare the differences between Redis and memcached, most of them will get the following ideas:
- Redis not only supports simple k/v types of data, but also provides storage of data structures such as List,set,zset,hash.
- Redis supports backup of data, that is, Master-slave mode of data backup.
- Redis supports data persistence, which keeps the in-memory data on disk and can be loaded again for use when restarting.
Aside from this, you can delve into the internal structure of Redis to see more essential differences and understand the design of Redis.
Network IO Model memcached network IO Model
Memcached is a multi-threaded, non-blocking IO multiplexing network model, divided into the main thread and the worker sub-thread, listening thread listening network connection, after accepting the request, the connection description Word pipe to the worker thread, read and write Io, The network layer uses Libevent encapsulated event Library, multithreaded model can play multi-core role, but introduced the cache coherency and lock problem, for example, memcached most commonly used stats command, the actual memcached all operations to the global variable lock, such as counting and other tasks, resulting in a performance loss.
Redis Network IO Model
Redis uses a single-threaded IO multiplexing model, which encapsulates a simple Aeevent event processing framework that implements Epoll, Kqueue, and select, which can be used to maximize the speed advantage for purely IO operations, but Redis also provides a number of simple computing functions, such as sorting, aggregation, etc., for these operations, the single-threaded model will actually seriously affect the overall throughput, CPU calculation process, the entire IO schedule is blocked .
Memory management aspects
- Memcached uses a pre-allocated pool of memory to manage memory using slab and chunk of different sizes, item selects the appropriate chunk storage based on size, the way memory pools can save the cost of requesting/freeing memory, and can reduce memory fragmentation, But this approach also leads to a certain amount of wasted space, and when memory is still large, new data may be rejected for reference to Timyang's article: http://timyang.net/data/Memcached-lru-evictions/
- Redis uses on-site memory storage to store data, and rarely uses free-list to optimize memory allocation, and there is a degree of memory fragmentation, and the Redis data store command parameters, which store the time-to-date information separately, and call them temporary data. Non-temporary data is never removed, even if there is not enough physical memory, so that swap will not eliminate any non-temporal data (but will attempt to eliminate some temporary data), which is more appropriate for Redis as storage instead of the cache.
Data consistency issues
- Memcached provides a CAS command that guarantees consistency of the same data for multiple concurrent access operations.
- Redis does not provide CAS commands, and this is not guaranteed, but Redis provides the functionality of a transaction that guarantees the atomicity of a sequence of commands and is not interrupted by any action.
Differences in cluster Management
Memcached is a full-memory data buffering system, although Redis supports data persistence, but full memory is the essence of its high performance. As a memory-based storage system, the size of the machine's physical memory is the maximum amount of data the system can hold. If the amount of data that needs to be processed exceeds the physical memory size of a single machine, it is necessary to build a distributed cluster to extend the storage capacity.
memcached itself does not support distribution , so it is possible to implement memcached distributed storage only at the client through a distributed algorithm such as a consistent hash. A distributed storage implementation architecture for Memcached is presented. Before the client sends data to the memcached cluster, the target node of the data is computed first through the built-in distributed algorithm, and then the data is sent directly to the node for storage. However, when the client queries the data, it also calculates the node where the query data resides, and then sends a query request directly to the node to get the data.
Redis is more inclined to build distributed storage on the server side than memcached can only implement distributed storage with clients. The latest version of Redis already supports distributed storage capabilities. Redis cluster is an advanced version of Redis that implements distributed and allows single points of failure, with no central node and linear scalability. The distributed storage architecture of Redis cluster is given, in which the nodes communicate with the nodes through the binary protocol, and the communication between the node and the client is through the ASCII protocol. On the data placement strategy, Redis cluster divides the numeric field of the entire key into 4,096 hash slots, each of which can store one or more hash slots, which means that the maximum number of nodes currently supported by Redis cluster is 4096. The distributed algorithm used by Redis cluster is also simple: crc16( key ) %HASH_SLOTS_NUMBER
.
To ensure data availability under a single point of failure, Redis cluster introduces master and slave nodes. In Redis cluster, each master node will have two corresponding slave nodes for redundancy. This way, any two-node outage in the entire cluster does not result in data unavailability. When the master node exits, the cluster automatically selects a slave node to become the new master node.
Storage methods and other aspects
- Memcached basically only supports simple key-value storage, does not support enumeration, and does not support persistence and replication functions.
- In addition to Key/value, Redis also supports many data structures such as list,set,sorted Set,hash, which provides the keys for enumeration operations, but cannot be used online, and if you need to enumerate the data on line, Redis provides tools to scan its dump files directly. All data, Redis also provides features such as persistence and replication.
according to the above comparisons it is not difficult to see that when we do not want the data to be kicked out, or need more data types other than Key/value, or need to use the landing function, using Redis is more appropriate than using memcached .
Summary :
1. The best way to use Redis is to in-memory all data.
2. Redis more scenarios are used as substitutes for memcached.
3. It is more appropriate to use Redis when more data type support is required other than key/value.
4. Using Redis is more appropriate when the stored data cannot be excluded.
5. When a distributed deployment is required.
reference Documentation :
- The difference between Redis and memcached
- The difference between Redis and memcached
Redis and memcached differences