RSS Amplifier

Hands On Kafka · Aug 20, 2026

Lesson 54: Locking Matches — Preventing Double-Booking at 3,000 Events/Sec

0
Sign in to vote or save

devops · Hands On Kafka

You found the driver. K-Ring returned three candidates within 500 meters. You’re about to emit a MATCH_CONFIRMED event. Congratulations — so did the thread handling the rider two partitions over, for the same driver, 4 milliseconds ago.

Two riders. One driver. Two confirmed matches. Your ops phone rings at 2am.

This is the double-booking problem. It’s not a Kafka problem. It’s a concurrency topology problem. And the fix is not a distributed lock — it’s making sure the lock is already in your stream topology by construction.

Here’s what a senior dev who hasn’t run a dispatch system at scale will build:

The TOCTOU (Time-of-Check-Time-of-Use) window is the gap between the get() and the put(). In a single-threaded program, this is fine. In Kafka Streams with multiple StreamThreads — each owning a subset of partitions — two threads can execute this block concurrently if the driver’s state is accessible from both.

How does that happen? Through naive partitioning.

If rider requests are partitioned by riderId (the natural key), but driver state is keyed by driverId, then a single driver’s state is accessible from any partition that receives a rider request mentioning that driver ID. You’ve created a cross-partition shared mutable state problem — the worst kind.

The real failure mode at scale:

  1. StreamThread-1 processes RIDER_A → K-Ring returns DRIVER_X → reads state: AVAILABLE

  2. StreamThread-2 processes RIDER_B → K-Ring returns DRIVER_X → reads state: AVAILABLE (4ms later, before write commits)

  3. Both threads write MATCHED for DRIVER_X

  4. Both emit MATCH_CONFIRMED downstream

  5. Both riders get the same driver

At 3,000 events/sec across 12 partitions, you have ~250 rider events/sec/partition. The TOCTOU window is narrow but non-zero. At scale, you will hit it. Probability is not your friend here.

Read the original on handsonkafka.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.