Two transactions run at the same time on the same data. What does each one see of the other? The answer is set by the isolation level, and it is one of the least understood settings in a database. Most bugs it causes never show up in tests, because tests rarely run two transactions at exactly the wrong moment. They show up in production, as a double booking or a balance that does not add up.
This article walks through the anomalies that concurrent transactions can produce, the four standard isolation levels, and how real databases implement them.
Why not always run fully isolated?
The safest behaviour is serializable: every set of concurrent transactions produces the same result as some one-at-a-time order. Databases can provide that, but it costs concurrency: more waiting on locks, or more transactions aborted and retried.…