Concurrency Catalog
Canonical Scenarios & Fixes
Inspect 10 concurrency failure scenarios based on academic isolation literature and real-world financial/distributed systems.
Crypto Arbitrage Circular Info
Cross-exchange arbitrage loop observes cyclic dependency edges, leading to phantom profit execution.
Cross-exchange arbitrage loop observes cyclic dependency edges, leading to phantom profit execution.
Schema and seed data
-- Schema
CREATE TABLE orderbooks (
id INT PRIMARY KEY,
exchange TEXT NOT NULL,
pair TEXT NOT NULL,
ask_price INT NOT NULL,
version INT NOT NULL
);
-- Seed
INSERT INTO orderbooks VALUES (1, 'Binance', 'BTC/USDT', 64000, 1), (2, 'Coinbase', 'BTC/USDT', 64200, 1);Concurrent workload (chaos.yaml)
version: "1.0"
name: "crypto_arbitrage_circular"
operations:
- name: "arb_bot_1"
steps:
- "SELECT ask_price FROM orderbooks WHERE id = 1 -> p1"
- "UPDATE orderbooks SET ask_price = {p1 + 50}, version = version + 1 WHERE id = 2"
- name: "arb_bot_2"
steps:
- "SELECT ask_price FROM orderbooks WHERE id = 2 -> p2"
- "UPDATE orderbooks SET ask_price = {p2 - 50}, version = version + 1 WHERE id = 1"
invariants:
- name: "monotonic_versioning"
query: "SELECT sum(version) AS total_ver FROM orderbooks;"
assert: "total_ver <= 100"Why the invariant breaks
Bot 1 reads Binance price and modifies Coinbase. Concurrently, Bot 2 reads Coinbase price and modifies Binance. The circular information flow creates a G1c cycle violating causal order.
T1 ──(wr)──► T2 ──(wr)──► T1Delta debugging reduces 30 operations to the 2 that cause the failure.
Production fix
Optimistic Concurrency Control (OCC) with Versioning or Canonical Locks
Anomaly G1c demonstrates cyclic read-write dependencies across distributed pairs. To preserve global linearizability and prevent execution on stale price spreads, applications should implement OCC with monotonic version numbers or acquire explicit FOR UPDATE locks in canonical primary key order across order books before evaluating arbitrage margins.
PostgreSQL, MySQL (InnoDB), CockroachDB
-- Optimistic Concurrency Control (OCC) with Monotonic Version Check
BEGIN;
SELECT id, ask_price, version FROM orderbooks WHERE id IN (1, 2) FOR UPDATE;
-- Validate profit spread and execute conditional update with version check:
UPDATE orderbooks
SET ask_price = :new_price, version = version + 1
WHERE id = :exchange_id AND version = :expected_version;
COMMIT;