Highlights

Contents

BACKEND ENGINEERING / UNIT IX / FIELD GUIDEConcurrency anddistributed locksThreads, event loops, races, leases and leaksone cook, many panstoken 34the lock is only as goodas its fencing tokenRuntimesthreads, loops, goroutinesRacesmutexes, atomics, CASLocksleases, fencing, RedlockOverloadbackpressure and leaks
Before we start

How to read this chapter

A backend is a crowd of requests sharing a handful of cores, connections and rows. Concurrency is the discipline of letting them share without corrupting what they share, and without letting one slow member of the crowd stall everyone else.

The unit works outwards. The first three parts stay inside one process. They cover how processes, threads, event loops and coroutines run many requests at once, how pools are sized, and how races, mutexes, semaphores and compare-and-swap behave in memory. Parts IV and V cross process boundaries, first with techniques that need no lock at all and then with distributed locks, leases, fencing tokens and the Redlock debate. Part VI handles overload, where more work arrives than can be done, and Part VII handles leaks, where resources go in and never come out. Parts VIII and IX open the Node.js and Go runtimes, which interviews increasingly ask about directly, and Part X follows one request through an instance.

Each section starts from a Wren situation, explains the mechanism, works the numbers and then shows a figure. Picture this boxes give analogies, notes add detail, Watch out names real mistakes, and Interview lens gives an answer to say aloud. In one breath closes each part.

By the end you should be able to explain why a lock in one process does not protect a row shared by four, why SET NX PX alone is not a safe lock, where the extra 40,000 requests per second go when only 10,000 can be served, and what happens inside Node when a request does 200 ms of CPU work.

The running system

Meet Wren

Wren is the illustrative food-ordering service from earlier units. These are the numbers this unit uses. All are assumptions chosen for readable arithmetic, not measurements.

Setting Value What it controls
Traffic per instance at peak 500 requests per second, 50 ms mean latency Requests in flight
Cores per instance 8 Thread pool sizing
Typical request 10 ms of CPU, 40 ms waiting on I/O Wait-to-compute ratio
Instances 4 normally, 20 at peak Locks across processes
Kitchen partner API 100 concurrent calls allowed Semaphores
Report lock lease 30 s, renewed every 10 s Distributed locks
Overload scenario 50,000 requests per second offered, 10,000 served Backpressure
File descriptor limit 65,536 per process Resource leaks
Password hash 100 ms of CPU Node’s thread pool

The examples follow Wren’s order request through one instance, the nightly report job that must run under a lock, and two incidents, a blocked event loop and a slow memory leak.

The map

What is inside

Many requests share a few cores and a few rows. Learn who gets to touch what, and when.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Processes, threads andevent loops2Async code and pools3Race conditions in memory4Concurrency acrossinstances5Distributed locks6Backpressure and overload7Memory and resource leaks8The Node.js runtime9Go and other runtimes10One instance end to end
  1. 01Processes, threads and event loopsSections 1 to 3. We look at how one server handles many requests at once.
  2. 02Async code and poolsSections 4 to 6. We look at how code is written for concurrent runtimes and how the resources it uses are pooled.
  3. 03Race conditions in memorySections 7 to 9. We look at what goes wrong when two threads touch the same data, and at the tools that make them take turns.
  4. 04Concurrency across instancesSections 10 to 11. We leave the single process and find that its locks no longer help.
  5. 05Distributed locksSections 12 to 16. We build a lock that many processes on many machines can share, then break it in every way the real world allows.
  6. 06Backpressure and overloadSections 17 to 19. We answer the syllabus's sharpest question, where the extra requests go when more arrive than can be served.
  7. 07Memory and resource leaksSections 20 to 22. We hunt the slow failures, where something is acquired on every request and released on none.
  8. 08The Node.js runtimeSections 23 to 25. We open Node.js, the runtime the syllabus uses as its first example of what an interviewer may ask.
  9. 09Go and other runtimesSections 26 to 28. We open the Go runtime, the syllabus's second example, and compare it briefly with the JVM and Python.
  10. 10One instance end to endSections 29 to 30. We follow Wren's order request through one instance and see every idea of the unit at work.
  11. +Interview prepInterview question bank, graded exercises and worked solutions.