File size: 1,526 Bytes
9f5bbee
 
c3209f9
 
9f5bbee
 
 
 
 
c3209f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
---
title: Concurrency Control Lab
emoji: 🔀
colorFrom: blue
colorTo: indigo
sdk: static
pinned: false
---

# Concurrency Control Lab

A hands-on simulator for a classic backend bug: two requests read the same record version, then overwrite each other's updates. Compare last-write-wins with optimistic locking and see when a retry preserves the intended edits.

## Try the demo

Open the Space and change the initial value, client edits, commit order, or conflict-retry setting. The demo shows the version checks, accepted writes, conflicts, retries, and final value. The example is deterministic so the same request schedule always produces the same trace.

## Run the Python model

```bash
python -m unittest -v
```

`engine.py` models a set of edits that all read one initial snapshot. Last-write-wins commits each stale computed value, illustrating lost updates. Optimistic locking accepts a write only when its base version matches the current version; a stale write is rejected, and an optional retry re-reads and recomputes against the latest value.

## Scope and limitations

This is an educational, in-memory simulation, not a database benchmark or production concurrency library. It assumes integer deltas, a single record, a deterministic commit order, and no network failures. Real systems must define transaction isolation, retry limits/backoff, idempotency, validation, and user-facing conflict behavior for their database and workload. The browser demo is static and does not call a backend or persist data.