Concurrency in an LLD round 00:00:00 Make a prediction You will turn a two-caller interleaving into an invariant and decide which object owns the atomic operation. Gate A and gate B each read one free seat before either writes. How many success messages could an unprotected implementation display? Two, although only one seat exists. The safe operation must both check and claim the seat under a single owner. Each caller then observes either a successful claim or an already-claimed seat. 00:00:32 Saturday at the ice rink At the ice rink two families see one pair of skates on the shelf. Both cannot own the pair just because both saw it. A clerk actually taking the pair off the shelf makes ownership clear. Seeing a state is not claiming it. Put observation and update in an operation whose atomicity matches the real resource, then trace two overlapping calls. Concurrency means that several pieces of work are in progress during the same time. They may really run at the same moment on two processor cores, or take turns on one. For your design the danger is the same: between any two steps of one caller, steps of another caller may happen. A race gives no error. The code passes its tests, works in the demo, and sells one seat twice on a busy evening. So interviewers look for people who see it while reading, and not for people who know many lock classes. 00:01:25 Trace two gates and one seat One seat is available. Gate A and gate B both read the count before either finishes writing. Both can report success if checking and taking are separate operations. Place the decision and the change inside one atomic seat-claim operation. The seat owner serializes those claims. Walk the requests again. If A wins, B observes an already claimed seat. Reversing the order may change the winner but never the number of tickets. Gate A reads available and pauses. Gate B reads available and confirms. Now resume A. The critical question is whether its update checks the current shared state again. A conditional claim fails for A; a blind write may produce two successful receipts for one seat. 00:02:13 Avoid locking slow work A receipt printer can stall for seconds. Holding the seat lock while printing prevents unrelated callers from discovering the free seats. Record the successful claim under the lock, release it, then print from the recorded result. Define how to reprint after a printer failure. Claim the seat, release the inventory lock, and then disconnect the printer. The durable booking still exists and its receipt can be reprinted. Keeping the lock while waiting for the printer would block unrelated customers without making the printer or the seat more reliable. A single thread can still interleave async work across an await. Do not assume one thread means no competing state changes. Move slow printing outside the critical section, retaining an immutable ticket record so a later reprint does not claim another seat. Jakarta Servlet: one object, many requests. The specification says that a container may send concurrent requests through the service method, so developers must design servlets for several threads. A request field in a servlet is the classic hidden sharing. Android: one thread owns the screen. The documentation says the UI toolkit is not thread-safe: do not touch views from a worker thread, do it from the UI thread. Correctness by giving the data a single owner. 00:03:38 Try an interview follow-up How would you review a team's code base for such trouble? List objects that live longer than a request. For each, write down its fields, rules and guard. Anything without an answer is a finding. Then make it stay true: comments or annotations such as guarded-by, a race detector in tests, immutable types by default, and one owner per rule. Two app instances sell the last seat. Draw both requests until they touch something shared. A lock inside one instance cannot serialize the other. Enforce the seat transition where both writes meet, and tell the losing buyer the seat is gone. Use this trace to decide which object needs a concurrency contract. 00:04:29 What to take with you Concurrency questions in this round are about one sentence: between two steps of mine, somebody else may act. Look for fields that are shared and written, for a decision followed by a change, for waiting in a loop, and for things that can run out. Name the kind, then speak in four sentences: what is shared, which rule can break, which guard keeps it, what it costs. Show one unlucky order. Then stop, unless asked for more.