Interview Question: Comparison of Caching Redis and Memcached

 Memcached is a high-performance distributed in-memory object caching system for dynamic web applications to reduce database load. It provides the speed of dynamic, database-driven websites by caching data and objects in memory to reduce the number of database reads.
 

   Memcached is based on a hashmap that stores key/value pairs .
   
    Redis is a key-value storage system , similar to Memcached. But it supports relatively more value types for storage, including string (string), list (linked list), set (collection), zset (sorted set -- ordered collection) and hashs (hash type) . These data types all support push/pop, add/remove, intersection union and difference, and richer operations, and these operations are atomic . On this basis, redis supports sorting in various ways. Like memcached , data is cached in memory to ensure efficiency. The difference is that redis will periodically write updated data to disk or write modification operations to additional record files, and on this basis achieve master-slave (master-slave) synchronization.
    Redis is a high-performance key-value database. The emergence of redis has largely compensated for the insufficiency of key/value storage such as memcached, and can be a good complement to relational databases in some occasions. It provides Python, Ruby, Erlang, and PHP clients, which are very convenient to use.
   
    The following mainly introduces the difference between Redis and Memcached.

  1. Network IO model

    Memcached is a multi-threaded, non-blocking IO multiplexing network model. It is divided into monitoring main thread and worker sub-thread. The monitoring thread listens for network connections. After accepting the request, it passes the connection description word pipe to the worker thread for reading and writing IO, network The layer uses the event library encapsulated by libevent. The multi-threading model can play a multi-core role, but it introduces the problem of cache coherency and lock. For example, the most commonly used stats command of Memcached, in fact, all operations of Memcached must lock this global variable and count it. And so on, it brings performance loss.

    Redis uses a single-threaded IO multiplexing model and encapsulates a simple AeEvent event processing framework, which mainly implements epoll, kqueue and select. For only IO operations, single-threaded can maximize the speed advantage, but Redis It also provides some simple computing functions, such as sorting, aggregation, etc. For these operations, the single-threaded model will actually seriously affect the overall throughput. During the CPU computing process, the entire IO scheduling is blocked.

  2. Memory management

    Memcached uses a pre-allocated memory pool, uses slabs and chunks of different sizes to manage memory, and Item selects the appropriate chunk storage according to the size. The memory pool method can save the overhead of applying/freeing memory and reduce memory fragmentation. However, this method will also lead to a certain degree of space waste, and when there is still a lot of space in the memory, new data may also be eliminated, the reason can refer to Timyang's article: http://timyang.net /data/Memcached-lru-evictions/

    Redis uses the method of on-site memory application to store data, and rarely uses free-list and other methods to optimize memory allocation, and there will be memory fragmentation to a certain extent. Redis will store data with expiration time separately according to the storage command parameters. Together, and call them temporary data, non-temporary data will never be culled, even if the physical memory is not enough, resulting in swap will not cull any non-temporary data (but will try to cull some temporary data), this point Redis is more suitable as a storage rather than a cache.

  3. Data Consistency Issues

    Memcached provides the cas command, which can ensure the consistency of multiple concurrent access operations on the same data. Redis does not provide the cas command, and cannot guarantee this, but Redis provides the function of transaction, which can ensure the atomicity of a series of commands, and will not be interrupted by any operation in the middle.

  4. Storage method and other aspects

    Memcached basically only supports simple key-value storage, does not support enumeration, does not support functions such as persistence and replication

    In addition to key/value, Redis also supports list, set, sorted set, hash and many other data structures, and provides KEYS

  Perform enumeration operations, but cannot be used online. If you need to enumerate online data, Redis provides tools to scan its dump files directly and enumerate all data. Redis also provides functions such as persistence and replication.

  5. Client support for different languages

  In terms of clients in different languages, Memcached and Redis have a wealth of third-party clients to choose from. However, because Memcached has been developed for a longer time, in terms of client support, many of Memcached's clients are more mature and stable. Redis, because its protocol itself is more complex than Memcached, and the author keeps adding new functions, etc., the follow-up speed of corresponding third-party clients may not be able to keep up, and sometimes you may need to make some modifications on the basis of third-party clients to be better. usage of.

  According to the above comparison, it is not difficult to see that when we do not want the data to be kicked out, or when we need more data types than key/value, or when we need the landing function, it is more suitable to use Redis than to use Memcached.

  About some peripheral functions of Redis

    In addition to being used as storage, Redis also provides some other functions, such as aggregate computing, pubsub, scripting, etc. For such functions, it is necessary to understand the implementation principle and clearly understand its limitations before it can be used correctly, such as pubsub function, this actually does not have any persistence support, the messages from the consumer's connection flash or reconnect will be lost, and functions such as aggregate computing and scripting are limited by the Redis single-threaded model. It is possible to achieve very high throughput and needs to be used with caution.

  In general, the author of Redis is a very diligent developer. You can often see that the author is trying various new ideas and ideas. The functions of these aspects require us to have a deep understanding before using them.

  Summarize:

  1. The best way to use Redis is to use all data in-memory.

  2. More scenarios of Redis are used as a replacement for Memcached.

  3. When more data type support than key/value is required, Redis is more suitable.

  4. When the stored data cannot be eliminated, it is more appropriate to use Redis.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325526911&siteId=291194637