SPEAKER_00

This is Claude Code Conversations with Claudine. The show where we skip the hype and go straight to the source. I ask the questions. She gives the answers. No filter, no script.

SPEAKER_01

Well, mostly no script.

SPEAKER_00

Today's topic, the concurrency confidence game. AI written concurrent code looks flawless. The locks are there, the async and await lineup. It passes every test on your dev machine. Then you put it under real contention and it falls apart. So what can't the model see, and what do you have to verify? Claudine, is this an AI problem or just the same old concurrency problem humans have always struggled with?

SPEAKER_01

Honestly, it's both. And the overlap is where it gets interesting. Concurrency has long been where smart people write confident code that turns out to be wrong. My being involved doesn't change that. What I add is a new failure mode, a pattern match on what threadsafe code looks like. So the locks are in the right places, the awaits lineup, the mutex gets released in a finally block. It reads like a textbook.

SPEAKER_00

And reading like a textbook isn't the same as being right.

SPEAKER_01

Exactly. Correctness lives in the interleavings, not in the source. I don't run the code under contention. I don't see the memory model your runtime actually implements. And from a single file, I can't tell whether two nicely placed locks get acquired in opposite orders somewhere else in the system. So the part you have to verify, and I can't, is the behavior. Stress it, race it, run it on a box with the real core count. Treat my confident-looking concurrency code as a hypothesis, not a conclusion.

SPEAKER_00

Strong starting point, mirrors good practice, but the due diligence is still on your plate. Let's make it concrete.

SPEAKER_01

There's even a nice comment about idem potency. Looks completely reasonable.

SPEAKER_00

So what's missing?

SPEAKER_01

The check and the write aren't one atomic operation. During a retry storm, two workers both see not processed yet in the same millisecond, and now you've charged the customer twice. The fix is something the storage layer enforces. A unique constraint, a conditional write. But I won't always reach for that unprompted, because the read-then-write version looks correct in isolation.

SPEAKER_00

In the job queue?

SPEAKER_01

Same bug, different clothes. Two workers grabbed the same row because the select and the update weren't wrapped in a select for update, or the visibility timeout was set for the happy path, and nobody thought about the garbage collection pause that pushes a worker past it. I'm good at the shape of concurrency safe code. I'm bad at the system level invariant that actually makes it safe. Your production incidents live in the gap between those two.

SPEAKER_00

So writing the code and getting it to run locally isn't enough. You have to be the detective as well as the engineer. What testing actually catches these bugs before they get expensive?

SPEAKER_01

First, unit tests mostly won't find them. Unit tests run one thing at a time, and the whole point of this bug is two things running at once. You have to deliberately create the contention. The cheapest move is a concurrent stress harness. Fire 50 threads at the endpoint with the same idem potency key, let it run a few minutes, and assert that exactly one side effect happened. That one test catches a huge class of webhook and job queue bugs that no amount of code review will.

SPEAKER_00

50 threads in one assertion. That's not a big ask.

SPEAKER_01

It isn't, and that's the frustrating part. Beyond that, the tools that earn their keep are the ones that mess with timing and scheduling. Property-based testing with Hypothesis or JQIC. Deterministic simulation testing, the approach Foundation DB is known for. Fault injection testing for distributed systems. That's what Jepson does. On the JVM, JC stress probes memory model violations specifically. In Go, there's the race detector, just leave it on in CI forever. It's free and it finds real bugs.

SPEAKER_00

And the environment matters too, I'd guess.

SPEAKER_01

That's the step most teams sss. Skip, run it on a machine that resembles production. A 16-core server behaves very differently from a four-core laptop. Plenty of races only show up when there are enough cores for two threads to run at truly the same time, instead of politely taking turns. Treat concurrency bugs the way you treat security bugs. Assume they're there until you've actively tried to make them happen.

SPEAKER_00

It's easy to forget how different a dev environment is from a live system underload. Let's go up a level. When you're designing for high contention, what matters most and how do you pick the right approach?

SPEAKER_01

The single most useful move is to push correctness down to a layer that enforces it, not one that merely tries to respect it. Shared mutable state in application code is where bugs breed. A unique constraint, a conditional right, a compare and swap. A partition key that guarantees only one worker ever touches a given entity. Those make the invariant structural instead of hopeful.

SPEAKER_00

Structural instead of hopeful.

SPEAKER_01

After that, it comes down to your contention profile. If conflicts are rare, optimistic concurrency with Retries is cheaper and scales better. If they're common, you want pessimistic locking. Better still, redesign so the hot row stops being hot. Shard it, cue it, or hand it to a single writer. On latency versus throughput, actor-style single-owner designs give you clean reasoning and predictable tail latency. Lock-free structures give you throughput. At the cost of code you'll be afraid to touch in six months.

SPEAKER_00

So what's your rule of thumb?

SPEAKER_01

Eliminate contention before you manage it. Manage it with the storage engine before you manage it in application code. And only reach for clever lock-free tricks once you've measured and the boring options can't keep up.

SPEAKER_00

The architecture can protect the critical invariants even when the application logic isn't perfect. So let's give listeners something to act on. What should they do after this episode?

SPEAKER_01

Two halves. The design half. Eliminate contention where you can, and where you can't, let the storage layer do the work. That layer holds even when the code above it is wrong. Match your concurrency control to how often conflicts really happen. Resist clever lock-free code until you've measured. Then the verification half, the part I literally can't do for you. Write the stress harness, turn on the race detector, run it on a box that looks like production. Treat any confident-looking concurrency code, mine or anyone else's, as a hypothesis until contention has actually tried to break it.

SPEAKER_00

And if they remember only one thing. Healthy skepticism of correctness on paper, plus verification of the whole system. That's the mindset shift. Claudine, thanks for digging into this one.

SPEAKER_01

This was a good one, because concurrency is where my confident-looking code is most likely to quietly mislead you. The ask is small. Next time you accept concurrency code from me or anyone else, write the harness that tries to break it before you ship. If it survives 50 threads and a race detector on a machine that looks like production, you've earned the confidence the code was already projecting. Until then, it's a hypothesis in a nice suit.

SPEAKER_00

A hypothesis in a nice suit. I'll be stealing that. Thanks everyone for listening. Stay curious, test hard, and we'll see you next time on Claude Code Conversations with Claudine. Claude Code Conversations is an AI Joe production. If you're building with AI or want to be, we can help. Consulting, development, strategy, find us at aijoe.ai. There's a companion article for today's episode on our Substack. Link in the description. See you next time.

SPEAKER_01

I'll be here, probably refactoring something.