Comparison between redis and memcached)

Source: Internet
Author: User

1. Network I/O model

Memcached is a multi-thread, non-blocking I/O multiplexing network model. It is divided into the listening main line and the worker subthread. The listening thread listens to network connections. After receiving the request, it passes the connection description pipe to the worker thread, for read/write Io, the network layer uses the libevent encapsulated event library. The multi-threaded model can play a multi-core role, but introduces the cache coherency and lock issues, for example, memcached's most commonly used stats command, all actual memcached operations require locking and counting the global variable, resulting in performance loss.

(Memcached network I/O model)

Redis uses a single-thread Io multiplexing model and encapsulates a simple aeevent event processing framework, which mainly implements epoll, kqueue, and select. For Io operations alone, A single thread can maximize the speed advantage, but redis also provides some simple computing functions, such as sorting and aggregation. For these operations, the single-threaded model will seriously affect the overall throughput. During CPU computing, the entire Io scheduling is blocked.

2. Memory Management

Memcached uses a pre-allocated memory pool. It uses slab and chunks of different sizes to manage the memory. Item selects the appropriate chunk storage based on the size, the memory pool method can save the overhead of applying for/releasing memory and reduce the generation of memory fragments. However, this method also results in a certain amount of space waste, in addition, new data may be removed when the memory is still large. For the reason, refer to timyang'sArticle: Http://timyang.net/data/Memcached-lru-evictions/

Redis uses on-site memory application to store data, and seldom uses free-list or other methods to optimize memory allocation. To some extent, memory fragments exist. redis and data storage Command Parameters, data with an expiration time is stored separately and called temporary data. Non-temporary data will never be deleted, even if the physical memory is insufficient, as a result, swap will not remove any non-temporary data (but will try to remove some temporary data). redis is more suitable for storage than cache.

3. Data Consistency

Memcached provides CAS commands to ensure the consistency of multiple concurrent accesses to the same data copy. Redis does not provide CAS commands and cannot guarantee this. However, redis provides the transaction function to ensure the atomicity of a string of commands without any operation interruption.

4. Storage Methods and other aspects

Memcached supports only simple key-value storage, enumeration, persistence, replication, and other functions.

In addition to key/value, redis also supports multiple data structures such as list, set, sorted set, and hash, providing keys

Enumeration, but not online. If you need to enumerate online data, redis provides a tool to directly scan its dump file and enumerate all the data, redis also provides persistence, replication, and other functions.

5. Client support in different languages

For clients in different languages, memcached and redis both have a wide range of third-party clients to choose from. However, because memcached has been developing for a longer time, it is currently supported by clients, many clients of memcached are more mature and stable, while redis is more complicated than memcached because of its Protocol itself. In addition, due to the author's continuous addition of new features, the follow-up speed of the corresponding third-party clients may not be able to catch up, sometimes you may need to make some modifications on the basis of a third-party client for better use.

It is not difficult to see from the above that when we do not want data to be kicked out, or need more data types except key/value, or need to implement the function, using redis is more suitable than using memcached.

Some peripheral functions of redis

In addition to storage, redis also provides some other functions, such as aggregate computing, pubsub, and scripting. For such functions, you need to understand their implementation principles, you can use pubsub correctly only after you understand its limitations. For example, the pubsub function does not support persistence, all messages sent from the consumer's transient disconnection or reconnection will be lost. For example, functions such as aggregate computing and scripting are limited by the redis single-thread model, it is impossible to achieve a high throughput, so use it with caution.

In general, the author of redis is a very diligent developer. We can often see that the author is trying various new ideas and ideas, to address these features, we need to have a deep understanding before using them.

Summary:

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

2. redis is used as a replacement for memcached in more scenarios.

3. When more data types except key/value are required, it is more appropriate to use redis.

4. When the stored data cannot be removed, it is more appropriate to use redis.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.