EDI - Electronic Data Interchange :👈 👉:Terms Of Use/Disclaimer

HPACK and QPACK - Header Compression Mechanisms

HPACK and QPACK: Complete Guide to HTTP Header Compression

1. Introduction

HPACK and QPACK are header compression mechanisms used in HTTP/2 and HTTP/3 respectively.

They reduce the amount of data transferred in HTTP request and response headers, improving network efficiency and reducing unnecessary data transmission.

HTTP headers contain information such as:

  • Content-Type
  • Authorization
  • Accept
  • User-Agent
  • Cookie
  • :method
  • :path
  • :status

When multiple HTTP requests contain the same headers, transmitting the complete header information repeatedly creates unnecessary overhead. HPACK and QPACK help reduce this overhead.

2. What Is HPACK?

HPACK (Header Compression for HTTP/2) is a header compression algorithm defined in RFC 7541.

HTTP/2 uses HPACK to compress HTTP request and response headers before transmitting them over a connection.

2.1 Why Is HPACK Needed?

Consider two HTTP requests sent by a browser.

Request 1

GET /products HTTP/2
Host: example.com
User-Agent: Chrome
Accept: application/json
Authorization: Bearer abc123

Request 2

GET /orders HTTP/2
Host: example.com
User-Agent: Chrome
Accept: application/json
Authorization: Bearer abc123

Notice that most headers are repeated. Only the request path changes.

Without header compression, repeated information would need to be transmitted again. HPACK can represent repeated header fields more efficiently using indexed representations and other compression techniques.

2.2 How Does HPACK Work?

HPACK uses three important techniques:

  1. Static Table
  2. Dynamic Table
  3. Huffman Encoding

A. Static Table

HPACK contains a predefined static table with 61 common HTTP header entries.

Examples include:

  • :method: GET
  • :method: POST
  • :status: 200
  • content-type

Instead of transmitting the complete header field, HPACK can reference its predefined table index when the entry matches.

B. Dynamic Table

The dynamic table stores header fields learned during a connection.

For example:

authorization: Bearer abc123

If this header is added to the dynamic table, subsequent requests can reference its index instead of transmitting the complete header field again.

The dynamic table is maintained by the encoder and decoder. Both sides must keep their table state synchronized.

C. Huffman Encoding

HPACK can use Huffman encoding to represent header strings more compactly.

Huffman encoding assigns shorter bit sequences to more frequent characters and longer sequences to less frequent characters.

This can reduce the size of header string representations.

2.3 HPACK Architecture Diagram

HTTP/2 Client HTTP Request Headers HPACK Encoder Compressed Header Block TCP Connection Compression techniques Static Table Dynamic Table Huffman Encoding Conceptual diagram: the server decodes the received header block.

Note: The diagram focuses on the client-to-server request path. The server also uses an HPACK decoder, and response headers are compressed in the opposite direction.

3. What Is QPACK?

QPACK (Field Compression for HTTP/3) is a header compression mechanism designed specifically for HTTP/3.

It is defined in RFC 9204.

QPACK also uses:

  • Static Table
  • Dynamic Table
  • Huffman Encoding

However, QPACK is designed to work with QUIC's independently delivered streams.

3.1 Why Was QPACK Introduced?

HTTP/2 uses TCP, whereas HTTP/3 uses QUIC, which operates over UDP.

HTTP/2 multiplexes multiple streams over a single TCP connection. Because TCP delivers bytes in order, packet loss can temporarily prevent data on other streams from being delivered.

QUIC supports independent streams. Data on one stream can continue progressing even if another stream is waiting for lost data.

This creates a special challenge for header compression.

Consider this situation:

  1. Stream 1 causes the encoder to add a new entry to the dynamic table.
  2. Stream 2 tries to reference that new table entry.
  3. The decoder has not yet received the instruction that creates the entry.
  4. The decoder cannot decode Stream 2's header block until the required entry becomes available.

QPACK manages these dependencies using encoder and decoder instructions, separate unidirectional streams, and mechanisms that limit blocking caused by dynamic table references.

3.2 How Does QPACK Work?

QPACK has three important parts:

A. Static Table

QPACK contains a predefined static table with 99 entries.

These entries represent commonly used HTTP header fields and values.

B. Dynamic Table

The dynamic table stores additional header fields that are learned during a connection.

Both the encoder and decoder maintain compatible dynamic table state.

