TxTransactions Lab
GitHub
II · Implement isolation

Correct, but waiting.

A single lock makes the service easy to reason about. It also makes independent customers wait behind one another.

You will: compare lock granularity under explicit assumptions and preserve the scope of the invariant while improving concurrency. Prerequisites: deadlocks and lock ownership. Runtime: a teaching model. Its assumptions appear before the scenario controls.

Concept diagram · before the experiment

Lock scope determines who must wait

A global lock makes unrelated requests form one queue. Per-key locks let requests for x and y run independently, while requests for x still serialize. A rule spanning x and y cannot be protected by unrelated per-key decisions alone.
Read the diagram: Read each lane as a lock ownership interval, not a measured benchmark. Finer locks improve concurrency only when the invariant can safely be protected at that granularity. Shorter lock hold time also reduces the queue. Open full diagram

Predict before running

Eight requests each need three logical service ticks. With one global slot, how many ticks are required? What changes if four independent keys can proceed together?

Control the experiment

The page loads Baseline with eight requests, four keys, three logical ticks per batch, and a global lock. Select Run to end and find slots and clock in Values at this step.

Synthetic logical ticks with equal service times and one request per key per batch. These are model results, not measured throughput. Per-key locking is valid only for independent invariants.

Edit the experiment

Change the JSON settings and select Step or Run to end. Restore scenario reloads the selected preset.

Select Step to inspect the initial state.

    Inspect the evidence

    The baseline processes one request per batch. Inspect completed, waiting, slots and clock. These are synthetic logical ticks, not elapsed measurements. The repaired scenario permits one simultaneous request for each independent key, up to the configured number of keys.

    Change one thing

    Choose Repair and select Run to end. Compare slots and clock with Baseline. Open Edit the experiment, change keys from 4 to 1, then select Run to end again. Does per-key locking still save any logical ticks? Keep the request count and service ticks fixed so the key count explains the change.

    Calculate batches and logical ticks

    The modeled completion time is ceil(requests / slots) × service. The arithmetic is meaningful only under the stated independence and scheduling assumptions.

    Check the scope of the lock

    A lock per account does not protect a bank-wide limit unless all relevant changes also coordinate over that limit.

    Why this result occurs

    The global design preserves counting but finishes the default workload in 24 ticks. Four independent slots finish it in 6 ticks. This is a model of available parallelism, not evidence of a fourfold production speedup. Real waiting also depends on skew, lock duration, I/O, retries, fairness and scheduling.

    Under the hood: lock hold time becomes a queue

    The lock table turns incompatible requests into waiting work. A transaction that retains ownership across a network wait occupies a scarce resource even if its CPU usage is zero. Queue length and waiting latency can grow while every committed result remains correct. A finer lock key reduces interference only when it still covers the complete invariant.

    In the lock-manager engine, follow the queue head and the release/wakeup path. A real throughput experiment must separately account for lock hold time, scheduler and I/O delay, validation aborts and retry work. MVCC can let readers proceed on old versions, but it does not eliminate every writer conflict or long-snapshot retention cost.

    Transfer the idea

    Design a workload where every request targets the same product. Explain how you would measure lock wait, useful completions, abort rate and p95 latency. Decide whether shorter transactions, admission control or a different invariant representation can help.

    Success criteria: derive batch count and logical ticks from the displayed configuration; explain keys = 1 and a hot-key counterexample; preserve the invariant’s scope and identify production measurements the arithmetic omits.

    Primary reference: PostgreSQL lock monitoring.

    Your design record

    Explain what happened and cite the run or SQL result that supports it.

    Write your design notes

    Download notes and evidence

    Downloads include current editor text. Run evidence stays on this page until you leave.