Redis brief and principles

Redis is a database key to Nosql based, since all of its data are stored in memory, so it's very amazing read and write performance.

A, Redis features

  1. Fast
    reason: the data which is stored in the memory
    for two reasons: using c language
    for three reasons: single-threaded architecture, to avoid the frequent memory switching multithreading
  2. Server-based data structure key-value pairs
    mainly provides five basic data structures: string, list, hash, set , zset
  3. Support rich functionality
    provides key expiration functionality
    provides publish and subscribe messaging function
    provides a simple transaction function
  4. Provide persistence function
    places the data in memory is not safe, once the power failure or machine failure, data loss occurs. redis provides two ways RDB persistence and AOF.
  5. Master-slave replication
    provides replication, replication is distributed on the basis

Two, redis usage scenarios

  1. Cache
  2. Ranking System
  3. Counter Application
  4. Social network
  5. Message queuing system
  6. Speed limit (one minute phone verification code sent once)
    Redis what not to do: data from the scale of it: it is not suitable for large-scale data, data from the hot and cold of it: it is not suitable for cold data

Three, Redis persistence

redis support ROF, AOF two kinds of persistence mechanism. Persistence mechanism can effectively avoid data loss.

1、RDB

RDB is the current process data generated snapshots saved to the hard disk of the process, starting RDB persistence process is divided into manual and automatic trigger trigger.

  1. Manual trigger
    (1) with the save command: blocking the current redis server until the process is complete RDB. It may cause obstruction for a long time, and is not recommended for use online.
    (2) Use bgsave command: Redis a fork operation, create a child process, the child process for persistence, automatically end after the completion of occlusion occurs only at the stage fork.

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-ErHQRu08-1574768001004) (http://10.15.0.10:8090/upload/2019/11/image-f2457d39533a4f4bb64470f89bc96e16 .png)]
2. passive trigger
(1) using the configuration save mn, when n indicates the revision of m seconds, the operation trigger RDB.
(2) the implementation of the whole amount of the copy operation from the node
(3) to perform debug reload command to reload redis, RDB trigger
the advantages and disadvantages of 3. RDB

  • Advantages of
    compact compressed binary file (1) rdf stored data representing a point in time snapshot.
    (2) Redis loaded RDB to recover data much faster than AOF way.
  • Disadvantages
    (1) can not be real-time persistent data
    (2) RDB version of a plurality of formats, the presence of incompatible.
2.AOF(append only file)

Independent log recorded each write command to restart when the AOF re-execute commands in the file for the purpose of data recovery. It solves the persistent problem of RDB can not be in real time. Now the mainstream of Redis persistent way.

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-d6sPQ6xh-1574768001006) ( http://10.15.0.10:8090/upload/2019/11/image-348b2eab27f9437c9c2eaa4c2f0649b5 .png)]
It should be noted: AOF content is written in text format

  1. Data synchronization strategy
    (1) always: Write a command every time you sync
    (2) everysec: sync once per second (recommended)
    (3) NO: operating system is responsible for a period of up to 30 seconds

  2. Rewriting mechanism
    with the command constantly being written to AOF, the file will be larger in order to solve this problem, Redis rewriting mechanism for the introduction of the volume of the compressed file. AOF file rewrite mechanism that converts data Redis process to synchronize write command to the new AOF file.
    Here is a question: Why rewrite is a mechanism to ensure that the compressed file size: (1) removing some useless command, del (2) to certain commands to merge, lpush num a, lpush num b , lpush num b can be combined to lpush num abc (3) may also exist timeout key, without the presence of AOF file
    [pictures of foreign chains dumping fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-Fvg6tcXM-1574768001007) ( http://10.15.0.10:8090/upload/2019/11/image-8227883f4ea14b6eb65c4bbfac68a9ee.png)]

Four, Redis problem

Redis persistence function has been the impact of the high incidence of Redis performance.

1.fork operation

Whether or RDB AOF, need to be fork operation, for the operating system, fork regarded as a heavyweight operation. Although the fork operation does not require a copy of the parent process memory space, but space replicated memory page table.
Solution: The maximum available memory control redis example, fork operation is proportional to the amount of memory and time consuming.

2.AOP additional obstruction

It can be seen by the figure below (1) everysec synchronization policy, loss of up to two seconds of data; (2) fsync if the system is relatively slow, it will affect the performance of the main Redis process.
[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-U4GTEZ0T-1574768001007) ( http://10.15.0.10:8090/upload/2019/11/image-fbbf2e40b3624eca99e2a9b420784ff7 .png)]
solution: (1) Do the hard disk and other high-load services deployed together, the storage service, the message Queuing service
(2) use ssd hard drive

Published 66 original articles · won praise 26 · views 10000 +

Guess you like

Origin blog.csdn.net/Time__Lc/article/details/103263597
Recommended