Interview: Comparing cached Redis with memcached

Source: Internet
Author: User
Tags cas memcached

memcached is a high-performance distributed memory object caching system for dynamic Web applications to offload database load. It provides a dynamic, database-driven site speed by caching data and objects in memory to reduce the number of times the database is read .

Memcached based on a HashMap for storing key/value pairs

Redis is a a Key-value storage System, similar to memcached. But it supports storing more of the value type, includes string (string), list (linked list), set (set), Zset (sorted set-ordered set), and HASHS (hash type)These data types support Push/pop, Add/remove, and intersection sets and differences and richer operations, and these operations are atomic。 Based on this, Redis supports sorting in a variety of different ways. As with memcached, to ensure efficiency, the data is cached in memory. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.
Redis is a high-performance Key-value database. The emergence of Redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational database. It provides the python,ruby,erlang,php client, which is very convenient to use.

Here's a look at the difference between Redis and memcached.

1. 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/write IO, the network layer using the Libevent encapsulated event Library , multithreading model can play a multi-core role, but the introduction of the cache coherency and lock problem, for example, memcached most commonly used stats command, the actual memcached all operations to the global variable lock, count, etc., resulting in performance loss.

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. However, Redis also provides some simple computing functions, such as sorting, aggregation, etc., for these operations, the single-threaded model can actually seriously affect the overall throughput, CPU calculation process, the entire IO schedule is blocked.

2. 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.

3. 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.

4. 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 supports numerous data structures such as list,set,sorted Set,hash, which provides the keys

enumeration, but not on-line, if you need to enumerate online data, Redis provides tools to scan its dump files, enumerate all the data, and Redis also provides the functionality of persistence and replication.

5. Client support for different languages

Memcached and Redis have rich third-party clients to choose from for different language clients, but because memcached has been developing for a longer period of time, many of Memcached's clients are more mature and stable in terms of client support at present. and Redis because its protocol itself is more complex than memcached, plus the author constantly add new features, and so on, the corresponding third-party client tracking speed may not catch up, sometimes you may need to make some changes on the basis of third-party clients to better use.

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.

about the Some of the perimeter features of Redis

In addition to being stored as storage, Redis also provides some other functions, such as aggregation calculation, pubsub, scripting, etc., for such functions need to understand its implementation principle, clearly understand its limitations, can be used correctly, such as pubsub function, This is actually not supported by any persistence, the consumer connection between the flash or the reconnection between the message is all lost, and such as aggregation calculation and scripting and other features are limited by the Redis single-threaded model, it is impossible to achieve high throughput, need to use caution.

In general, the Redis author is a very diligent developer who can often see that the author is experimenting with a variety of new ideas and ideas, and that the functionality of these areas requires that we need to know more about them before using them.

Summarize:

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

2.Redis more scenes 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.

From the following several dimensions, the Redis, Memcache, MongoDB made a comparison, welcome to shoot Bricks

1. Performance

are relatively high, performance is not a bottleneck for us

In general, the TPS is about the same as Redis and Memcache, more than MongoDB

2, the convenience of the operation

Memcache Data Structure Single

Redis is rich, data manipulation, Redis better, less network IO times

MongoDB supports rich data expression, index, most similar relational database, support query language is very rich

3. Size of memory space and amount of data

Redis has added its own VM features after the 2.0 release, breaking the limits of physical memory; You can set the expiration time for key value (similar to memcache)

Memcache can modify the maximum available memory, using the LRU algorithm

MongoDB is suitable for large data storage, depends on operating system VM to do memory management, eat memory is also very bad, service not with other services together

4. Availability (single point of issue)

For a single point of problem,

Redis, which relies on clients for distributed reads and writes, and master-slave replication relies on the entire snapshot every time the primary node is reconnected from the node, without incremental replication, due to performance and efficiency issues,

Therefore, the single point problem is more complicated, the automatic sharding is not supported, and the dependent program is required to set the consistent hash mechanism.

An alternative is to use your own proactive replication (multiple storage) instead of Redis's own replication mechanism, or change to incremental replication (you need to implement it yourself), consistency issues and performance tradeoffs

Memcache itself has no data redundancy mechanism, it is not necessary, for fault prevention, relying on mature hash or ring algorithm to solve the single point of failure caused by the jitter problem.

MongoDB supports Master-slave,replicaset (internal using Paxos election algorithm, automatic fault recovery), auto sharding mechanism, blocking the failover and segmentation mechanism to the client.

5. Reliability (persistent)

For data persistence and data recovery,

Redis Support (snapshot, AOF): dependent on snapshots for persistence, AOF enhances reliability while impacting performance

Memcache not supported, usually used in cache, improve performance;

MongoDB supports persistent reliability from the 1.8 release with the binlog approach

6. Data consistency (transactional support)

Memcache in concurrent scenarios, with CAS to ensure consistency

Redis transaction support is weak and can only guarantee continuous execution of each operation in a transaction

MongoDB does not support transactions

7. Data analysis

MongoDB has built-in data analysis capabilities (MapReduce), others do not support

8. Application Scenario

Redis: More performance operations and calculations with smaller data volumes

Memcache: Used to reduce database load in dynamic system, improve performance, cache, improve performance (suitable for read and write less, for a large amount of data, you can use sharding)

MongoDB: The main solution to the massive data access efficiency problem

Interview: Comparing cached Redis with memcached

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.