O
O
Oleg Tsvetkov2017-03-14 15:37:21
Database design
Oleg Tsvetkov, 2017-03-14 15:37:21

What is the best way to keep balance?

There is a table that stores the balance of users:

  • userId
  • balance
  • holdBalance

Now the balance field stores both the money earned by the user within the framework of the service and the money entered by him into the service (balance replenishment) . It is necessary to transfer the money that the user entered into the system into a separate column, conditionally depositBalance , which is needed to simplify the logic of the withdrawal mechanism.
What's the best way to do it?
  1. Store all money in the balance column , and use depositBalance in parallel ().
  2. Store the money earned in the system in the balance table, and in the depositBalance entered in the system. At the model/controller level, add both values.
  3. Something other...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lega, 2017-03-14
@olegtsvetkov

1st option + transaction log

S
sim3x, 2017-03-14
@sim3x

user_id
balance_change = Decimal
datetime
balance is received as balance_change sum
Or you can suffer with record locks
Or you can have problems with race updates

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question