The Real Fullstack

The Dirty Read

The transfer was rolled back. So where did the €9,000 the loan check saw come from?

工程师7 分钟时间线模拟器代码挑战
sqlpostgresqlmysqlsql-server

此挑战尚未翻译为简体中文,你正在阅读英文版本。

任务:Choose an isolation level on MySQL that prevents the loan check from seeing uncommitted data, without paying for more isolation than needed.背景 · 学习目标 ▾

Pocketly's loan check reads the customer's balance while a large incoming transfer is still being verified. The transfer is later rolled back by fraud detection, but the loan was already approved. Years ago someone set the reporting connection pool to READ UNCOMMITTED “for speed”, and the loan service reused it.

任务: Choose an isolation level on MySQL that prevents the loan check from seeing uncommitted data, without paying for more isolation than needed.

学习目标

  • Define a dirty read.
  • Map isolation levels to the anomalies they prevent.
  • Know that engines differ (PostgreSQL never reads uncommitted data).
模拟模拟数据

运行模拟以查看输出。

运行模拟
-- loan-check service (MySQL / InnoDB)SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;   -- "faster dashboards", added years agoSELECT balance FROM account WHERE id = 7;                    -- may read another transaction's uncommitted write-- fix: READ COMMITTED (each statement sees only committed data)SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

简化说明

Relative costs are illustrative. In practice READ COMMITTED and REPEATABLE READ cost about the same in MVCC engines; SERIALIZABLE adds retries. 教学模型——数字用于说明行为,并非基准测试。

概念

相关挑战

参考资料