Sub topic 13
Back Pressure in TCP, HTTP/2, and Reactive Streams
This chapter assumes you know the producer/consumer model, the idea of a bounded queue, and basic HTTP. Earlier chapters showed back pressure inside one process, where a full queue makes put() wait. Here you will see the same idea applied between machines and between libraries.
The common pattern
Every protocol in this chapter does the same thing. The receiver tells the sender how much more it can accept, and the sender stops when that allowance runs out. The allowance is a bounded buffer made visible to the other side. No one has to guess, and no data is dropped to signal "slow down".
TCP: the receive window
TCP gives each connection a receive buffer. In every acknowledgment, the receiver advertises a receive window, which is the number of bytes it still has room for. The sender may not have more unacknowledged bytes in flight than that window.
When the application reads slowly, the buffer fills and the advertised window shrinks. At zero, the sender pauses and probes occasionally until space opens. The slow reader's problem travels back to the writer with no extra code.
You see this in practice when a blocking socket.send() stalls, or when await writer.drain() in asyncio waits. Ignoring drain() is a common mistake: data piles up in the user-space write buffer, and you have rebuilt an unbounded queue.
import asyncio
async def send_all(writer: asyncio.StreamWriter, chunks) -> None:
for chunk in chunks:
writer.write(chunk)
await writer.drain() # waits while the transport buffer is above its high-water markHTTP/2: per-stream flow control
TCP controls a whole connection, but HTTP/2 multiplexes many streams over one connection. One slow stream could otherwise block the rest. HTTP/2 therefore adds its own flow control, using WINDOW_UPDATE frames.
- Each stream has its own window, and the connection has an overall window.
- A sender may transmit
DATAframes only while both windows have credit. - The receiver sends
WINDOW_UPDATEas the application consumes data, which restores credit.
The default initial window is 65,535 bytes. A receiver that never sends updates stalls the stream. A receiver that grants huge windows loses the protection. Flow control applies only to DATA frames, so it does not limit the number of requests. The SETTINGS_MAX_CONCURRENT_STREAMS setting limits those.
Reactive streams: demand-driven pull
Libraries that follow the Reactive Streams specification (Project Reactor, RxJava, Akka Streams) move the same idea into application code. A subscriber calls request(n) to ask for n items. The publisher must not send more than has been requested.
Because demand is explicit, a slow stage limits every stage upstream of it. Operators such as onBackpressureDrop or onBackpressureBuffer let you choose what happens when a source cannot slow down, such as a sensor. That choice is where back pressure turns into load shedding.
Comparing the three
| Mechanism | Scope | Credit unit | Receiver grants credit by |
|---|---|---|---|
| TCP | One connection | Bytes | Advertising the receive window in ACKs |
| HTTP/2 | Each stream and the connection | Bytes of `DATA` | `WINDOW_UPDATE` frames |
| Reactive Streams | One subscription | Items | `request(n)` |
Where it fails
Back pressure only works if every hop passes it along. A proxy that buffers without limit, or a handler that reads a request body into memory and then queues it, breaks the chain. Back pressure also does not protect a service from many independent clients. Each client may respect its own window while the total still exceeds capacity. That case needs load shedding.
Exercise
A client uploads a large file to your asyncio server. The handler reads chunks and puts them on a slow processing queue, and memory keeps growing even though TCP flow control is working. What is the likely cause, and how do you fix it?
Show answer
The processing queue is unbounded, so the handler keeps reading from the socket at full speed and TCP never sees a full receive buffer. Use a bounded queue (for example asyncio.Queue(maxsize=N)) and await queue.put(chunk). The handler then pauses reading when the queue is full, the receive buffer fills, and TCP slows the client.