Database & SQL Engineering / TRANSACTIONS, MVCC & CONCURRENCY / SQL-019

Last Balance Race

Explore concept graph

Praktik dan verifikasi mandiri

Independent challenge — Two-session overspend prevention

Konsep 1 / 3

Coordinate two overlapping withdrawals against a single account using SELECT FOR UPDATE and one transaction per session.

On a separate sandbox, coordinate requests for 95.00 and 40.00 against an initial balance of 110.00; prove the second request loses the lock race without a negative balance.

Kasus yang berbeda dari contoh: Uses a new starting balance and different withdrawals rather than 100.00 with 80.00 and 70.00.

Bukti yang bisa kamu periksa

  • Two real psql backends, observed Lock event, one accepted withdrawal, final balance 15.00 and one persisted withdrawal record.

Jika ingin lanjut

Repeat the same contention without an explicit SELECT FOR UPDATE, using an atomic conditional UPDATE.

Jika terputus: Start from the one-wallet 100.00 state; require observing B blocked before A commits.

Konsep 2 / 3

Use UPDATE ... WHERE balance >= amount with a checked affected-row count under concurrent READ COMMITTED writes.

Start with 90.00 and concurrently request debits of 60.00 and 50.00 using conditional UPDATE only; prove one UPDATE 0 and no second withdrawal record.

Kasus yang berbeda dari contoh: Different initial balance and amounts using guarded UPDATE, not SELECT FOR UPDATE then debit.

Bukti yang bisa kamu periksa

  • Observed Lock wait, checked ROW_COUNT failure, final balance 30.00, exactly one successful ledger row from a separate session.

Jika ingin lanjut

Extend the lock discipline from one wallet to two accounts touched in opposite directions.

Jika terputus: Test SELECT FOR UPDATE once, then compare it with conditional UPDATE plus ROW_COUNT.

Konsep 3 / 3

Acquire the same pair of account row locks in ascending ID order for opposing transfers.

Using two new accounts X-01/X-02 with balances 200.00 each, run concurrent opposite transfers of 60.00 and 25.00 with both acquiring locks in sorted ID order.

Kasus yang berbeda dari contoh: Uses different account IDs, initial balances and amounts, checking the generality of the ordered locking rule.

Bukti yang bisa kamu periksa

  • Observed blocked second session; both transactions commit; X-01=165.00, X-02=235.00, sum 400.00 and two distinct transfer rows.

Jika ingin lanjut

Apply this same locking contract to a different two-account transfer with independently checked invariants.

Jika terputus: When more than one row is modified, decide the shared lock order before writing either row.

Last Balance Race — no double withdrawal

Gunakan dua akun atau wallet baru; buat dua withdrawal overlap yang bersama-sama melebihi saldo. Tunjukkan satu transaksi ditolak dan tidak ada saldo atau ledger salah.

Latihan manual — jalankan sendiri di lingkunganmu. Kriteria di bawah adalah pemeriksaan mandiri, bukan penilaian otomatis.

Kriteria selesai

  • Dua backend PostgreSQL 16 harus benar-benar hidup dan overlap, bukan dua eksekusi serial.
  • Amati Lock wait aktual dari pg_stat_activity pada session B sebelum session A commit.
  • Paling banyak satu withdrawal berhasil; cek jumlah row dan balance dari koneksi independen.
  • Saldo akhir tidak negatif, dan UPDATE 0 diperlakukan sebagai kegagalan bisnis.
  • Jika dua account disentuh, kunci keduanya dalam urutan ID yang konsisten.
  • Coba dataset berbeda: 110.00 dengan 95.00 dan 40.00; jelaskan expected final state.
  • Jangan mengklaim serializability, semua jadwal konkurensi, production payment atau settlement.

Kriteria verifikasi mandiri

  • Mampu menjelaskan mengapa dua SELECT biasa rentan stale balance.
  • Membedakan row lock eksplisit dan conditional write yang melakukan locking implisit.
  • Bisa menunjukkan satu Lock wait dan satu kegagalan saldo dalam dua transaksi nyata.
  • Menjelaskan mengapa urutan lock berbeda bisa membentuk deadlock.
  • Memisahkan bukti concurrency lab dari klaim universal maupun settlement ke bank.