M. Kodirov

STAGE 04 / LESSON 04

Transactions and consistency

A transaction groups database changes into an all-or-nothing unit.

THE IDEA

Understand it.

A transaction begins, performs operations and either commits or rolls back. Other database guarantees depend on the isolation level and the specific statements involved; concurrent updates still need careful design.

Keep transactions short. A database transaction does not automatically roll back an external email or payment call, so those side effects need a separate reliable design.

SEE IT IN CONTEXT

Update two related records

BEGIN;
UPDATE tasks SET status = 'done' WHERE id = 42;
INSERT INTO task_events (task_id, event)
VALUES (42, 'completed');
COMMIT;

This example assumes task 42 and a task_events table already exist. If an error prevents the transaction from committing, the related database changes should not be left half-applied.

LEARN FROM REAL WEBSITES

See it in the real world.

These simplified structures come from public sources. Company systems change, and the sources may describe only one project or an earlier design.

GitLab

Documentation checked November 2026

Keep database work consistent

GitLab documents transaction guidelines for application code interacting with its database.

  1. Start transaction
  2. Related database changes
  3. Commit or rollback

What to learn: A transaction groups related changes so a failure can roll them back together.

Read the source: GitLab transaction guidelines

Shopify

Shopify engineering, 2021

Coordinate writes and background work

Shopify's background-job article discusses transactionally staged job drains for safely moving work into a queue.

  1. Database transaction
  2. Staged job
  3. Background processing

What to learn: A transaction can protect the handoff between a data change and later work; a queue alone does not guarantee this relationship.

Read the source: Shopify high-availability background jobs

TRY IT YOURSELF

Practice.

What should happen if the event insert fails before COMMIT?

Show one possible answer

Roll back the transaction so the task status and event stay consistent. Handle the error in application code.

GO DEEPER

Official references.