1. Now the business logic is to update a data state to 2 and then to count the data with a status of 2. Due to MySQL master-slave delay, data is sometimes accurate and sometimes inaccurate
2. The updated data and statistics are logically together and cannot be separated. For example, I just saved the money, the table field status changed to 2. Then I need to sum up immediately, the user total amount of money, to ensure that the user total money is correct.
3. What are the solutions? Are there any good ideas in logic that can be separated? To ensure that the sum is correct, but also to ensure that the user in the state of the amount of money?
4. Come on, you great God.
Reply content:
1. Now the business logic is to update a data state to 2 and then to count the data with a status of 2. Due to MySQL master-slave delay, data is sometimes accurate and sometimes inaccurate
2. The updated data and statistics are logically together and cannot be separated. For example, I just saved the money, the table field status changed to 2. Then I need to sum up immediately, the user total amount of money, to ensure that the user total money is correct.
3. What are the solutions? Are there any good ideas in logic that can be separated? To ensure that the sum is correct, but also to ensure that the user in the state of the amount of money?
4. Come on, you great God.
Update the cache before updating to the database.
Read the cache first, cache no, read the database again
Directly read the main library chant
It is recommended that you first describe what you want to achieve.
And then how did you do it;
Finally, what is the problem.
I have summed up several ways:
As mentioned above, increased buffering, or the use of multi-live multi-slave database form, but this can not fundamentally solve such problems. Because the problem of master-slave delay cannot be eliminated.
Read the main library directly as described above.
This is primarily a business logic issue,
The technology can be solved by optimistic locking method.
You can add transactions to the database and roll back.
Data redundancy method, do not use daily update data and then statistics updated data mode, directly to the User Add Balance field, each update this field, with optimistic lock and transaction, perfect solution.
With more user data, the performance problem of sum will become more and more prominent
The wrong place also invites the great God to instruct
Master-Slave delay, the data is time-stamped, the time stamp from the machine over the master-slave delay data to ensure that is consistent, not necessarily the latest, the significance is small. Master-Slave delay is only for disaster preparedness, in case the data clerk is cheap, the data deleted, then can be recovered from the delay from the machine.