Highlights

Contents

Part II
02

Async code and pools

We look at how code is written for concurrent runtimes and how the resources it uses are pooled. The syntax has changed from callbacks to async and await, but what runs underneath has not. We will cover callbacks, futures, promises and coroutines, sizing thread and worker pools, and what happens when CPU work blocks an event loop.

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

#Section 4Callbacks, futures, promises and coroutines

On an event loop, code cannot simply wait for the database. It must say what to do when the result arrives and give the thread back. The first way to say it was a callback, a function passed along with the request, getOrder(id, (err, order) => ...). Callbacks work, but a request with five sequential steps becomes five nested functions, error handling must be repeated at every level, and the shape is known as callback hell.

A future, called a promise in JavaScript, is an object representing a result that will exist later. getOrder(id) returns a promise immediately, and .then(fn) attaches what to do with the value. Promises can be chained, combined with Promise.all to wait for several, and carry errors down the chain to one .catch. Java’s CompletableFuture, Python’s asyncio.Future and Rust’s Future are the same idea.

THREE WAYS TO WRITE "LOAD THE ORDER, THEN THE USER, THEN RESPOND"THREE WAYS TO WRITE "LOAD THE ORDER, THEN THE USER, THEN RESPOND"callbacksgetOrder(id, (e, o) => { getUser(o.uid, (e, u) => { send(o, u) })})promisesgetOrder(id) .then(o => getUser(o.uid) .then(u => send(o, u))) .catch(fail)async / awaitconst o = await getOrder(id)const u = await getUser(o.uid)send(o, u)same execution: each await hands the thread back to the loop until the result arrivesawait in a loop runs one at a time; Promise.all or a task group runs them together
Figure 9.7Callbacks, promises and async/await, all expressing the same three steps.

Async/await makes promise-based code look sequential. const order = await getOrder(id) suspends the function at that line, returns control to the event loop, and resumes the function when the promise settles, with the value assigned. C# introduced it in 2012, JavaScript in 2017, Python in 3.5 in 2015, and Rust in 2019.

Underneath, an async function is a coroutine, a function that can pause at defined points and resume later with its local variables intact. Its state is saved in a small heap object, a few hundred bytes, rather than a full thread stack. Thousands of coroutines can be suspended at once, each waiting on its own I/O, with one thread running whichever is ready.

A COROUTINE PAUSES AT EACH AWAIT AND RESUMES WHERE IT LEFT OFFA COROUTINE PAUSES AT EACH AWAIT AND RESUMES WHERE IT LEFT OFFcoroutine Acoroutine Bloop threadtimerunsuspended at awaitresumerunsuspendedresumeABABstate is saved in the coroutine's frame, a few hundred bytes, not a whole thread stack
Figure 9.8Each coroutine pauses at an await and the loop thread runs another. Nothing blocks.

Two mistakes recur. The first is awaiting in a loop when the steps are independent. for (const id of ids) await getUser(id) makes ten calls one after another, ten round trips. await Promise.all(ids.map(getUser)) or Python’s asyncio.gather makes them concurrently, though it also needs a limit on how many run at once, or it fires a thousand requests at a downstream service. The second is forgetting to await, which starts work whose errors nobody sees and whose completion nobody waits for, a source of the leaks in Part VII.

Go and Java’s virtual threads take a different path. Code is written as ordinary blocking calls, and the runtime suspends the goroutine or virtual thread instead of the operating system thread. The programmer gets sequential code without async keywords, and the runtime does the coroutine work.

#Section 5Thread pools, worker pools and their sizes

Creating a thread per request and destroying it afterwards wastes time, and unlimited threads exhaust memory under load. A thread pool keeps a fixed set of threads and a queue of tasks. A request becomes a task, a free thread runs it, and when all threads are busy, tasks wait in the queue. Tomcat, Jetty and most Java servers have a request thread pool, 200 threads by default in Tomcat. A worker pool is the same idea for background jobs, and Unit VIII sized one with Little’s law. The connection pool from Unit VI is a pool of a different resource, used the same way.

How big should a thread pool be? It depends on how much of each task is computing and how much is waiting. A rule from Brian Goetz’s Java Concurrency in Practice (2006) gives the number of threads that keeps all cores busy.

Nthreads=Ncores×(1+WC)N_{threads} = N_{cores} \times \left(1 + \frac{W}{C}\right)

