← Concepts

阅读中文版本

Chunked Transfer Encoding

Chunked transfer encoding streams a response of unknown length as a sequence of length-prefixed chunks, ending with a zero-size chunk, enabling progressive delivery without buffering.

http

Definition

Chunked encoding is the HTTP/1.1 mechanism for delivering a body whose total size is not known in advance. Each chunk is prefixed by its size in hexadecimal, the body ends with a zero-length chunk, and optional trailers can follow. It removes the need to compute Content-Length before sending.

Context and scope

Used by streaming HTML responses, server-sent event endpoints, large database exports, LLM token streams, and any handler that produces output incrementally before the total size is known.

Common misconceptions

  • Chunked responses cannot be resumed — without a length, range logic has no anchor for partial retry
  • Trailers are just like headers — many clients and proxies ignore or drop them entirely
  • Chunking implies streaming to the user — chunk boundaries are transport framing, not flush decisions

Anti-patterns

  • Disabling Nagle on chunked streams without also flushing per logical event, producing byte trickle latency
  • Relying on trailer headers for critical metadata that intermediaries strip
  • Assuming a reverse proxy forwards chunks as they arrive rather than buffering the whole response

Related concepts

Related examples

Authoritative source

https://www.rfc-editor.org/rfc/rfc9112

What it is

When a server does not know the final size, it writes the body as self-delimiting chunks: a hex length line, the bytes, a CRLF. A zero chunk terminates the body. This is the wire format that makes streaming responses possible under HTTP/1.1 semantics.

How it works

The sender emits Transfer-Encoding: chunked and no Content-Length. Receivers concatenate chunks and may expose trailers after the terminating chunk. In HTTP/2 and HTTP/3 the concept disappears — frames and streams are already length-delimited, and the chunked header is translated rather than spoken natively.

When to use it

Progressive HTML rendering, event streams, and token-by-token model output all rely on it. Buffering proxies in front can silently destroy the streaming property, so configurations like proxy_buffering off in nginx matter as much as the application code.

Pitfalls

A chunked response cannot easily support range-based resume because the size is unknown until the end. Trailer support is inconsistent across clients, so critical data belongs in the head. Compression plus chunking interacts with flush behavior: a compressed stream may not emit bytes until its window fills.