On this page

Concurrency in LLD

Almost every low-level design interview has a moment when the interviewer leans back and asks, "What happens when a thousand users do this at the same time?" The parking lot hands the same spot to two cars. The movie booking system sells seat A7 twice. The rate limiter lets through twice its quota. The logger stalls every request because the disk is slow. None of these bugs is visible in a single-threaded walkthrough, and all of them are what interviewers at mid-level and above are listening for.

This module teaches concurrency as a design skill, applied inside the object models you build in the rest of the course. It doesn't re-derive mutexes and semaphores from first principles; the System Design concurrency section does that, and the lessons here link to it wherever the theory matters. The question in this module is always the practical one: given this class and these invariants, what's shared, what can race, and what's the simplest design that stays correct?

What's in this module

The module has three lessons. Read them in order: each builds on the vocabulary of the one before.

1. Thread-Safe Class Design

What "thread-safe" means for a class, and why it's about invariants that span several fields rather than about individual fields. The lesson covers the four strategies in the order to consider them (confine, make immutable, delegate to a thread-safe class, synchronize), then how to choose between synchronized, ReentrantLock, atomics and concurrent collections, and how to pick lock granularity, including lock striping. The two bugs behind most incidents, check-then-act and read-modify-write, each get a failing demo and a fix. It ends with visibility and safe publication, how to document a class's thread-safety policy, and how to test concurrent code with stress tests, start gates and jcstress. Java leads throughout, with Python and Node versions and notes on how the GIL, asyncio and Node's event loop change the picture.

2. Concurrency Patterns in LLD

The recurring shapes of concurrent code inside LLD problems, each mapped to the problems that use it: producer–consumer with a BlockingQueue (the async logger), delay queues (the task scheduler), worker pools and bulkheads (the notification service), read-write locks and copy-on-write snapshots (config registries and caches), double-checked locking done correctly and holder-idiom singletons, CompletableFuture composition with timeouts (checkout aggregation), thread-per-request against virtual threads, a rate-limited worker built on a token bucket, graceful shutdown, and per-subscriber queues for pub-sub. It closes by assembling several of them into a notification service design.

3. Booking & Inventory Concurrency: Preventing Double Booking

The most common concurrency follow-up in LLD rounds, treated in depth. The lesson shows the double-booking race on a timeline, then climbs a ladder of fixes: in-process (a lock per seat or per show, ConcurrentHashMap claims, compare-and-set), seat holds with expiry as a hold → confirm → release state machine, and database fixes (SELECT … FOR UPDATE, version columns, unique constraints as the last line of defence, idempotency keys). Flash-sale inventory gets the atomic UPDATE … WHERE stock > 0, plus what to do when one row gets too hot. It finishes with a complete movie-seat booking service in Java, Python and TypeScript, whose concurrency test proves exactly one winner per seat. The database samples were run against PostgreSQL.

How the lessons fit together

flowchart LR
    T["Thread-safe class design<br/>invariants, locks, atomics"] --> P["Concurrency patterns<br/>queues, pools, futures, shutdown"]
    T --> B["Booking and inventory<br/>holds, CAS, SQL locking"]
    P --> B
    B -->|"apply in case studies"| C["Movie booking, hotel,<br/>parking lot, rate limiter"]
    S["System Design concurrency<br/>theory and primitives"] -.->|"background"| T

The first lesson gives you the reasoning: find the invariant, find who shares it, choose the cheapest protection. The second gives you the building blocks that hide most of the locking inside well-tested library classes. The third applies both to the domain where interviewers press hardest, and adds the step that in-memory designs can't take on their own: moving the guarantee into the database once the service runs on more than one machine.

The theory these lessons build on

When a lesson here needs a concept in more depth, it links to these pages instead of repeating them:

PageWhat it coversUsed in
Race ConditionsData races, non-atomic operations, visibility, check-then-act, the GIL's false safetyLesson 1
Mutex, Semaphore & MonitorThe basic primitives, condition variables, read-write locks and spinlocksLessons 1 and 2
DeadlocksThe four Coffman conditions, lock ordering, tryLock with timeouts, livelock and starvationLessons 1 and 3 (multi-seat locking)
Lock-Free ProgrammingCAS loops, progress guarantees, the ABA problem, memory orderingLessons 1 and 3 (CAS seat claims)
Classic Synchronization ProblemsBounded buffer, readers–writers, dining philosophers, barriers, each proved from first principlesLesson 2 (producer–consumer, read-write locks)
Concurrency PatternsThe short catalogue: producer–consumer, thread pools, work stealing, actors, barriers, rate limitersLesson 2
Actor ModelSingle-owner state behind a mailboxLessons 1 and 2 (confinement)
Concurrency Interview ProblemsPractice problems to try after this moduleAfter the module

You don't need to read them first. If a lesson uses a term you haven't met, follow the link, then come back.

Before you start

The module assumes you're comfortable with:

  • classes, interfaces and composition (Object-Oriented Programming);
  • the LLD interview framework, which puts the concurrency discussion after the core code, and the "ignoring concurrency" entry in Common LLD Mistakes;
  • reading Java. Every lesson leads with Java, because its memory model and java.util.concurrent give names to every idea here, and then shows Python and TypeScript where the runtime model changes the answer.

Interview questions

  1. "What happens when many users call this at once?" Answer in five steps, for any design. Name the shared state ("each show's seat map"). Name the invariant ("a seat is free, held by one hold, or booked"). Name the race ("booking is check-then-act"). Choose the protection and its granularity ("one lock per show, since no invariant spans shows"). Then say where it stops working ("with several instances, a conditional update in the database enforces it, with a unique index behind it").
  2. "Isn't a ConcurrentHashMap enough?" It's enough when every operation is a single atomic map call such as putIfAbsent or compute. It isn't when an operation reads and then writes, or touches several keys: those sequences need a lock or a conditional write.
  3. "When do you move concurrency control into the database?" As soon as more than one process can change the state. In-memory locks protect one process only.

Each lesson ends with more questions that rehearse these answers in different settings. After the module, practise them on the case studies, starting with movie booking.