A question has been puzzling for a long time, about the design of MySQL table

Source: Internet
Author: User
I now have a business need, is the account balance problem, how to design This account balance of the table?
Simple update updates the balance is obviously not working.
If the user on the PC side, the mobile operation balance, it is very likely that an error occurred
Do you have to use business?

Reply content:

I now have a business need, is the account balance problem, how to design This account balance of the table?
Simple update updates the balance is obviously not working.
If the user on the PC side, the mobile operation balance, it is very likely that an error occurred
Do you have to use business?

Transactions are not required, but transactions are the simplest, clearest, and most convenient way to implement them. You can use some other state transitions to achieve the correctness of the balance operation, but the whole state transition is quite complicated and troublesome, and even if you get out of the state conversion process, you are actually doing a transaction system.

This has nothing to do with business, you should be talking about locks. In fact
There is only one problem you need to solve: synchronous security, Solution two:

1. Using the locks provided by the database
After each operation of the balance to lock the balance (row lock, for update), and then you can operate at will, remember to commit the last transaction (will automatically release the lock, note that MySQL does not turn on autocommit transactions)

2. The application provides its own lock
If the operation of the balance is on the same interface as the PC or the mobile, the request for the balance operation is locked by using a separate lock (one lock per user) directly in the interface.
If it is not the same interface, then you need a distributed lock, Redis has a solution to provide a distributed lock, you can refer to the Redis official scheme: Build a distributed lock with Redis

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