Redis and Memcache are both memory-based data storage systems. Memcached is a high-performance distributed memory cache service, which is essentially an in-memory key-value database. Redis is an open source key-value storage system. Similar to Memcached, Redis stores most data in memory. Supported data types include: strings, hash tables, linked lists, sets, ordered sets, and related operations based on these data types. So, what is the difference between Memcache and Redis?
#1. Different data operations
Unlike Memcached which only supports data records with simple key-value structure, Redis supports The data types are much richer. Memcached basically only supports simple key-value storage, does not support enumeration, persistence and replication and other functions. Redis supports server-side data operations. Compared with Memcached, Redis has more data structures and supports richer data operations. It supports many data structures such as list, set, sorted set, hash, etc., and also provides persistence and replication. Function.
Usually in Memcached, users need to get the data to the client to make similar modifications and then set it back, which greatly increases the number of network IOs and the data volume. In Redis, these complex operations are usually as efficient as regular GET/SET. Therefore, if you need the cache to support more complex structures and operations, Redis will be a better choice.
2. Different memory management mechanisms
In Redis, not all data is always stored in memory. This is the biggest difference compared with Memcached. When physical memory runs out, Redis can swap some values that have not been used for a long time to disk. Redis will only cache all key information. If Redis finds that the memory usage exceeds a certain threshold, it will trigger the swap operation. Redis calculates which keys correspond to the value based on "swappability = age*log(size_in_memory)" swap to disk. Then the values corresponding to these keys are persisted to disk and cleared in memory. This feature allows Redis to maintain data that exceeds the memory size of its machine itself.
Memcached uses the Slab Allocation mechanism by default to manage memory. The main idea is to divide the allocated memory into blocks of a specific length according to the predetermined size to store key-value data records of the corresponding length to completely solve the problem. Memory fragmentation problem.
In terms of memory utilization, if simple key-value storage is used, Memcached's memory utilization is higher. If Redis uses a hash structure for key-value storage, its memory utilization will be higher than Memcached due to its combined compression.
3. Different performance
Since Redis only uses a single core, while Memcached can use multiple cores, on average, Redis has better performance than Memcached when storing small data on each core. higher. For data of more than 100k, the performance of Memcached is higher than that of Redis. Although Redis has also optimized the performance of storing big data, it is still slightly inferior to Memcached.
4. Different cluster management
Memcached is a full-memory data buffering system. Although Redis supports data persistence, full memory is its high performance after all. Nature. As a memory-based storage system, the size of the machine's physical memory is the maximum amount of data that the system can accommodate. If the amount of data that needs to be processed exceeds the physical memory size of a single machine, a distributed cluster needs to be built to expand storage capabilities.
Memcached itself does not support distribution, so Memcached's distributed storage can only be implemented on the client through distributed algorithms such as consistent hashing. Compared with Memcached, which can only use the client to implement distributed storage, Redis prefers to build distributed storage on the server side.
The above is the detailed content of What is the difference between Redis and Memcache?. For more information, please follow other related articles on the PHP Chinese website!