Key-value Memory Database: memcached vs Redis

Source: Internet
Author: User
Tags lua memcached redis cluster

Memcached:

    • High-performance distributed cache database
    • Key-value storage mode (HASHMAP)
    • Data persistence is not supported, all data is lost after server shutdown
    • C developed, based on the Libevent library, available on most linux,bsd,solaris, as well as in Windows and other OS
    • The client implements the software in a number of languages: C + +, PHP, Java, Python, Erlang, Perl, Lua,go, etc.

Redis:

    • Open Source Key-value Storage System
    • C Language Development
    • Can be used on most linux,bsd,solaris and other systems without dependency
    • Client language: c/c++,c#,obj-c,php, Python,java,perl,lua,erlang

Memory Management methods:

Memcached: Using the slab allocation mechanism to manage memory by default, the main idea is to divide the allocated memory into blocks of specific lengths to store the corresponding length of Key-value records in a predetermined size to solve the problem of memory fragmentation. Slab allocation is only used for external data storage, and memcached other memory requirements are applied through malloc/free.

The principle of Slab allocation is quite simple. As shown in 3, it first requests a large chunk of memory from the operating system and divides it into blocks of various sizes chunk, and divides the same size blocks into group slab Class. Where chunk is the smallest unit used to store key-value data. The size of each slab class can be controlled by making growth factor when the memcached is started. Assume that the value of growth factor in Figure 1 is 1.25, so if the size of the first set of chunk is 88 bytes, the second group chunk is 112 bytes in size, and so on.

When memory is low, the LRU mechanism is used to replace stale data

Redis:

Redis memory management mainly through the source code in the zmalloc.h and zmalloc.c two files to achieve. To facilitate memory management, Redis allocates a chunk of memory to the head of the memory block.

Size | MEM Block

Redis records all memory allocations by defining an array whose length is zmalloc_max_alloc_stat. Each element of the array represents the number of memory blocks allocated by the current program, and the size of the memory block is the subscript for that element. In the source code, this array is zmalloc_allocations. ZMALLOC_ALLOCATIONS[16] represents the number of memory blocks that have been allocated for a length of 16bytes. ZMALLOC.C has a static variable used_memory used to record the total amount of memory currently allocated. So, on the whole, Redis uses packaging mallc/free, which is much simpler than Memcached's memory management approach.

Take a disk storage mechanism to achieve data persistence. However, when the amount of data reached about 10 million, because the memory can not store such a large number of data, frequent data exchange with the disk, resulting in a sharp decline in data query, storage performance, will lead to service unavailable

Distributed mechanism:

Memcached: It does not natively support distribution, so it is possible to implement memcached distributed storage only at the client through a distributed algorithm such as a consistent hash.

Redis: More inclined to build distributed storage on the server side. Although the stable version currently released by Redis has not yet added distributed storage functionality, Redis cluster has the basic functionality of Redis. It is expected that after the 2.6 release, Redis will release a stable version that fully supports distributed, not later than the end of 2012. Below we will briefly introduce the core idea of Redis cluster according to the implementation in the development version.

Redis cluster is an advanced version of Redis that implements distributed and allows single points of failure, with no central node and linear scalability.

Overall Performance:

  

1) Performance comparison: Since Redis uses only single cores, and memcached can use multicore, on average, Redis has a higher performance than memcached for storing small data on each core. In more than 100k of data, Memcached performance is higher than Redis, although Redis has recently been optimized for the performance of storing big data, but it is slightly inferior to Memcached.

2) Memory usage efficiency comparison: With simple key-value storage, memcached memory utilization is higher, and if Redis uses hash structure to do key-value storage, its memory utilization will be higher than memcached because of its combined compression.

3) Redis supports server-side data manipulation: Redis has more data structures and supports richer data operations than memcached, usually in memcached, you need to get the data to the client for similar modifications and set it back. This greatly increases the number of network IO and the volume of data. In Redis, these complex operations are often as efficient as the general get/set. Therefore, Redis is a good choice if you need caching to support more complex structures and operations.

4) If you have requirements for data persistence and data synchronization, it is recommended that you choose Redis. because neither of these features is memcached. Choosing Redis is also wise, even if you just want the cached data to be not lost after upgrading or rebooting the system.

Key-value Memory Database: memcached vs 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.