Introduction and difference of memcache and redis

What is memcache?


Memcached is a distributed memory object caching system, is based on the Key / Value pairs HashMap. Each pair, the expiration time can be set, (if set to 0 [never expire actually never expires] expires in 30 days)
, but the data Memcached is stored in memory, so that a power failure or a server failure will cause the loss of data
Memcached can be a variety of data storage arrays, objects, characters, and other types of characters

Memcached features:
(1) no backup, no backup data between each other each Memcached node, a node once hung, the data cache will be lost.
(2) basic command is to add a specific key, deletions, substitutions, atomic update, read, etc., including the Set, the Get, the Add, the Replace, the Append, Inc is an / On Dec
(. 3) of the Memcached application mode, does not support transactions but you can do some atomic operations command.

Memcached memory allocation principles:
that will be pre-allocated memory, characterized by speed, but not the rational use of space, the simple point is to use the space for speed, in general, a memcahced process will advance to divide themselves into several slab, each under the slab there are several page, under each page there are multiple chunk
number slab is limited too, a few dozen or a few dozen, with this configuration process was related to memory, and to be under each slab page corresponding to the actual physical space default is 1M, that is to say the next page 100 has a slab 100M, the data stored in the chunk itme encapsulated inside. Chunk is a fixed-size memory space, the default is 96Byte.
The same size chunk, also known as slab.

Memcache data deletion mechanism:
when the data has expired, memcache does not remove the data directly from memory, because memcache does not reclaim the memory has been allocated, but only checks whether the timestamp expired when get data, then the client if expired not visible, but the original allocation of memory can be reused, which is called inertia failure. So memcache not expired monitor data and, therefore, the cpu saving resources.

LRU algorithm mechanism:
using LRU mechanism delete data in memcache, that is the least recently used. In each slab data is stored on the list, the list has a head and tail pointer, pointing to the oldest and the most recent data. When the LRU mechanism starts, two hands at the same time play a role in the failure data query, then it will fail if there is no data to delete the data of the least recently used. Counter to record the data by which the least used to remove it, so it may not yet expired or permanently delete some valid data.

LRU only for each slab, not for the whole. LRU will be called only if the memory can not allocate new page in the slab.
An effective way to ease the use of LRU is:
1. Avoid large object
if the system only has a few large objects and individual words, a waste of memory space, because Slab applied Page can not release memory, and individual large objects can cause Slab application memory resources are not fully utilized.
2. Adjust the growth factor
to adjust the growth factor according to the needs of the project, to make full use of memory.

What is Redis?


Redis is an open source key-value storage system, and the like Memcached, Redis most of the data is stored in memory.
Supported data types include: string, hash tables, lists, sets, and ordered set based on these data types related operations, the most commonly used data consists of five main types: String, Hash, List, Set and Sorted Set.

redis concept can support transactions via Multi / Watch / Exec commands, atomic execute a batch command.
Redis prefer to construct the distributed storage on the server side.

Redis offers different levels of data persistence ways:


RDB persistent manner can be spaced to store a snapshot of your data at a specified time. The actual operation is fork a child process, the first set of data written to a temporary file, write successfully, and then replace the previous file, binary compressed storage.
AOF persistent log is recorded each time the server write operations, delete, query operation will not be recorded, when the server is restarted when re-executes these commands to restore the original data, each write operation to the end of the file .

RDB advantage:
Once this manner, then your entire Redis database will contain only one file, which for file backup is perfect. For example, you might intend to archive every hour last 24 hours of data, but also archived once in the last 30 days of data every day. With this backup strategy, once the system has failed catastrophically, we can very easily be restored.
For disaster recovery purposes, RDB is a very good choice. Because we can very easily be a separate file compression and then transferred to other storage media.
Performance is maximized. For service of process Redis, at the beginning of persistence, it is the only need to do is fork out the child, and then after the completion of these persistent work by the child, so that you can greatly avoid the process of an IO operation of the service.
Compared to AOF mechanism, if the data set is large, start RDB efficiency will be higher.

RDB Cons:
If you want to guarantee high availability of data, that is, the maximum to avoid loss of data, then the RDB would not be a good choice. Because once the system downtime phenomenon before the timing of persistence, after a few minutes is not enough time to write data on the disk will be lost.
Since the RDB by fork child process to assist with data persistence work, so if and when the data set is large, it may cause the entire server to stop serving hundreds of milliseconds, or even one second.

AOF Advantages:
This mechanism can result in higher data security, namely data persistence. Redis provides 3 in synchronization strategy, that sync every second, every modification and synchronization are not synchronized. In fact, asynchronous synchronization is complete per second, its efficiency is very high, the difference is that once the system downtime phenomenon, then within seconds the modified data will be lost. And every modify the synchronization, we can be regarded as synchronization persistence that occurs every time data changes will be immediately recorded to disk. It is foreseeable that in this way is the lowest in efficiency. As for non-synchronous, needless to say, I think we can correct understanding of it.
Since the mechanism writes the log file is used in append mode, so even if there is the phenomenon of downtime during the writing process, it will not destroy the contents of the log file that already exists. However, if we write this operation is only half the data appeared system crash, do not worry, before the next startup Redis, we can to help us solve the problem of data consistency by redis-check-aof tool.
If the log is too large, Redis can automatically enable rewrite mechanism. That Redis append mode will keep the modified data is written to the old AOF file while Redis also creates a new file to record what has changed on command is executed during this period. It is possible to better ensure data security during overwriting handover.
AOF format comprising a clear, easy to understand for the log file records all the changes. In fact, we can complete the reconstruction data through the file.

AOF disadvantages:
for the same number of data sets, is generally larger than the file AOF RDB file. RDB speed when recovering large data sets faster than the speed of recovery of AOF.
Depending on the synchronization policy, AOF on the operating efficiency tends to be slower than in RDB. In short, the efficiency of synchronous per second strategy is relatively high, disable synchronization strategy of the efficiency and RDB as efficient.
Both the selection criteria is to look at the system as AOF is willing to sacrifice some performance in exchange for greater cache coherency, or are willing to write as frequently as RDB operating time, do not enable the backup in exchange for higher performance, to be run manually save time, do backup.


Redis and Memcache difference:

 

  1. Redis only supports simple k / v types of data, and also supports storage list, set, zset (sorted set), hash data structures, etc., such that it has a wider application scenario.
  2. Redis biggest bright spot is to support data persistence, it at runtime can back up data to disk, or restart after a power failure, the cached data can be loaded into memory again, as long as reasonable Redis configuration, basically without losing data .
  3. Application of Redis master-slave mode support.
  4. Redis single maximum limit value is 1GB, and Memcached can only save data in 1MB.
  5. Memcache in concurrency scenarios, cas can ensure consistency, and Redis transaction support is relatively weak, only to ensure that each successive execution of operations in the transaction.
  6. Performance, Redis on the read and write operations are slightly ahead of Memcached.
  7. Memcached Redis memory management not so complicated, metadata metadata smaller, relatively speaking, very little overhead.
  8. Memcached only supported data type is a string string, is ideal for read-only data cache, because the string does not require additional treatment.
     
Published 72 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_39399966/article/details/104360598