Core Concepts

ACID Transactions

Commit a pending turn and its capacity reservations together, then handle concurrent claims and an uncertain response.

Free~16 min

A database transaction lets several writes commit together. When checkout creates an order and reserves stock in one transaction, a failed reservation can roll back the order too. ACID also describes how transactions preserve database rules, what concurrent requests can see, and what survives a crash.

Group the writes that belong together

In the ChatGPT walkthrough, the gateway saves a prompt as a pending reply job, called a turn. The job needs one slot from the account's allowance and one from the worker pool for the selected model. Account A17 has 0 out of 1 slots used, and model pool P3 has 1 out of 2. If the gateway commits the job first and crashes, a worker can claim it without reserved capacity. If the gateway commits the reservations first and crashes, both slots can stay reserved with no job to run.

In PostgreSQL, BEGIN opens the unit and COMMIT makes its changes take effect. ROLLBACK discards the changes in an open transaction. Put the account update, the pool update, and the pending turn insert inside the same transaction. A commit saves the pending job and leaves A17 at 1 out of 1 and P3 at 2 out of 2. A rollback leaves the original counts and no pending job. The gateway sends 202 Accepted after the commit. PostgreSQL transaction blocks

This is atomicity, the A in ACID. A successful group takes effect as a whole. If it cannot complete, the database can discard the changes it made in the group.

State the rule that a commit must preserve

The capacity counts must stay within their limits. We can put a check constraint on a capacity row to require that the used count falls between zero and the row's limit. There is also an application rule across several rows. Each admitted turn has a reservation in the right account and pool. A check on a single count cannot enforce that relationship by itself. The gateway enforces it by checking capacity and writing the turn and reservations together. On completion, change the reservation from unreleased to released inside the transaction. Only the worker whose conditional update succeeds decrements both counts. Competing completions check the same row, so the second sees the completed release. A failure rolls back both the flag and counters. PostgreSQL check constraints

This is consistency, the C in ACID. The schema and transaction logic together preserve the rules that define a valid database state. The application has to choose those rules and make every writer follow them.

Make competing requests decide in order

Atomic writes still leave a question about what each transaction can read before it writes. Suppose a request from A18 arrives for the same model pool. If both gateways read P3 before either reserves it, they can both think they got the last slot. The decision to admit must happen against a count the other request cannot change until we finish.

The ChatGPT design locks the account's capacity row, then the selected pool's row. In PostgreSQL a row lock from SELECT ... FOR UPDATE makes another transaction that wants the same lock wait. A17 and A18 can hold their own account locks, but they compete for the same pool lock. If A17 acquires it first, its transaction sees P3 at 1 out of 2, reserves the last slot, and commits the pending job. At PostgreSQL's default Read Committed level, A18 can then lock the updated row, see P3 at 2 out of 2, and roll back without adding a job. PostgreSQL row locks · PostgreSQL Read Committed behavior

This is an isolation question, the I in ACID. Isolation determines which changes one transaction can see while another is running, and how their decisions interact. The lock puts one admission ahead of the other. Keep those row locks held only through the admission transaction. If the account lock must span a call to the rate limiter, the ChatGPT design gives that call a short deadline so a slow check does not hold the row indefinitely.

Know what the commit can survive

Durability, the D in ACID, is the promise that a confirmed commit survives the failures covered by the database configuration. With normal synchronous local commit, PostgreSQL flushes the write-ahead log (WAL), its record of changes, through the commit record before reporting success. It can use that log to recover changes that had not reached the table files when the server crashed. PostgreSQL write-ahead logging

The durability promise depends on where the acknowledged changes reached. PostgreSQL can report success earlier with asynchronous commit, before the local WAL flush. A crash can then lose the whole transaction. Under local synchronous commit, a separate replica can still lag behind the primary. Promoting a replica that did not receive the commit can lose it. For an accepted reply job, state whether the promise covers a database process crash, loss of the primary, or both. Recovery on the same machine needs durable local WAL. Recovery after losing the primary needs a replication and promotion policy that preserves the acknowledged commit. PostgreSQL asynchronous commit · PostgreSQL replication and failover

A timeout leaves the outcome uncertain

The database commits the reply job, but the browser never receives the gateway's 202. The browser cannot tell whether the job was saved. It sends the same parameters and client retry key R9 again. The gateway looks up R9 under A17 and returns the existing turn ID without reserving more capacity. The retry key, pending job, and both counter updates were saved in the same transaction.

A client retry can reach the gateway before the original transaction has finished. The ChatGPT design looks up the retry key again after locking the account row so the retry can see the original admission decision. Reusing R9 with different inputs returns a conflict. The database transaction gives the server one outcome. The retry key gives the client a way to learn that outcome.

External work has its own outcome

The rate gate in the ChatGPT design spends allowance in Redis before the PostgreSQL capacity decision. If the pool turns out to be full, the gateway rolls back the database transaction. The earlier Redis debit can still stand. That counter measures attempts that passed the rate gate, so the behavior fits its stated policy.

If another system needs a message for every committed turn, committing the turn and sending the message are separate failure points. A transactional outbox saves a record of the message with the turn in the same database transaction. A separate process sends it later and can retry when delivery is uncertain. It may publish the same event twice after a lost acknowledgment. Give each event a stable ID so the receiving service can apply its effect once. The transaction makes the obligation to send durable alongside the turn. AWS transactional outbox example

See also

Used in

  • ChatGPT commits the pending turn and capacity counts together, then applies one release when the model stops.
  • URL Shortener saves the new mapping and its idempotency record together.

Done reading? Mark it so it sticks in your dashboard.

Discussion