You found the driver. K-Ring returned three candidates within 500 meters. You’re about to emit a
MATCH_CONFIRMEDevent. 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 theput(). 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 bydriverId, 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:
StreamThread-1processesRIDER_A→ K-Ring returnsDRIVER_X→ reads state:AVAILABLE
StreamThread-2processesRIDER_B→ K-Ring returnsDRIVER_X→ reads state:AVAILABLE(4ms later, before write commits)Both threads write
MATCHEDforDRIVER_XBoth emit
MATCH_CONFIRMEDdownstreamBoth 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.

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.