Stop 12 · Transactions & ConcurrencyProjects · 1/1

Bank Transfer Simulation

Part of the SQL Roadmap

In short

Moving money is the classic test of everything in this phase. In this project you build a transfer that can't lose a rupee, even with many transfers running at once.

Build a small accounts and transfers schema and write a transfer routine that is safe under concurrency: a transaction, row locking, a CHECK that balances never go negative and a transfer log.

Inside a bank vault, the jaadu pushes a bundle of rupee notes through a sealed glass tube connecting two steel lockers, with a thick register open on a desk beside it.

Think of it like this

The sealed tube between two lockers

Picture a cooperative bank in Nashik where money moves between lockers only through a sealed tube. The notes leave one locker and arrive in the other in one push. They can't fall out halfway, two clerks can't push from the same locker at once, and a register records every push. Your SQL transfer needs the same three things: all or nothing, locks, and a log.

Watch it happen

One safe transfer

BEGIN
Lock rows
Check
Debit + credit
Log
COMMIT

1/6Kavya wants to send ₹1,500 to Arjun. Start a transaction.

The big ideas

What you need to know

NUMERIC for money

Use NUMERIC(12,2), never FLOAT. Floating point can't store amounts like 0.10 exactly.

Constraints are the last guard

CHECK (balance >= 0) stops an overdraft even if the app's own check has a bug.

Lock in id order

Locking both rows lowest id first prevents deadlocks between opposite transfers.

Log every transfer

A transfers table makes every movement traceable. Total balance should never change.

How it works

Building the project

  1. 1

    Schema

    accounts with a CHECK on balance, and transfers with foreign keys and a positive amount.

  2. 2

    Seed

    Insert a few accounts with realistic rupee balances.

  3. 3

    Transfer

    Write the transaction: lock, debit, credit, log, commit.

  4. 4

    Stress it

    Run opposite transfers from two sessions at once and confirm no deadlock or lost money.

  5. 5

    Prove it

    Show that SUM(balance) is the same before and after, and document the edge cases.

See it in code

Walk through the code

sql
1CREATE TABLE accounts (2  account_id INT PRIMARY KEY,3  holder     TEXT NOT NULL,4  balance    NUMERIC(12,2) NOT NULL CHECK (balance >= 0)5);6 7CREATE TABLE transfers (8  transfer_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,9  from_id     INT NOT NULL REFERENCES accounts,10  to_id       INT NOT NULL REFERENCES accounts,11  amount      NUMERIC(12,2) NOT NULL CHECK (amount > 0),12  created_at  TIMESTAMPTZ NOT NULL DEFAULT now(),13  CHECK (from_id <> to_id)14);15 16INSERT INTO accounts VALUES (1, 'Kavya Iyer', 10000), (2, 'Arjun Singh', 2500);17 18-- Transfer ₹1,500 from Kavya (1) to Arjun (2)19BEGIN;20SELECT account_id FROM accounts21WHERE account_id IN (1, 2)22ORDER BY account_id23FOR UPDATE;24UPDATE accounts SET balance = balance - 1500 WHERE account_id = 1;25UPDATE accounts SET balance = balance + 1500 WHERE account_id = 2;26INSERT INTO transfers (from_id, to_id, amount) VALUES (1, 2, 1500);27COMMIT;28 29SELECT SUM(balance) AS total_money FROM accounts;   -- still 12500

1/5The CHECK makes a negative balance impossible at the database level.

PostgreSQL syntax. In MySQL, use INT AUTO_INCREMENT for transfer_id and START TRANSACTION instead of BEGIN. MySQL enforces CHECK constraints from 8.0.16.

Save yourself the pain

Common mistakes

Oops

Checking the balance with a plain SELECT, then updating in a separate step.

Do this instead

Another transfer can slip in between. Lock with FOR UPDATE, and rely on the CHECK as a final guard.

Oops

Storing balances as FLOAT or REAL.

Do this instead

Use NUMERIC(12,2) so ₹0.10 + ₹0.20 is exactly ₹0.30.

Oops

Locking the sender first, then the receiver.

Do this instead

A to B and B to A at the same time will deadlock. Always lock the lower account_id first.

Your mission

Build and break it

~60 min

Quick check

Kavya has ₹1,000 and you try to transfer ₹1,500. With the CHECK (balance >= 0) constraint, what happens?

Go deeper

  1. 1Create accounts and transfers tables with suitable constraints.
  2. 2Write the transfer as a transaction and test it with two sessions moving money at once.
  3. 3Verify that the total money in the system stays the same after many transfers.

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge