Picking a Guard Against Race Conditions — Atomic, Lock, Retry, Serializable

Picking a Guard Against Race Conditions — Atomic, Lock, Retry, Serializable A workflow diagram generated by Archify. 01 / Decision 02 / Atomic Operations 03 / Explicit Locking 04 / Detect and Retry Classify the race Apply the guard Race Condition Found · concurrent read-modify-write · Decision › Classify the race · goal: safe writes Race Condition Found concurrent read-modify-write goal: safe writes Expressible as Atomic Op? · increment, append, CAS · Decision › Classify the race · decide Expressible as Atomic Op? increment, append, CAS decide Rows Known in Advance? · you can lock what you read · Decision › Apply the guard · decide Rows Known in Advance? you can lock what you read decide Conflict Auto-Detected? · database aborts the loser · Decision › Apply the guard · decide Conflict Auto-Detected? database aborts the loser decide Serializable Isolation · SSI blocks write skew · Decision › Apply the guard · materialize conflicts last Serializable Isolation SSI blocks write skew materialize conflicts last Atomic Write or CAS · UPDATE ... value = value + 1 · Atomic Operations › Classify the race · best option Atomic Write or CAS UPDATE ... value = value + 1 best option Explicit Locking · SELECT ... FOR UPDATE · Explicit Locking › Apply the guard · watch for deadlocks Explicit Locking SELECT ... FOR UPDATE watch for deadlocks Detect and Retry · tx aborts, app retries · Detect and Retry › Apply the guard · no special code Detect and Retry tx aborts, app retries no special code yes no yes no yes no concurrent write Legend Agent logic Context / trace External system