Managing user balances is an important part of many applications and systems. Here are some reasons why it's important:

Ensuring Proper Application Functioning

Applications dealing with users' financial data must guarantee accuracy and reliability of balance operations. This is especially crucial for financial apps, marketplaces, and gaming platforms. Developers and support staff need confidence that user balances are accounted for and changed properly according to users' actions.

Customer Loyalty

Users expect convenience and transparency in managing their funds. Effective balance management provides reliable tools for financial control. This increases trust in the product or service. Errors in balance management can lead to user dissatisfaction. Loss of funds or incorrect transactions can affect a company's reputation.

Fraud Prevention

Effective balance management also plays a role in preventing fraud. Developers and support should be able to quickly identify and investigate suspicious transactions and balance anomalies.

Ease of Support and Development

A well-designed balance management system makes life easier for developers and support staff. It allows quickly finding and fixing problems and providing swift support to users.

In some industries, compliance with legal requirements and regulations is mandatory. Effective balance management helps adhere to standards and regulations.

Different Ways to Store and Work with Balances

1. Simple Balance Table

The first and easiest approach is to use a simple balance table. Each user is assigned a unique ID, and the user's current balance is stored as a number or other value.

userId

balance

59a20c57

300

5babf7f8

100

d071d371

200

To change the balance, you need to execute the following query (PostgreSQL syntax below):

UPDATE "Balances"
SET amount = amount + 100
WHERE "userId" = '5babf7f8';

ADVANTAGES


DISADVANTAGES


WHERE IT’S USED


2. Transaction Table

The second approach involves using a transaction table to store the history of all operations.

id

userId

amount

createdAt

1

5babf7f8

100

2022-10-25

2

5babf7f8

50

2022-10-26

3

d071d371

300

2022-10-27

4

5babf7f8

-100

2022-10-28

In this table, every balance change operation is recorded indicating the user, amount, and timestamp. Each row represents a new account transaction for a specific user. If needed, fields explaining the reason for each record or other metadata can be added.

To get the current balance of each user, you need to make the following query:

SELECT "userId", SUM(amount) AS balance
FROM "Transaction"
GROUP BY "userId";

userId

balance

5babf7f8

50

d071d371

300

The query can be saved as a view.


ADVANTAGES


DISADVANTAGES

id

userId

amount

createdAt

5

d071d371

-200

2022-10-29

6

5babf7f8

200

2022-10-29


The change records for both balances are not linked in any way. Even if a linkage is made via a third field, there is no guarantee of transaction integrity. With potential manual edits, it will be difficult to navigate and maintain consistency.


WHERE IT’S USED


3. Transaction Table with Sender and Receiver

The third approach is to use a transaction table indicating the sender and receiver for each operation. This provides full transparency and cohesion of transactions between users.

id

from

to

amount

createdAt

1

source-1

d071d371

500

2022-10-24

2

source-2

5babf7f8

400

2022-10-25

3

source-2

d071d371

100

2022-10-26

4

d071d371

source-2

100

2022-10-27

5

d071d371

5babf7f8

200

2022-10-28

Each row signifies a transfer from the "from" sender to the "to" receiver. The query to output all balances will look like this:

SELECT "userId", SUM(amount) AS balance
FROM (
  SELECT "to" AS "userId", amount FROM "Transactions"
  UNION
  SELECT "from", -amount FROM "Transactions"
) "T"
GROUP BY "userId";

For convenience, a view can be created from the query. For this method to work properly, service accounts need to be created; in this case, source-1 and source-2. These could be, for example, different ways to top up your own accounts. It may be important for users to know where the funds came from.

In addition, transfers between accounts take up only one record, and income and expenses are inextricably linked. When deleting, balances change immediately on both accounts.


ADVANTAGES


DISADVANTAGES


WHERE IT’S USED


In summary

The choice of method for working with balances in the database depends on the specific requirements of the system. A simple balance table can be an ideal solution for small projects that require minimizing complexity.

A transaction table is suitable for situations where it is important to have a history of transactions. A transaction table indicating the sender and recipient is the most powerful and flexible solution for complex financial systems. Each method has its advantages and disadvantages, and the choice should be based on the needs of a particular project.