Unlike simply assuming that an entry is already available, QPACK provides instructions to communicate dynamic table changes and manage references to those entries.

C. Encoder and Decoder Streams

QPACK uses special unidirectional streams:

  • Encoder Stream: Carries instructions from the encoder to the decoder to update the dynamic table.
  • Decoder Stream: Carries feedback from the decoder to the encoder, including acknowledgements and instructions about table references.

HTTP/3 header blocks are carried in HTTP frames on request or response streams. The encoder and decoder streams carry compression instructions, not the HTTP header blocks themselves.

3.3 QPACK Architecture Diagram

HTTP/3 Client QPACK Encoder Header Block On HTTP request/response stream Encoder Stream Dynamic table instructions QUIC Connection QPACK Decoder Decoder feedback via decoder stream Conceptual diagram: QUIC carries separate HTTP and QPACK unidirectional streams.

Note: The diagram is conceptual. In a real HTTP/3 connection, the encoder stream and decoder stream are separate QUIC unidirectional streams. The header block is carried on an HTTP request or response stream.

4. HPACK vs QPACK

Feature HPACK QPACK
Full Form Header Compression for HTTP/2 Field Compression for HTTP/3
Used By HTTP/2 HTTP/3
RFC RFC 7541 RFC 9204
Transport TCP QUIC over UDP
Static Table Yes, 61 entries Yes, 99 entries
Dynamic Table Yes Yes
Huffman Encoding Yes Yes
Compression Coordination Shared connection compression state Encoder and decoder streams with feedback
Main Design Concern Efficient header compression for HTTP/2 Header compression compatible with QUIC's independent streams

5. HTTP/1.1 vs HTTP/2 vs HTTP/3

Feature HTTP/1.1 HTTP/2 HTTP/3
Transport TCP TCP QUIC over UDP
Header Compression No HPACK or QPACK HPACK QPACK
Multiplexing Limited; typically one active response per connection Multiple streams over one connection Multiple independent QUIC streams
Binary Framing No Yes Yes
Transport-Level Head-of-Line Blocking Yes Yes, with TCP packet loss Can avoid cross-stream blocking from packet loss

6. HTTP Versions and Compression Architecture

HTTP/1.1 HTTP Headers Text Transmission No HPACK/QPACK HTTP/2 HTTP Headers HPACK TCP transport HTTP/3 HTTP Headers QPACK QUIC over UDP Each HTTP version uses its own framing and transport design.

7. Are HPACK and QPACK Encryption Mechanisms?

No. HPACK and QPACK are compression mechanisms, not encryption mechanisms.

They reduce header size, but they do not protect header values from being read by an attacker who can observe unencrypted traffic.

Technology Purpose
HPACK Compresses HTTP/2 headers
QPACK Compresses HTTP/3 headers
TLS Provides encryption and authentication
QUIC Provides secure transport and multiplexed streams for HTTP/3

For example:

  • HTTP/2 commonly uses TLS for secure communication.
  • HTTP/3 uses QUIC, which incorporates TLS 1.3 security.
  • HPACK and QPACK compress headers but do not encrypt them.

Important security consideration: Compression can create security risks when secrets and attacker-controlled data are compressed together in observable contexts. Implementations must follow protocol security recommendations and avoid unsafe handling of sensitive headers.

8. Where Are HPACK and QPACK Used?

Technology Header Compression
HTTP/1.1 Neither HPACK nor QPACK
HTTP/2 HPACK
HTTP/3 QPACK
ASP.NET Core Web API over HTTP/2 HPACK
ASP.NET Core Web API over HTTP/3 QPACK

The protocol used depends on the client, server, hosting configuration, and network support. Developers generally do not need to implement HPACK or QPACK manually; the HTTP server and client protocol implementations handle them.

9. HPACK and QPACK in ASP.NET Core

When building an ASP.NET Core Web API, you can expose endpoints over HTTP/1.1, HTTP/2, or HTTP/3, depending on the server and hosting environment.

The underlying HTTP protocol implementation handles header compression automatically.

9.1 Example: HTTP/2 Endpoint Configuration

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options =>
{
    options.ListenAnyIP(5001, listenOptions =>
    {
        listenOptions.UseHttps();
        listenOptions.Protocols = HttpProtocols.Http2;
    });
});
var app = builder.Build();
app.MapGet("/products", () =>
{
    return Results.Ok(new[]
    {
        new { Id = 1, Name = "Laptop" },
        new { Id = 2, Name = "Keyboard" }
    });
});
app.Run();

