DEV Community

Shreya Princy
Shreya Princy

Posted on

Durability

Ensuring Reliable Money Transfers Using Database Transactions

In a digital wallet system similar to PhonePe, Google Pay, or Paytm, users expect their money to be handled accurately and securely. Even a small inconsistency such as deducting money from one account without crediting another can lead to serious financial issues. To prevent such problems, database systems rely on ACID properties , especially Durability, to guarantee safe and consistent transactions.

System Overview

The system maintains an accounts table where each user has a balance. Users can:

Store money in their wallet
Transfer money to other users
View their transaction history

Performing a Secure Transfer:
A typical money transfer between two users (for example, Alice to Bob) is executed within a transaction:

  • The system begins a transaction
  • Deducts money from the sender’s account
  • Adds the same amount to the receiver’s account
  • Commits the transaction

If all steps succeed, the transaction is permanently saved. After committing, querying the database shows the updated balances correctly.
What Happens During a System Failure?
Failure Before Commit:

If the system crashes before the transaction is committed, none of the changes are saved. The database automatically rolls back the transaction, ensuring that no partial updates occur. This prevents situations like money being deducted without being credited.

Failure After Commit:

When the database restarts, the updated balances remain intact. This behavior demonstrates Durability, meaning once a transaction is committed, it will not be lost.

How the Database Ensures Durability:
Modern databases like PostgreSQL use a mechanism called WAL. Before applying any changes to the actual data, the database records them in a log file. In case of a crash, the system replays this log to restore the latest committed state. This guarantees that committed transactions survive unexpected failures.

Why This Matters:
Without proper transaction handling:

  • Users could lose money
  • Duplicate transactions might occur
  • Account balances could become incorrect

By enforcing ACID properties:

  • Transactions are either fully completed or not applied at all
  • Data remains consistent at all times
  • Concurrent operations do not interfere with each other
  • Committed data is permanently stored

Step 1: Successful Money Transfer (Transaction)

Example: Transfer 200 from Alice to Bob

UPDATE accounts SET balance = balance - 200, last_updated = CURRENT_TIMESTAMP WHERE name = 'Alice';
UPDATE accounts SET balance = balance + 200,last_updated = CURRENT_TIMESTAMP WHERE name = 'Bob';
COMMIT;

Top comments (0)