Unit 7 of 8 · about 4 hours · explain, measure

Connection shutdown, pools and latency decomposition

Setup cost decides reuse policy; shutdown order decides deploy safety. It assumes the Programming container health and SIGTERM-drain unit (programming/concurrency-profiling-operations/health-containers-runbooks); here the mechanism is sockets, accept queues, pools and load-balancer coordination.

You'll be able to

  • Explain half-close, TIME-WAIT and CLOSE-WAIT ownership, and the graceful shutdown order.
  • Size a connection pool from concurrency with Little's Law and key pools by security boundary.
  • Decompose client latency into DNS, connect, TLS, queue, handler and dependency parts from client, server and trace evidence.

Covers

Cancellation, half-close and graceful connection shutdownConnection pools, server queues and load-balancing vocabularyClient, network, queue, handler and dependency latency
1

Read

In this order, only the parts listed

0 of 6 done

  1. TCP termination and half-closeAlgoMaster Networks - pcap and concurrent server labs

    The Normal Four-Segment Close; Understanding TIME-WAIT; Understanding CLOSE-WAIT; Half-Close at the Socket API; Abrupt Termination with RST

  2. listen backlog, accept, close and shutdownBeej's Guide to Network Programming - sockets to multiplexing

    Section 5.5 (listen and the backlog); Section 5.6 (accept); Section 5.9 (close and shutdown)

  3. Connection pooling and keep-aliveAlgoMaster Networks - pcap and concurrent server labs

    How a Pool Handles a Request; Pool Keys Protect Routing and Security Boundaries; Size the Pool from Concurrency; Separate the Timeout Boundaries; Stale Connections Cannot Be Eliminated Completely

  4. Graceful shutdown and drainingAlgoMaster Networks - pcap and concurrent server labs

    A Shutdown State Machine; The Order of Operations Matters; Budget the Grace Period; Testing Shutdown Under Load

  5. Where latency comes fromAlgoMaster Networks - pcap and concurrent server labs

    The Four Per-Hop Delays; Protocol Round Trips Add Latency; Measuring an HTTP Latency Breakdown; Interpreting Common Network Measurements

  6. A latency decomposition worked exampleAlgoMaster Networks - pcap and concurrent server labs

    Turn the Stream into a Timeline; Inspect the Edges of the Gap

    Read as a decomposition method: phase table first, then edges of the dominant gap. You already studied its flow-control mechanism in the transport unit.

2

Focus on

  • Each direction closes independently. shutdown for writing sends FIN while the read side stays open; close with unread data still queued can reset the connection.
  • TIME-WAIT belongs to the active closer and lasts twice the maximum segment lifetime; a lingering CLOSE-WAIT means the application never closed, which is a leak, not a TCP quirk.
  • Pool size follows offered concurrency: arrival rate times hold time, plus headroom. Pool keys must include every security boundary (host, TLS identity, credentials), never just the IP.
  • One client number never decomposes itself. DNS, connect, TLS, queue, handler and dependency times come from different instruments; subtract only compatible timestamps.
Common mistakes · 3
  • Reusing a connection across hostnames that resolve to one IP. Certificates and authorization attach to names, not addresses.
  • Treating starttransfer minus appconnect as server time. It still includes request travel, queueing and first-byte return.
  • Draining by closing the listener only. In-flight requests, keep-alive connections and load-balancer caches need explicit propagation, a grace budget and a forced-stop step.
3

Try it yourself

Closed-book, before checking anything

Derive the pool size closed-book: 600 requests/s times 50 ms mean hold gives 30 busy connections, so 20 cannot sustain the average and exactly 30 leaves no burst headroom. Then split one HTTPS request to https://example.com/ into DNS, connect, TLS and first-byte intervals with curl -w, three times, and report the numbers with your curl and TLS library versions and warm or cold DNS.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.ss shows many sockets in CLOSE-WAIT. Whose bug is it?Answer

    The local application's. The peer closed its side; the local end never closed its socket, so the state persists.

  2. 2.A pool holds 20 HTTP/1.1 connections at 600 requests/s with 50 ms holds. What happens?Answer

    Waiters accumulate without bound. The pool sustains about 400 requests/s; the excess queues, and latency and memory grow.

  3. 3.Why must a pool discard a connection that received Connection: close?Answer

    The server will not accept another exchange on it. Lending it out guarantees the next borrower fails.

  4. 4.A pcap shows a 5 s gap with zero-window advertisements and no retransmissions. Which latency component grew?Answer

    None of the network components. An endpoint stall at the receiver inflated total time; DNS, connect and TLS phases were already complete.

5

Practice

  • AlgoMaster: Design a Connection Pool Queue

    acquire and release with FIFO waiter handoff; a release hands directly to the oldest waiter

    The release-to-waiter handoff is the bounded-admission behavior your pool needs.

You're done when

You size a pool from rate and hold time, place a CLOSE-WAIT leak and a zero-window stall from evidence, and write the shutdown order for a pooled service.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 7 more in the section
  • Beej's Guide to Network Programming - sockets to multiplexingBrian Hall · primary

    Read What is a socket?, IP addresses and data structures, Client-server background, System calls or bust, Client-server examples, Slightly advanced techniques and sendall. Build the simple TCP pair first, then replace delimiter assumptions with a bounded length-prefixed protocol.

  • AlgoMaster Networks - pcap and concurrent server labsAlgoMaster · required

    Use Reading a Packet Capture: A Real Bug and Building a Concurrent TCP Echo Server after the Beej implementation. Reproduce one packet-level observation and one admission, deadline or shutdown behavior in your independently written service.

Also in this section

  • Beej's Guide to Network Concepts - TCP and UDP pathBrian Hall · required

    Read the OS and sockets, protocol layering, IP, UDP and TCP chapters alongside packet captures from your own client and server. Use it to explain transport behavior rather than memorize layer names.

  • MIT 6.5840 - RPC, threads and fault vocabularyMIT PDOS · support

    Read the introductory RPC and threads material plus the lab guidance. Use it to enumerate timeout ambiguity and duplicate effects. Stop before Raft and the full lab sequence.

  • Replicated Log and Stream Processor - protocol sliceRoadmap-Final · support

    Use only the protocol requirements, deterministic network fault matrix and delivery-contract questions. Build the single-node framed service now; keep replication and consensus for the later project.

  • Designing Data-Intensive Applications, 2nd edition - Chapter 5O'Reilly · required

    Read Formats for Encoding Data (printed pp. 163 to 177) for schema merits and compatibility, and Dataflow Through Services: REST and RPC (printed pp. 180 to 186) for service dataflow, version coexistence and the idempotence pointer.

  • gRPC Deadlines guidegRPC Authors · reference

    Read Overview, Deadlines on the Client and Deadline Propagation for absolute deadlines, remaining-time forwarding and clock-skew shielding.

  • gRPC Status CodesgRPC Authors · reference

    Read the full status list, including DEADLINE_EXCEEDED, UNAVAILABLE and the codes never generated by the library, for the error taxonomy.

  • W3C Trace ContextW3C · reference

    Read Section 3.2 for the traceparent format and Sections 3.4 to 3.5 for the legal traceparent and tracestate mutations when forwarding.