Note: Add using Microsoft.AspNetCore.Server.Kestrel.Core; if required. This is an illustrative HTTP/2 Kestrel configuration, not a complete production hosting setup.

When HTTP/2 is negotiated, Kestrel handles HPACK automatically. You do not need to call an HPACK compression method in your controller or endpoint.

9.2 HTTP/3 Configuration

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options =>
{
    options.ListenAnyIP(5001, listenOptions =>
    {
        listenOptions.UseHttps();
        listenOptions.Protocols = HttpProtocols.Http3;
    });
});
var app = builder.Build();
app.MapGet("/products", () =>
{
    return Results.Ok("HTTP/3 API endpoint");
});
app.Run();

Important: HTTP/3 hosting requires appropriate QUIC support, a suitable HTTPS certificate, and correct hosting and network configuration. HTTP/3 commonly uses UDP port 443. Some hosting environments may require additional configuration.

When HTTP/3 is negotiated, the underlying protocol implementation handles QPACK automatically.

10. Common Mistakes

  • Confusing compression with encryption: HPACK and QPACK compress headers; TLS provides encryption.
  • Thinking HTTP/2 uses QPACK: HTTP/2 uses HPACK.
  • Thinking HTTP/3 uses HPACK: HTTP/3 uses QPACK.
  • Thinking QUIC is TCP: QUIC runs over UDP and provides its own reliable transport features.
  • Assuming compression removes all repeated data: Compression effectiveness depends on the headers and table state.
  • Assuming developers manually implement compression: Normal ASP.NET Core applications rely on the HTTP server and client implementation.

11. Interview Questions and Answers

Q1. What is HPACK?

HPACK is a header compression mechanism used by HTTP/2 to reduce the size of HTTP request and response headers.

Q2. What is QPACK?

QPACK is a header compression mechanism used by HTTP/3. It is designed to work efficiently with QUIC's independent streams.

Q3. Why was QPACK introduced when HPACK already existed?

HPACK has compression dependencies that can cause blocking when used with independently delivered QUIC streams. QPACK introduces mechanisms to manage these dependencies.

Q4. What are the main components of HPACK?

  • Static Table
  • Dynamic Table
  • Huffman Encoding

Q5. What is the size of the HPACK static table?

The HPACK static table contains 61 predefined entries.

Q6. What is the size of the QPACK static table?

The QPACK static table contains 99 predefined entries.

Q7. Does HTTP/1.1 use HPACK?

No. HPACK is designed for HTTP/2.

Q8. Does QPACK encrypt HTTP headers?

No. QPACK compresses headers. HTTP/3 uses QUIC with TLS 1.3 for transport security.

Q9. What is the main difference between HPACK and QPACK?

HPACK is designed for HTTP/2 over TCP, whereas QPACK is designed for HTTP/3 over QUIC and manages compression dependencies across independent streams.

Q10. What is a dynamic table?

A dynamic table stores header fields learned during a connection so that later header blocks can reference them more efficiently.

Q11. What is an encoder stream in QPACK?

An encoder stream is a unidirectional QUIC stream that carries instructions from the encoder to update the decoder's dynamic table.

Q12. What is a decoder stream in QPACK?

A decoder stream is a unidirectional QUIC stream that carries feedback from the decoder to the encoder, including acknowledgements and instructions related to dynamic table references.

Q13. Does ASP.NET Core require developers to implement HPACK or QPACK manually?

No. Kestrel and the underlying HTTP protocol implementations handle header compression automatically when the corresponding HTTP version is negotiated.

12. Final Summary

HTTP Version Compression Mechanism Transport
HTTP/1.1 No HPACK/QPACK TCP
HTTP/2 HPACK TCP
HTTP/3 QPACK QUIC over UDP

Remember this simple rule:

  • HTTP/1.1 → No HPACK or QPACK.
  • HTTP/2 → HPACK.
  • HTTP/3 → QPACK.

Both HPACK and QPACK improve HTTP communication efficiency by reducing repeated header data. Their main difference is how they manage compression state and dependencies in their respective HTTP transport architectures.

13. Official References

🔙
EDI - Electronic Data Interchange :👈 👉:Terms Of Use/Disclaimer

Comments and Discussion