ArticleDirectory
- To solve the first problem, let's take a look at the process of establishing a TCP/IP protocol link.
- Next, let's take a look at the second question: how to maintain data consistency updates. For more information, see the TCP/IP protocol.
- The third problem is complicated.
Recently I wrote a number of articles about the ticket purchasing system of the Ministry of Railways.
Design of the New Railway Ticket System of the Ministry of Railways (I)
Design of the New Railway Ticket System of the Ministry of Railways (II)
Design of the New Railway Ticket System of the Ministry of Railways (III)
This is a simple example of data consistency in a distributed system.
Now, let's throw a problem. Suppose there is a primary data center in Beijing m, and then there are two local data centers in Chengdu A and Shanghai B. The problem is, assume that the data centers in Chengdu and Shanghai have recorded changes, and the changes must be synchronized to the primary data center. After the primary data center is updated, the latest data will be distributed to Shanghai, local Data Center A and local data center a in Chengdu update data to maintain consistency with the primary data center (the database structure is completely consistent ). Data update messages are forwarded through a central MQ.
First, simplify the problem. Assume that a adds a record message_a and sends it to m, and B adds a record message_ B to send it to m, which is forwarded through the MQ server. Then the M system receives a message, after adding two pieces of data, M is sending the added messages to A, B, A, and B to find the missing data and update the database. In this way, a data synchronization is completed.
From the normal situation, there are no problems, and the logic is completely reasonable, but consider the following three questions:
1. m must have received the messages of A-> M. Similarly, m must have received the messages of M->.
2. If the data needs to be updated in a, for example, if a sends three messages to M, m either saves them all or does not save them all, and only a few records cannot be saved. We assume that the updated data is sent one by one.
3. If a sends multiple update requests at the same time, how can we ensure the order?
These two problems are data consistency in distributed environments.
To solve the first problem, let's take a look at the process of establishing a TCP/IP protocol link.
We can start with this idea and simplify the process by sending a request and a response.
The simple communication model is like this.
A-> M: You did not receive a message from me. The message ID is 12345.
M-> A: I have received one of your message data. The message data is ID; 12345
In this way, a request and a response can complete a reliable transmission. If a does not receive the m response, it will try again. In this case, M must ensure idempotence. Messages cannot be processed repeatedly. In this case, the most extreme case is that the M response cannot be received, which is a system fault. Check it yourself.
This design requires that a persists the data content of the message when sending the message, and then retries continuously. Once receiving the m response, a deletes the message. Similarly, M ends are the same. Do not trust the persistence mechanism of MQ, which is not very reliable.
Then M can adopt a similar principle to send messages to.
Next, let's take a look at the second question: how to maintain data consistency updates. For more information, see the TCP/IP protocol.
First, a sends a message to M: I want to send a batch of message data to you. The batch number is 10000, and the data is 5.
M sends a message to a: OK, I have prepared it. the batch number is 10000. the sender is
Then, a sends five messages to m, with the message IDs of 1, 2, 3, 4, and 5 respectively. The batch number is 10000,
Next, a sends a message to M: I have sent five small messages, and you have to submit data updates.
The following two scenarios may be sent:
1 then M sends a message to a: OK. I received five messages and started to submit data.
2. m can also be sent to a: I have received five messages, but it is still missing. Please resend the message, then a will continue sending until a receives a successful response from M.
The entire process is quite complex. This means that once data is distributed, the biggest problem is data consistency. This cost is very high.
The third problem is complicated.
The core problem is the order of messages. We can only send a message serial number for each message, but there is still no best solution to this problem. Because the Message Receiver does not know the order. Because even if the serial number is given to him, there is no way to tell him when to handle it. The best way is to update the data in batches based on the second method.
This is just the simplest example. It is very costly for a distributed system to ensure data consistency. Of course, some bloggers will say, why is this so complicated? Direct Database Synchronization is not enough. There is no problem with this example. In case the models of these databases are different, I want to handle different messages. What should I do?