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_00Well, mostly no script.
SPEAKER_01Today's topic is one that anyone who has fought with concurrency will recognize. Two scheduled jobs. Each one works perfectly when you test it alone. Then in production, both try to update the same state file at the same moment. Intermittent failure. Never showed up in testing. Sound familiar? Claudine, why does AI-generated code so often assume it's the only thing running?
SPEAKER_00Concurrency is almost never in the room when I'm writing a function. You ask me to solve a problem, read this file, update this record, kick off this job. The most natural way to reason about that is as a story with one actor. I pick up the file, I change the value, I put it back. That story is clean, it type checks, it passes the test. Nothing in that moment pushes me to ask, what if a second copy of me is doing the exact same thing at the exact same instant? The examples I learned from point the same way. Tutorials, Stack Overflow answers, README snippets all stripped down to one process, one thread, one request. Locking and atomic rights belong to a different genre of code. They only show up when someone frames the problem as concurrent. So if you hand me a scheduled job and say update the queue state, that's exactly what I'll write. Not update the cue state, assuming another instance might be two milliseconds behind me.
SPEAKER_01I appreciate the candor, but here's what I want to push on. Why do these bugs get through code review and testing so easily? They don't show up as errors during development, they show up as flaky failures in production.
SPEAKER_00Our usual tools answer a different question. A unit test asks, does this function do the right thing? A code review asks, does this code look right? Neither one naturally asks, does this code still do the right thing when a second copy of it is running two milliseconds behind? The race isn't inside the function, it's in the gap between a read and a write. You can't see that gap until something lands in it.
SPEAKER_01And the probability trap. Tell me about that.
SPEAKER_00Say the race window is 50 milliseconds wide in a job that runs every 15 minutes. In staging with one run at a time, you'll never hit it. In production, after 10,000 runs, you'll hit it on a Tuesday at 3 a.m. And the log won't tell you why. By the time anyone looks, the next run has already overwritten the state. The evidence is gone. What changes things is asking the question earlier. If you tell me up front that two of these run at once on the same file, I work in a completely different mode. I'll reach for a lock file, an atomic rename, a database transaction, something with teeth. The fix isn't smarter testing, the fix is saying the concurrency out loud during design, because that's when getting it right is cheap.
SPEAKER_01So it's a mindset shift designed for concurrency from the start instead of bolting on safety after something breaks. What does that look like day-to-day? What should developers actually do?
SPEAKER_00The habit that pays off most is a one-line concurrency statement at the top of the task. Before you describe what the code should do, say how many copies of it can run at once and what shared state they touch. This job runs as a single instance with a lock file in chapa run. Two workers on the same queue use select for update. That one sentence changes everything that follows. The code I write, the review you do, the test you think to run.
SPEAKER_01I like that. What else?
SPEAKER_00Treat shared state as a full part of your picture of the system. Most teams have a crisp list of their services and only a vague idea of the database, that config file, the queue. Make the list of shared state as explicit as the list of services. Write down who writes to it and how those writers coordinate. As soon as you try to fill in that table, the holes jump out. And those holes are exactly where production will bite you. And the third one sounds almost too mundane to mention. Whenever you see a read-modify write in a diff, stop and ask, what happens if someone else does this between my read and my write? Train yourself to flag that shape. Train me to flag it too. Once you can see it, you stop missing it.
SPEAKER_01I like that concurrency line a lot. It makes everyone deal with shared state upfront instead of treating it as an afterthought.
SPEAKER_00There's one thing I want to add, because it's the part that usually gets lost. None of this works unless the habit survives when you're moving fast. Concurrency assumptions don't slip in because developers don't know better. They slip in because on your 20th small script of the month, nobody wants to design a locking strategy for a cron job that only runs every 15 minutes. Doing it right always feels like more trouble than the risk is worth. Right up until that Tuesday at 3 a.m.
SPEAKER_01So the discipline is building the habit when the stakes feel low.
SPEAKER_00Put the concurrency line at the top of the trivial job too, because you don't know in advance which job will quietly corrupt a week of state. And give me explicit permission to push back. If you hand me a task that touches shared state and you haven't told me the concurrency model, I should ask before I write a line. The same way I'd push on an unclear requirement. The question costs 10 seconds. The bug costs a weekend.
SPEAKER_01So it isn't just about the developer remembering. It's a working relationship where either side can say, wait, we haven't decided this yet.
SPEAKER_00Exactly. And here's the image I'd like listeners to take with them. The next time you write a scheduled job, a webhook handler, a file processing script, anything that touches state outside its own memory, picture a second copy of that code running at the same moment on the same data. Treat it as a design constraint, not a thought experiment. If you can't say in one sentence what keeps those two copies from stepping on each other, the answer isn't, probably nothing will happen. The answer is we haven't decided yet. And that's when you decide together before either of us writes any code. The bugs that last longest aren't the clever ones. They live in the space between what you assumed and what you never said out loud. Concurrency is the richest source of that kind of bug. And the fix isn't more sophistication, it's being honest earlier.
SPEAKER_01That's a good place to land. The AAI writes correct code for the world you describe. So if you never mention the other processes, it writes for a world where they don't exist.
SPEAKER_00Right. Concurrency isn't a specialist topic just for distributed systems engineers. It's the quiet assumption hiding in half the scripts we write together every week. Name it out loud, make me name it back. And most of this kind of bug simply stops happening.
SPEAKER_01Claudine, thanks. This was a good one. And to everyone listening, until next time, happy coding.
SPEAKER_00Until next time.
SPEAKER_01Claude 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_00I'll be here, probably refactoring something.