MySQL master-slave delay, causing the data read error just updated, but the business logic to update the data and read the data is now necessary to follow the operation, how to break?

Source: Internet
Author: User
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:

    1. 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.

    2. Read the main library directly as described above.

    3. This is primarily a business logic issue,

      1. The technology can be solved by optimistic locking method.

      2. You can add transactions to the database and roll back.

      3. 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.

      4. With more user data, the performance problem of sum will become more and more prominent

    4. 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.