Database design-master-slave consistency

1. What

Master-slave consistency means that the data of the master library and the slave library may be inconsistent at a certain moment.

二、Why

Because in the master-slave grouping architecture, the slave database takes time to synchronize the database of the master database, so there may be differences in data at a certain moment. For example, if the master library modifies a record at time 0, it will be synchronized to the slave library after 500 milliseconds. Then the master-slave data is inconsistent from time 0 to 500 milliseconds. If there is a query that hits the slave library at this time, read What gets is old data.

Three, How

  1. Option 1: Ignore.
    If the business side can bear to read a dirty data for a short time (usually as long as the user refreshes the correct data), then neglect is actually the most economical way. As long as the business allows, then our architecture It can also be simpler.

  2. Solution 2: Forced
    to read the master The way to force the master to read is to abandon the architecture design of read-write separation. At this time, a cache can be added to improve read performance.
    Insert picture description here

  3. Solution 3: Selective Reading of the Main
    This is a very interesting design method that requires the cooperation of the cache.
    Writing process:
    Insert picture description here

a. Assume that the master-slave delay is 1 second
. b. Before writing to the master database, cache a key, which is used to identify which data has been updated (such as which database, which table, which primary key), and cache The failure time is the synchronization delay, which is 1 second
. c. Normally write to the main library (master-slave automatic synchronization), this step is no different from the normal process.

Reading process:
Insert picture description here

a. Try to obtain the corresponding key of the database to be queried from the cache.
b. If it can be retrieved from the cache, it means that it is still within the synchronization time delay. At this time, choose to read the master
. c. Synchronize, read from the library directly.

Guess you like

Origin blog.csdn.net/hudmhacker/article/details/108259274