7 routes42 sections324 units

Unit 3.7 · about 4 h

255 / 324

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.

  • explain
  • measure

You can now

  • 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 shutdown
  • Connection pools, server queues and load-balancing vocabulary
  • Client, network, queue, handler and dependency latency

Study, in this order

6 steps

  1. TCP termination and half-close
    AlgoMaster 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 shutdown
    Beej'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-alive
    AlgoMaster 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 draining
    AlgoMaster 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 from
    AlgoMaster 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 example
    AlgoMaster Networks - pcap and concurrent server labs

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

    Depth · 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.

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.

Reproduce closed-book

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.

Traps

  • 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.

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.

Self-test

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

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

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

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

Completion criterion

Stop 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.

Mark passed, then open 3.8 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Beej's Guide to Network Programming - sockets to multiplexing
    Brian Hallprimarycourse
    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 labs
    AlgoMasterrequiredpractice
    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 path
    Brian Hallrequiredtheory
    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 vocabulary
    MIT PDOSsupporttheory
    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 slice
    Roadmap-Finalsupportproject
    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 5
    O'Reillyrequiredtheory
    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 guide
    gRPC Authorsreferencereference
    Read Overview, Deadlines on the Client and Deadline Propagation for absolute deadlines, remaining-time forwarding and clock-skew shielding.
  • gRPC Status Codes
    gRPC Authorsreferencereference
    Read the full status list, including DEADLINE_EXCEEDED, UNAVAILABLE and the codes never generated by the library, for the error taxonomy.
  • W3C Trace Context
    W3Creferencereference
    Read Section 3.2 for the traceparent format and Sections 3.4 to 3.5 for the legal traceparent and tracestate mutations when forwarding.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels