Skip to content
IT glossary
DatabasesUpdated December 3, 2026

Transaction

Транзакция

A database transaction groups one or more operations into a single unit. Either all of its changes are committed, or none of them are.

What a transaction does

Imagine an order service with two jobs: reserve an item and record who bought it. If it reserves the item but crashes before recording the order, the inventory is wrong. A transaction lets the database commit both changes together. It can also contain reads, and a transaction does not have to write anything at all.

Where the transaction starts and ends

In SQL, BEGIN typically starts an explicit transaction. COMMIT makes its changes permanent, while ROLLBACK discards its uncommitted changes. With autocommit enabled, successful statements outside an explicit transaction commit individually. Two statements sent one after the other are therefore not necessarily part of the same transaction.

ACID names four properties to keep separate. Atomicity means the changes commit as a unit. Consistency means a committed transaction preserves the rules the database and application enforce; the database cannot infer a rule you never specified. Isolation governs how concurrent transactions see and affect each other. Durability means committed changes survive the failures covered by the database's guarantees.

Example: reserving the last item

BEGIN;
UPDATE inventory SET quantity = quantity - 1
WHERE sku = 'P42' AND quantity > 0;
-- If no row was updated, roll back and skip the INSERT.
INSERT INTO orders (customer_id, sku) VALUES (7, 'P42');
COMMIT;

This is the success path, assuming sku uniquely identifies an inventory row. The application checks the affected-row count before inserting the order. Updating zero rows is not a SQL error, so the application must handle that case itself. The update checks stock as part of the write instead of trusting a value read by an earlier query.

Common traps

Transactions are useful when changes must agree, such as writing both sides of a transfer or creating a record and its dependent rows. Keep the transaction open only as long as needed: an open transaction can hold locks and make other requests wait. Do not assume a database rollback can undo an email or an API call; those side effects are outside the transaction.

What interviewers look for

Expect to trace two requests that both try to buy the last item. Explain why wrapping a read, a check, and a write in a transaction is not by itself enough at every isolation level. A conditional update avoids relying on a stale read. A strong answer also covers what the application does when the update matches no rows or when the database aborts the transaction with a retryable concurrency error.