Memcache and Redis all belong to the cache but the Memcache storage size is the memcache of the received limit, the length of the key is 250, the size limit of the memory is 1M and the memcache does not support persisted cache of data
Redis supports five data types, string list set (set) hash and Sorted set (ordered collection), and Redi is a persistent cache that supports data, two mechanisms of Redis aof and Rdb
The persistence of Redis
- Rdb
A) In short, a snapshot of redis-stored data is generated at different points in time and stored on media such as disks;
b) for Rdb mode, Redis creates (fork) a child process separately to persist, and the main process does not do any IO operations, thus ensuring very high performance of Redis.
c) If large-scale data recovery is required and is not very sensitive to the integrity of data recovery, the RDB approach is more efficient than the AOF approach.
D) If you are sensitive to the integrity of your data, then the RDB approach is not suitable for you, because even if you persist every 5 minutes, there will still be nearly 5 minutes of data loss when Redis fails.
- AOF
A) record all the write commands performed by Redis, and at the next Redis reboot, the data can be recovered by simply repeating them once before and after the write commands are executed again.
b) The default aof persistence strategy is fsync once per second ;
c) aof in the file appended to the write , so do not do any processing, theaof file will become more and more large, for this, Redis provides a aof file rewrite (rewrite) mechanism, When the size of the aof file exceeds the set value, Redis initiates the content compression of the AoF file, preserving only the smallest set of instructions that can recover the data. If we call 100 times the incr instruction, 100 instructions are stored in the AoF file, but this is obviously very inefficient, it is perfectly possible to combine the 100 instructions into a set instruction, which is the principle of the rewriting mechanism.
- The difference between an RDB and a aof:
A) The Rdb method is the data, the AOF is the instruction;
b) Rdb and aof can be used at the same time, in which case the Redis reboot will take precedence over the AOF approach to data recovery because the AOF method has a higher degree of data recovery.
c) The size of the same data,the AoF file is larger than the size of the Rdb file. Also, the AOF mode is slower than the Rdb method.
- AoF Rewrite principle:
A) first, when the rewrite begins, a rewrite subprocess is created;
b) The child process reads The instruction parsing in the existing AoF file and compresses it into a temporary file;
c) While the main process receives a new write instruction, while continuing to write to the aof file, to ensure the availability of the original aof file, to avoid accidental rewriting process, while writing to the memory buffer;
d) when the child process finishes rewriting the task, it wants the main process to send a signal, and the main process receives the signal and appends the new write instruction in the memory buffer to the new aof file;
e) After the Append, the old aof file will be replaced with the new aof file, at this point, the rewrite ends;
Redis's master-slave
- master-slave structure, one for pure redundant backup, the second is to improve read performance, such as the consumption of performance Sort can be assumed by the server.
- The master-slave synchronization of Redis is asynchronous, which means that master-slave synchronization does not affect the primary logic, nor does it degrade the processing performance of Redis.
- In the master-slave architecture, you can consider turning off the data persistence feature of the primary server, allowing only persistence from the server, which can improve the processing performance of the primary server.
memecached Key Start setting parameters
- –m: The default allocated memory size is 2G for each process under 64m,32-bit operating system, so if you need to allocate more memory to use a 64-bit operating system, this will not be used as soon as necessary, it is gradually assigned to each slab . ;
–I: Adjust the size of the assigned page page, default 1M, Minimum 1K, Max 128K;
–f:memcached By default, the maximum value for the next slab is 1.25 times times the previous one , which can be set by this parameter ;
–P:TCP port setting, default is 11211;
–l: The IP address of the listener, if the machine is not set;
–d: Running in the daemon mode;
–u: Specify the user;
–M: Suppress LRU policy, return error when memory is exhausted, not delete item;
–c: The maximum number of simultaneous connections, the default is 1024;
–t: Number of threads, default is 4;
Memcache and Redis differences