Redis knowledge consolidation

Redis knowledge consolidation

1. Redis, and MySQL difference?
a) .mysql is a relational database, while the NOSQL redis is, non-relational database. mysql persistent data to the hard disk, the read data is slow, and redis data previously stored in the cache, read speed is fast, but the limited storage time, may be selected as desired and finally persisted to disk.

b) .mysql as persistent database, each access for I / O operations on the hard disk. Frequent access to the database will spend a lot of time on repeated database connection. redis will be a lot of data stored in the cache frequently accessed, when a browser to access data, access to the cache first, if not re-enter the access database.

2.Redis five data types?
a) add the string type String operations: set
B) the object hash hash add operation: HSET
. C) List object list add operation: LPUSH
D) a collection of objects set add operation:. Sadd
. E) ordered collection of objects to add the sorted set operation: Zadd
(other operating omitted)

How 3.Redis key to solve the conflict?
Zipper law. This problem can be taken into account another problem, how to solve the key conflict HashMap, the same method is also used zipper address. If the key value is asked to solve the conflict there any other ways? General method to solve the key values of a conflict of two kinds, one is open address method, and the other is the zipper method.

4.Redis storage mechanism and persistence solution?
You must first understand what is persistence? The first problem described above has been passed, in simple terms, this is similar to MySQL data directly saved to the hard way is persistence. But Redis can not copy the SQL storage method, the biggest reason is talked about --NoSQL, which means no operation is similar to the B + tree.
1
The first way RDB persistence strategy. Is the default method of redis. First store data to memory, when the data accumulation triggers a DUMP operation, to change the threshold value to a certain set of the file data into the RDB. Once Redis abnormal exit, you will lose all data changes after the last snapshot


The second approach: strategy first open AOF persistent data stored in the memory. After opening AOF each perform a persistent change in the data Redis commands, Redis will be written to the hard disk in order AOF file .AOF file does not store data, it is stored command execution. AOF is the most critical configuration with respect to the frequency of the additional call fsync the log file, there are two preset frequency, always come in each record are added, everysecond added once per second. (Action function fsync is to write data from the hard disk memory).

Since the contents of the file AOF and storage is not the same RDB, RDB memory is true, deposit into the data, the storage of a large number of AOF instruction manipulating data, such instructions once increased, will inevitably make AOF files expand, so will triggered a new mechanism: Rewrite. Is called Rewrite all data log files are re-written to another new log file, but the difference is that for the old log files for multiple operations Key, retaining only the final value of that operation to record log files, thereby reducing the size of the log file.

(trigger Rewrite the size of the current log file is written to account for a certain percentage of the initial log file size) auto-aof-rewrite-percentage 100.
auto-aof-rewrite-min- size 10mb ( the minimum amount of data written to this Rewrite).
5.Redis expiration policy
on how to set the expiration time of redis? The easiest is to use
expire key second, of course, there are various other ways, such as
pexpire key milliseconds and so on, redis View expiration time syntax is very simple, ttl key left you can see how long.
1
2
3
How to delete an expired key over and over again, that is most likely to think a key is created for each timer, a time to automatically delete. This method is not reliable, although it expired to ensure that each key can be removed as soon as possible, but to create a timer itself consume a large amount of engineering resources, it is not desirable.

redis built-in strategy is called inertia delete, also known as "passive deleted."

key does not remove the expired time, get key from the database each time to check for expired, if expired, deleted, returns null.
This method is preferable, but there must be another problem, use the time to determine whether the expired delete, if I do not use it?

If a large number of key after a timeout beyond, within a long period of time, have not been acquired, then the memory leak may occur

Out of memory out of memory, refers to the program at the time of application memory, there is not enough memory space for its use, appear out of memory; memory leaks
memoryleak, refers to the program after the application memory to free up memory space has been filed, a memory leak hazards can be ignored, but the accumulation of memory leaks very serious consequences, no matter how much memory, sooner or later occupied light.

Memory heap overflow is not enough memory space to store, still no new object is enough memory to store the GC, OOM error is generated.
Although the object is a memory leak is useless, but it is still a path GCroot objects associated with it, there is no way GC recovery, but once the accumulation of too much will cause OOM.

This time redis still need to take the initiative to retrieve, so it is necessary to use another strategy to
periodically delete. , Also known as active deleted.
Periodically perform a deletion (hz set in redis.conf profile, 1s refresh frequency) expired key operation

First, the configuration redis.conf hz options, default is 10 (i.e., 1 second performed 10 times, once 100ms, the faster the greater the value of the refresh rate, the greater the loss in performance most Redis)

Second, the configuration redis.conf maxmemory maximum, when the memory has been qualified with more than maxmemory, triggered the initiative to clean up strategy. Initiative is different from the clean-up initiative to remove, to avoid triggering the initiative to clean up.
*
1
2
3
4
Meanwhile, redis expiration policy for persistence and does not affect. If you use RDB, it will detect whether or not expired prior to the persistent key, the key expiration will not be saved into a file, while the recovery from the RDB database into memory will be tested once has expired.
If you use the AOF, because the instructions are stored, if the key expires, the program will append del instructions to the AOF. When Rewrite the trigger, it will determine whether the key expired, expired key command does not override the AOF file.

6.Redis cluster strategy?
redis clusters have more, but the performance is the most powerful official redis-cluster.

1. Thought to the center, using the hash slot way hash slot 16348 to cover all the nodes is stored for each key value using CRC16 (KEY) & 16348 = slot obtained and his corresponding hash slot in the access key find his hash slot in which a current access node by the node and then actually allocated from this hash slot node to fetch data.
2. Voting fault tolerance. Fault Tolerance by voting to determine whether the node is still work. Redis cluster, each node time to send "heartbeat packet" to the other nodes. When a node does not respond, then the node failure is determined. More than half of the voting requirement is not responding.
3. In order to solve the problem failed master node fail, you can use master-slave replication strategy.

When the entire cluster is unavailable (cluster_state: fail)?
If the cluster master hang arbitrary, and is not the current master Slave. Enter the cluster fail state, as may be appreciated that slot cluster mapping [0-16383] to enter the fail state is incomplete.

If the cluster master hang up more than half, regardless of whether slave (from), enter the cluster fail state.

 

Tips: inconsistent data A, B, C is stored in three nodes, the node integrates three data is completed. However, the primary server A and a server data has been stored from the.

Architectural details:
(1) all the nodes are interconnected and redis (PING-PONG mechanism), using the binary protocol optimized internal transmission speed and band
width.
(2) is to take effect until the node fail through the cluster node detects a failure of more than half.
(3) redis client node is connected, without an intermediate layer proxy. the client need not be connected to all cluster nodes, the cluster is connected
to any one of the available nodes can
. 1
2
3
. 4
. 5
. 6
. 7
. 8
7.Redis is single-threaded ?
redis is single-threaded!
redis core is, if all my data in memory, I went operational efficiency is the highest single-threaded, why, because nature is multi-threaded CPU simulated case of multiple threads, which have a simulated situation the price is the context switching for a memory system, it has no context switch is the most efficient. When redis binding piece of memory data with a single CPU, and multiple data read and write for this memory, it is done on a CPU, so it is single-threaded handle this matter. In the case of memory, this program is the best option.

When a multi-threaded program it?
The answer is: the case of the lower storage and other slow. Such as disk
----------------
Disclaimer: This article is the original article CSDN bloggers "trained male is short", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_39658020/article/details/86023606

Guess you like

Origin www.cnblogs.com/alliswell2king/p/11842559.html