Transaction boundaries

Decide which changes must succeed or fail as a single operation.

Written deep dive25% previewBy Daniyal Saqib

Read the opening here. A free account gives you the full guide.

A web application needs to apply several related changes in one operation. For example, transferring $100 from Account A to Account B must debit one row and credit another. Or bulk-importing 10,000 rows from a CSV: should each row be a separate transaction, or should all rows be inserted in one big transaction?

How large should each transaction be? What operations must be bundled together to ensure correctness, and what is the performance impact? The trade-offs are between atomicity (doing all or nothing) and latency/resource usage (transactions can hold locks and consume log space). The examples use PostgreSQL. First choose the correctness boundary; tune its size only where the product permits partial progress.

01

Simple transactions and ACID guarantees

A transaction groups the database changes that must commit together.

A transaction in SQL begins (e.g. BEGIN), executes one or more statements, and then ends with COMMIT or ROLLBACK. ACID properties apply:

Atomicity: All statements in the transaction either succeed together, or none take effect. A classic example is a funds transfer:

The application checks the returned rows before proceeding. Insufficient funds makes the guarded debit update zero rows; that is not automatically a SQL error. The application must roll back and stop. A missing destination also rolls back the debit. If either statement raises an error, roll back the transaction.

2 more sections to read

Sign in to keep reading

Transaction boundaries

Continue this section and read the complete guide and explore its visuals.

Free account. No payment required.

Read how we handle account information in our privacy notice.

2 more sections to read

There’s more in the full article

Continue reading with a free account.

Keep reading

Free account. No payment required.

Inside the full article

Continue the guide with 2 more sections.

  1. Transaction grouping and batch sizeRead after sign-in
  2. Using staging tables and partial commitsRead after sign-in
Continue reading

2 more sections to read

Sign in to keep reading

Transaction boundaries

Continue this section and read the complete guide and explore its visuals.

Free account. No payment required.

Read how we handle account information in our privacy notice.