Concurrency & Parallelism
Transcript
Made automatically from the video's sound, so it may contain mistakes. Click a line to play from there.
Right, let's jump straight into this explainer. If you've been following along, we're picking up exactly where we left off last time. In our previous session, we saw exactly why wall clocks absolutely cannot order events across different machines.
Well, today, with zooming in, we're gonna look at how interleaved execution completely shatters state ordering inside a single service. We're talking about what happens when multiple things try to happen at the exact same millisecond in your code, and why this is, quite frankly, one of the hardest problems in computing.
You know, as developers, we often rely on this false comfort of beautifully structured code, don't we? We just assume it executes top to bottom, nice and neatly, but we forget that interleaved execution creates what Jim Gray famously called Heisenbugs.
These are nasty race conditions that completely break state ordering, but then, almost like magic, they just vanish the second you attach a debugger or chuck in a print statement. It gives us this absolute illusion of order that just shatters the moment your system faces real-world traffic.
Here's the roadmap for today. One, concurrency is not parallelism. Two, the single-threaded async trap. Three, memory models and the metal.
And finally, four, the pragmatic playbook. Right, part one. Concurrency is not parallelism. This brilliant quote from Rob Pike's Wazza presentation perfectly separates these two ideas.
Concurrency, you see, is a property of your program's structure. It's how you actually design your code to deal with overlapping, interleave tasks. Parallelism, on the other hand, is the physical hardware reality of executing those computations simultaneously across multiple CPU cores.
So you can have concurrency without parallelism. And realizing that distinction, well, it changes absolutely everything about how we manage state. Now, what's really fascinating here is how different ecosystems map this out.
Take Node.js. It uses a single-threaded cooperative event loop via LibUV, where just one heavy synchronous regex can literally block every single client. Then you've got Go, giving us M to N green threads that isolate process heaps.
Rust tackles the whole thing at compile time with its strict send and sync ownership rules. And Python, well, they're currently shaking things up with PEPS 703, making the global interpreter lock optional for true free threading.
The single-threaded async trap. And trust me, beware talk to. So many of us fall into this vibecoder trap. We think, hey, I'm on a single-threaded event loop.
I must be immune to race conditions. No way. Enter TOCTUE. Time of check, time of use.
This is where you check a balance, your task suspends to await some network input, and before it even resumes, a totally different concurrent task sneaks in and mutates that exact same value. Let's put this into perspective with the recent port swigger HTTP2 single-packet attack.
Imagine an attacker sends 10 concurrent coupon requests in just one single packet to a node or fast API server. All 10 coroutines suspend the exact same await-fetch user line. All 10 read the credit balance as 100, and then all 10 deduct from it.
A single thread just authorized massive fraudulent transfers, or because asynchronous code is inherently not atomic. It's wild because innocent looking code completely hides this vulnerability. Think about a simple X increment.
It looks like one quick step, but at the instruction layer is actually three distinct actions: read, compute, and write. When you interleave those without locks, data just gets lost. And this isn't just theory.
This exact lack of hardware atomicity was at the very heart of the infamous Therac 25 radiation overdose disaster. Memory models and the metal. Let's talk hardware optimizations.
To truly understand why this chaos happens, you have to look down at the hardware. To maintain blistering speeds, both your compiler and your hardware, well, they cheat. As Russ Cox points out in his memory models series, architectures like x86 use total store ordering, while ARM relies on relaxed reordering.
They will aggressively shuffle your ordinary reads and writes behind your back. That means those naive Boolean flags you're using for synchronization, they will completely break on modern metal. And this is exactly where DRFSC comes to the rescue.
Data race free sequentially consistent execution. It basically means if you use proper synchronizing atomics, like mutexes, your program is considered data race free. You are forcing the compiler and the hardware to guarantee that your code will execute predictably, exactly as if it were safely and sequentially interleaved on a single processor.
But honestly, this gets incredibly complex when you go deep into systems programming. Just look at the Linux kernel. A standard spin lock on a non real-time kernel simply disables preemption to guarantee exclusivity.
But put that on a real-time kernel, it maps that exact same spin lock to a per CPU lock, keeping preemption enabled so the system doesn't block critical tasks. It's the exact same code, producing totally different synchronization behaviors depending on the metal it's running on.
Right, let's wrap this up with part four the pragmatic playbook for bulletproof systems. So how do we actually survive this? Here's the checklist.
First, leverage ownership systems to catch races at compile time if your language allows it. Second, aggressively run tools like Go's race detector or LLVM's thread sanitizer in your test pipelines.
Third, always utilize thread safe primitives. And finally, bound your concurrency. Seriously, beware of unbounded promise.org loops exhausting your single threaded event loop.
Don't block it with heavy tasks. Another massive risk is unprotected global variables. A classic trap for vibecoders is caching state in a shared global dictionary in FastAPI or Express.
Concurrent rights there will literally cause memory corruption and crash your app. If you absolutely must share state in memory, you have to use channels, mutexes, or atomic operations to load and store safely. For the front end, make sure you wire up an abort controller to cancel overlapping fetch requests and stop stale data overwrites.
On the back end, never just slap an in-process mutex onto a multi-container web app. It won't work. Instead, push that atomicity down to where it actually belongs. A single SQL update statement with a returning clause.
Let the database handle the locks. So by understanding execution models, memory barriers, and atomic operations, we've successfully secured a single service. The single core is fixed.
Alright, this leaves us with a massive, somewhat terrifying question. What happens to our data state when it's split across entirely different distributed databases that literally cannot share a mutex? How on earth do we prevent data collisions then?
Well, that is exactly the puzzle we will be tackling next time in our explainer on distributed consensus. Thanks for joining me, and I'll catch you in the next one.