Experiments and implementations · Intermediate

Comparing latency with connection reuse

Compare accepted connection counts and elapsed time for three local HTTP/1.1 requests with and without reuse.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Observe connections before declaring speed
  2. Consume each response completely
  3. Run both local conditions
  4. Describe the timed interval precisely
  5. List differences from a deployed service
  6. Handle failed reuse and retries separately
  7. Ask Logeia about this articleCheck your understanding

Observe connections before declaring speed

Connection reuse can avoid repeatedly establishing a connection. Start a small loopback HTTP/1.1 server rather than contacting an external site. Compare three requests with a new connection each time against three requests on one connection, counting how many connections the server accepts. Display elapsed time too, but do not assert a universal speedup factor. Localhost lacks an Internet round-trip path and this experiment does not use TLS. Python server and client overhead are also included. The primary deterministic observation is the number of connections used for equal responses; timing supplies local context rather than a portable performance promise.

Consume each response completely

A reusable connection needs unambiguous message boundaries. The server sends Content-Length, and the client fully consumes response.read before sending another request. Do not compare this with a version that leaves unread body bytes and call it the same workload. Reusing a connection does not mean sharing application authentication state or transmitting every request simultaneously. The program sends one request at a time; it does not measure HTTP pipelining or HTTP/2 multiplexing. Name the resource being reused and the sequence being exercised. This makes later changes to concurrency or protocol visible as new experimental conditions instead of hidden assumptions.

Run both local conditions

The OS selects an available port and the server binds only to loopback. This is not a server intended for public exposure. Assert three accepted connections for fresh requests and one for reuse, while also checking every status and response body. Connections and the serving thread are shut down afterward. Exclude failed or incomplete-response runs from an interpretation of successful equal-work timing rather than quietly averaging them together. The server’s lifetime belongs to this program, so later steps should not assume the selected port remains available. No external credentials, network service or additional Python package is needed.

Python 3
import http.client, threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from time import perf_counter
class Handler(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"
    def do_GET(self):
        body = b"ok"
        self.send_response(200)
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)
    def log_message(self, *args):
        pass
class Server(ThreadingHTTPServer):
    accepted = 0
    def get_request(self):
        result = super().get_request()
        self.accepted += 1
        return result
with Server(("127.0.0.1", 0), Handler) as server:
    thread = threading.Thread(target=server.serve_forever)
    thread.start()
    try:
        for reuse in (False, True):
            before, connection = server.accepted, None
            start = perf_counter()
            for _ in range(3):
                if connection is None:
                    connection = http.client.HTTPConnection("127.0.0.1", server.server_port, timeout=3)
                connection.request("GET", "/")
                response = connection.getresponse()
                assert response.status == 200 and response.read() == b"ok"
                if not reuse:
                    connection.close()
                    connection = None
            if connection is not None:
                connection.close()
            elapsed = perf_counter() - start
            accepted = server.accepted - before
            assert accepted == (1 if reuse else 3)
            print({"reuse": reuse, "connections": accepted, "seconds": elapsed})
    finally:
        server.shutdown()
        thread.join()

Describe the timed interval precisely

Timing starts immediately before each group of three requests and ends after its final connection close. Server startup and shutdown are excluded. The measurement does not separately isolate DNS, TCP, TLS, application processing and body transfer. If reuse appears slower locally, that alone does not make reuse pointless: buffering, tiny writes and runtime scheduling can dominate. If it appears faster, the ratio still need not transfer elsewhere. Keep the mechanism check supplied by connection counts separate from the environment-dependent elapsed-time observation. A valid timing interval is not the same thing as isolation of every possible contributing cost.

List differences from a deployed service

External HTTPS introduces resolution, path latency, certificate verification, TLS establishment or resumption, proxies and server-initiated closure. A connection pool additionally has checkout waiting and capacity limits. Having reusable connections somewhere in a pool does not imply that this request can borrow one immediately. When extending the experiment, vary protocol, encryption, response size, request count and concurrency in controlled stages rather than changing them all at once. Any real-service comparison also needs an authorized target and bounded request rate. The loopback result is a baseline for understanding reuse, not permission to infer an external service’s capacity.

Handle failed reuse and retries separately

An idle connection can have been closed by the peer. A failed attempt to reuse it does not justify blindly replaying every operation. Read requests and state-changing operations have different duplicate-execution consequences. Design connection lifetime, complete response consumption and handling of unknown outcomes together. This example uses a simple GET and does not validate retry behavior for payments or updates. Record accepted counts, success of the three responses, timing for both conditions and Python version. Those observations establish a specific local behavior while leaving failure recovery and operation idempotency to separately tested application contracts.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 10

Choose a learning path