>_ DevTrendsen

Language

Home

Languages

Sections

Frontend Backend Mobile DevOps AI / ML GameDev Security
Rust

Diving into Quiche: Low-Level QUIC and HTTP/3 Magic from Cloudflare

11,606 stars

quiche

Hello, colleagues! Today I want to dig into the repository that powers a significant portion of today's fast internet. You're familiar with the situation when you open a website on your phone with unstable Wi-Fi and it loads painfully slowly? Or when a single "heavy" image blocks the entire page from loading? The TCP protocol, a veteran of networking technologies, has its limitations, especially in the mobile era. And this is where QUIC comes onto the stage.

And the tool that lets you "cook up" this very QUIC is quiche — a project from Cloudflare. Let's figure out what this "pie" is and why it might be interesting for every systems developer.

What is Quiche and Why Do You Need It?

To put it briefly, quiche is a Rust library that implements the QUIC transport protocol and the HTTP/3 application protocol. It was created and is actively used by Cloudflare to serve all of their HTTP/3 traffic.

"Well, just another HTTP library," you might think. But there's an important nuance here. Quiche is a low-level implementation. What does this mean in practice? It doesn't give you a ready-made HTTP client or server out of the box. Instead, it provides you with a powerful API for managing QUIC connection state and handling packets. All the work with sockets, async operations, and timers remains on your side.

It's as if you were given not a ready-made car, but a powerful, perfectly assembled engine. You decide yourself what body to put it in, what wheels to bolt on, and what the transmission will be. This provides incredible flexibility and control, which is critically important for high-performance systems.

By the way, its users speak to the project's seriousness:

  • Cloudflare: Their entire edge network uses quiche for HTTP/3 support.
  • Android: Starting with Android 11, the DNS resolver uses quiche to implement DNS-over-HTTP/3, improving privacy and domain name resolution speed.
  • curl: Yes, good old curl can work with HTTP/3 precisely thanks to integration with quiche.

Key Features: A Look Under the Hood

Let's explore what makes quiche so attractive for serious projects.

1. Full Control Over I/O

As I mentioned, quiche doesn't take network control away from you. Your code is responsible for reading and sending UDP packets. The workflow looks roughly like this:

  1. You receive a UDP packet from the socket.
  2. Pass it to the quiche connection via the conn.recv() method.
  3. The library processes the packet, changes the internal connection state (for example, acknowledges data receipt, handles flow control commands).
  4. You periodically call the conn.send() method to get UDP packets ready to send from quiche, and send them yourself through the socket.

This allows you to integrate quiche into any, even the most exotic, network engine or event loop, whether it's mio, tokio, async-std, or something custom-built.

2. Rust's Safety and Speed

Network protocols are an area where memory management errors can lead to serious vulnerabilities. Writing such a library in Rust is a strategic choice. Rust provides memory safety guarantees at the compiler level, which eliminates entire classes of bugs (such as buffer overflows or data races) without sacrificing the performance characteristic of C/C++.

3. Flexible Stream Handling

QUIC is a multiplexed protocol. This means that within a single connection, there can be many independent streams. Packet loss in one stream doesn't block the others (goodbye, "head-of-line blocking"!).

Quiche provides a simple and clear API for working with these streams:

// Отправляем данные в поток с ID=0
if conn.is_established() {
    conn.stream_send(0, b"hello", true)?;
}

// Проверяем, в каких потоках есть данные для чтения
if conn.is_established() {
    for stream_id in conn.readable() {
        while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
            println!("Получено {} байт в потоке {}", read, stream_id);
        }
    }
}

This approach makes it easy to implement complex interaction scenarios characteristic of HTTP/2 and HTTP/3.

4. API for Other Languages (C/C++)

Interestingly, the developers didn't confine themselves to the Rust ecosystem. Quiche has an FFI layer (Foreign Function Interface) that provides a C API. This means you can compile quiche as a static library (libquiche.a) and use all the power of QUIC in your C, C++, Python, Go, and any other language projects that can call C functions.

This opens up huge possibilities for integrating HTTP/3 into existing applications without the need to completely rewrite them in Rust.

How Does It Work in Practice?

Let's look at the connection lifecycle in quiche. It perfectly illustrates the library's philosophy.

  1. Configuration. First, a Config object is created, where you configure the QUIC version, protocols, stream and data limits, as well as TLS parameters.
  2. Connection creation. The client uses quiche::connect(), and the server uses quiche::accept(). At this stage, you provide the connection ID and network addresses.
  3. Main loop. And here's the heart of the application:
    • Receiving packets: You read data from the socket and "feed" it to the connection via conn.recv().
    • Sending packets: You call conn.send() in a loop until it returns the Done error. You send the received packets to the socket.
    • Timer management: QUIC is a stateful protocol, and it cares about timers (for example, for retransmitting packets). You must periodically call conn.timeout() to find out when you need to "wake up" the connection next. When the timer fires, you call conn.on_timeout(), after which you try to send packets again via conn.send().

Yes, this is more complex than just calling http.get("..."). But this complexity is exactly what gives you full control over your network's performance and behavior.

Who Should Try Quiche?

Quiche is not the tool you need for writing a simple Telegram bot. It's built for more serious tasks:

  • Network infrastructure developers: proxy servers, load balancers, CDNs.
  • Web server and framework creators: for native HTTP/3 support.
  • Game and real-time application developers: where minimal latency and packet loss resilience are important.
  • Those building custom protocols: QUIC is an excellent foundation for building your own reliable protocols on top of UDP.

Quiche from Cloudflare is a fantastic example of how modern tools like Rust enable the creation of safe and incredibly performant implementations of fundamental internet protocols. This isn't a library for beginners, but for an experienced systems engineer, it's a real Swiss Army knife for working with QUIC and HTTP/3.

If you want to peek into the future of the web, understand how networks work at a low level, or you need a reliable, battle-tested tool for your high-load project — definitely check out the cloudflare/quiche repository. Try running the client and server examples — it's a great way to "get your hands on" the protocol yourself.

Related projects