Read it as “one thread per core, plus enough extra threads to fill the time each one spends waiting”. Wren’s request computes for 10 ms and waits for 40 ms, so W ÷ C = 4, and on 8 cores the pool needs 8 × (1 + 4) = 40 threads. Fewer leaves cores idle while threads wait. Many more adds context switching and memory without adding throughput.

SIZING A THREAD POOL: CORES × (1 + WAIT ÷ COMPUTE)SIZING A THREAD POOL: CORES × (1 + WAIT ÷ COMPUTE)one thread10 ms cpuwaiting 40 ms10 ms8 × (1 + 40 ÷ 10) = 40 threads keep 8 cores busypure CPU work wants about one thread per core; more only adds switching
Figure 9.9One thread computes 10 ms and waits 40 ms, so 5 threads per core keep it busy, 40 on 8 cores.

For pure CPU work, W is near zero, and the answer is about one thread per core, which is why image resizing pools and parallel stream pools default to the core count. For pools that call slow external services, W ÷ C can be 100 or more, which is exactly when thread pools become expensive and event loops or virtual threads shine.

Every pool also needs a bounded queue and a policy for when it is full. An unbounded task queue in front of a thread pool is the unbounded buffer of Part VI, and Java’s Executors.newFixedThreadPool creates exactly that by default. Wren’s request pool has a queue of 100 and rejects beyond it with 503, which turns overload into a visible error instead of a slow memory leak.

Pools also interact. A request thread that borrows a database connection, then waits on a pool of HTTP clients, then on a lock, can deadlock if those pools are sized so that every request holds one resource while waiting for another. Sizing pools together, and acquiring resources in a consistent order, avoids it.

#Section 6Blocking the event loop

Wren’s first receipt feature rendered the PDF inside the Node.js request handler. Rendering took 200 ms of pure CPU. During those 200 ms the event loop ran nothing else. At 500 requests per second, about 0.2 × 500 = 100 requests arrived during each render and waited for it to finish before even being parsed. Their latency jumped by up to 200 ms, health checks timed out, and the orchestrator restarted instances that were busy rather than broken.

A 200 MS CPU TASK ON THE EVENT LOOP: EVERY OTHER REQUEST WAITS BEHIND ITA 200 MS CPU TASK ON THE EVENT LOOP: EVERY OTHER REQUEST WAITS BEHIND ITPDF render200 msat 500 requests/s, 100 arriveduring the 200 ms and all waitloop stuckhealth checks time out too, so the orchestrator may kill a process that is merely busy
Figure 9.10One 200 ms task holds the loop. A hundred requests queue behind it, and the health check too.

The arithmetic gets worse with volume. If 10% of requests did 200 ms of CPU work, the instance would need 50 × 0.2 = 10 s of CPU per second on a loop that has 1 s per second to give. The loop saturates, the queue of pending events grows without bound, and latency climbs until requests time out. Nothing is technically broken, and nothing is getting done.

The usual culprits are easy to name. Large JSON parsing or serialization, such as a 5 MB request body. Complex regular expressions, especially those that backtrack catastrophically on crafted input, an attack called ReDoS. Password hashing done synchronously. Image and PDF processing. Synchronous compression. Sorting or transforming large arrays in memory.

The fixes move CPU work off the loop. Node’s worker_threads run JavaScript on separate threads with their own event loops, and a pool such as Piscina hands tasks to them. Heavy, slow or retryable work goes to a job queue and separate worker processes, Unit VIII, which is what Wren did with receipts. Where work cannot move, it can be split into chunks that yield to the loop between them. And input limits, such as maximum body sizes and safe regular expression engines, stop attackers from creating the CPU work in the first place.

KEEP THE LOOP FOR I/O; SEND CPU WORK TO WORKERSKEEP THE LOOP FOR I/O; SEND CPU WORK TO WORKERSevent loopworker threads or a job queuerender(order 124)result, later
Figure 9.11The loop keeps doing I/O. CPU-heavy tasks go to worker threads or a queue.

The symptom to watch is event loop lag, the delay between when a timer should fire and when it does. Node exposes it through perf_hooks.monitorEventLoopDelay, and a p99 lag above tens of milliseconds means something is hogging the loop. Python’s asyncio has a debug mode that logs callbacks taking longer than 100 ms.