Data & Integrity

Data Integrity & Transactions

Intermediate

A system is only as trustworthy as its data is consistent. The dangerous failures are not the ones that crash loudly. They are the ones that leave the database in a state nobody designed: an order marked paid with no payment, or a balance debited but never credited. Transactions and integrity rules make those half-states impossible.

Integrity means every read reflects a consistent, valid state, and every write either fully happens or fully does not. The tools are concrete: database transactions for atomicity, constraints for invariants, idempotency for safe retries, and concurrency control for correctness when two operations race. Used together, they keep the data correct under failure and load.

In an AML or payments context, a broken invariant is not a small glitch. It is a wrong balance, a missed transaction, or a regulatory error. Multi-step operations that touch money or regulated state must be all-or-nothing. Retries must never apply the same change twice.

Make writes atomic and consistent

Survive retries and races

Partial write, no transaction db.Execute("UPDATE Accounts SET Balance = Balance - @amt WHERE Id=@from", ...);
// the process crashes here
db.Execute("UPDATE Accounts SET Balance = Balance + @amt WHERE Id=@to", ...);

A crash between the two statements destroys money. It is debited from one account and never credited to the other. There is no way back to a correct state.

One atomic transaction using var tx = conn.BeginTransaction();
conn.Execute("UPDATE Accounts SET Balance = Balance - @amt WHERE Id=@from AND Balance >= @amt", p, tx);
conn.Execute("UPDATE Accounts SET Balance = Balance + @amt WHERE Id=@to", p, tx);
audit.Record("Transfer", ..., tx);
tx.Commit();

Both legs and the audit record commit together or not at all. The balance check prevents overdraw. A crash rolls the whole thing back.

Self-review checklist

Why it matters: Data corruption is hard to spot. It builds up silently, breaks trust in every report and balance, and is often impossible to fully rebuild later. Transactions, constraints, and idempotency are how we make sure that under crashes, retries, and concurrency the data stays exactly as correct as the day it was written.