# Subzed - High Performance Linux Static HTTP/1.1 Server

**URL:** <https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066>\
**Category:** Showcase\
**Created:** [January 30, 2024, 8:52pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066 "2024-01-30T20:52:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [January 30, 2024, 8:52pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/1 "2024-01-30T20:52:44Z")

</div>

> **[subzed](https://codeberg.org/dude_the_builder/subzed)**
>
> High Performance Linux Static HTTP/1.1 Server

Experimenting with the build system on one hand and Linux io\_uring on the other. `subzed` is a very basic HTTP/1.1 web server that generates all the HTTP responses for your static site, embeds them into the binary executable, and serves them at blazingly fast speed from memory. I got around 175k requests per second on a not-so-new Linux box.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [January 31, 2024, 8:50am UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/2 "2024-01-31T08:50:17Z")

</div>

> [@dude\_the\_builder](#):
>
> I got around 175k requests per second on a not-so-new Linux box

This number is more or less in accordance with an epoll/uring comparison (echo-server):

 ![uring-epoll](https://ziggit.dev/uploads/default/original/2X/0/08c87f990dae2308b4be78cd8be0c10804206272.png)

Taken from [here](https://habr.com/ru/articles/589389/) (in Russian, 2021)

---

<div class="post-metadata">

**Author:** ![Cloudef](https://ziggit.dev/user_avatar/ziggit.dev/cloudef/32/1370_2.png) [@Cloudef](https://ziggit.dev/u/Cloudef)\
**Post date:** [January 31, 2024, 8:51am UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/3 "2024-01-31T08:51:40Z")

</div>

Great, I’ve been wanting to learn iouring and this is great resource. Is the whole flow apart from parsing HTTP handled by kernel?

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [January 31, 2024, 8:55am UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/4 "2024-01-31T08:55:14Z")

</div>

> [@dude\_the\_builder](#):
>
> … embeds them into the binary executable …

I think, it would be more interesting to use `io_uring` both for sockets i/o and for file i/o.

---

<div class="post-metadata">

**Author:** ![Cloudef](https://ziggit.dev/user_avatar/ziggit.dev/cloudef/32/1370_2.png) [@Cloudef](https://ziggit.dev/u/Cloudef)\
**Post date:** [January 31, 2024, 8:58am UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/5 "2024-01-31T08:58:37Z")

</div>

I was thinking about this and was confused for not finding any file iops, until I reread the first post. It might be actually faster using files as you can skip writing bytes to kernel (sendfile).

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [January 31, 2024, 9:20am UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/6 "2024-01-31T09:20:21Z")

</div>

`sendfile` was a workaround invented with the purpose of reducing the number of user space ↔ kernel space data copying: “traditional” way to send a file assumes at least 2 transfers between the spaces, one is when we read a file content and the second when we supply this content to a socket API; `sendfile` eliminates redundant data copying. `io_uring` is a general mechanism for async i/o, whereas `sendfile` is just an optimized way of copying data between two file descriptors, it has not to do with async ops.

---

<div class="post-metadata">

**Author:** ![Cloudef](https://ziggit.dev/user_avatar/ziggit.dev/cloudef/32/1370_2.png) [@Cloudef](https://ziggit.dev/u/Cloudef)\
**Post date:** [January 31, 2024, 9:48am UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/7 "2024-01-31T09:48:48Z")

</div>

Right, what I meant to say, since I’m unfamiliar with iouring API in general, is that I was hoping to find something equivalent to sendfile done with iouring so the whole connect, accept, send is done in kernel in a single syscall (or in few as possible).

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [January 31, 2024, 10:38am UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/8 "2024-01-31T10:38:09Z")

</div>

> [@Cloudef](#):
>
> I’m unfamiliar with iouring API

me too 🙂

> [@Cloudef](#):
>
> so the whole connect, accept, send is done in kernel in a single syscall (or in few as possible)

I think [this](https://developers.redhat.com/articles/2023/04/12/why-you-should-use-iouring-network-io) might be useful. IMHO, asynchronous way of doing I/O requires total rethinking of an application architecture.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [January 31, 2024, 1:25pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/9 "2024-01-31T13:25:42Z")

</div>

Yes, it does combine multithreading with io\_uring by having each thread have its own separate ring. The main thread only accepts connections on its ring and then passes the socket fd to the worker threads via a mutex-protected FIFO queue (basically a poor man’s channel).

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [January 31, 2024, 1:31pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/10 "2024-01-31T13:31:13Z")

</div>

This server doesn’t even read files ever. It just sends bytes directly from memory every time. The files are read by the build system, where they are cpmpressed if needed and prepared as canned HTTP responses placed in a `ComptimeStringMap` for fast retrieval.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [January 31, 2024, 1:34pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/11 "2024-01-31T13:34:10Z")

</div>

This server is using the Zig standard library io\_uring wrapper of the Linux io\_uring low-level API. There are still some newer aspects missing that would in theory allow for zero syscall operation. But you could use C’s liburing directly for that if needed.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [January 31, 2024, 1:36pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/12 "2024-01-31T13:36:41Z")

</div>

That’s serving 128 and 512 bytes so I suspect this server would then perform even better in that scenario. My benchmark was done serving a real 9.8 kilobytes index.html file. I’ll try to run it with 128 and 512 to see what I get.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [January 31, 2024, 1:50pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/13 "2024-01-31T13:50:36Z")

</div>

> [@dude\_the\_builder](#):
>
> This server doesn’t even read files ever

Yes-yes, I understand. I mean you hardly will use such an approach (full HTTP-replies prepared in advance in RAM) for big files like video or so. And that is why I noted that it would be also interesting to utilize io\_uring both for network and for file storage.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [January 31, 2024, 6:56pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/14 "2024-01-31T18:56:19Z")

</div>

> [@dude\_the\_builder](#):
>
> Yes, it does combine multithreading with io\_uring by having each thread have its own separate ring

If I got it right, each thread at any moment of time is serving exactly one client.  
But then there is no need to multiplex i/o (taking into account that you do not touch file storage at all). I/O multiplexing is for concurrency within single thread, isn’t it?..

When we want to serve many clients concurrently, we can

- serve one client by one process/thread (PG, for ex.) and let them block, concurrency will be arranged by OS.
- serve all clients in one thread using I/O multiplexing, no matter, reactor/proactor (redis for ex.)
- use several processes/threads (not many, one per CPU core or so) and serve many clients in each thread concurrently (nginx for ex.)

There is [old text about C10K problem](http://www.kegel.com/c10k.html), just in case.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [January 31, 2024, 9:13pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/15 "2024-01-31T21:13:50Z")

</div>

> [@dee0xeed](#):
>
> If I got it right, each thread at any moment of time is serving exactly one client.

No, each thread can be serving many clients at once concurrently via its own io\_uring. So it’s like taking your single threaded async model and multiplying it by the number of cores on the machine. The main thread is the dispatcher of accepted connections, sending them via the queue to the worker threads. The worker threads loop over both the io\_uring completions _and_ the queue from the main thread, so they can for example submit a first receive to one client and in the next iteration submit a send to another, and in the next receive again from the previous, etc. Whatever the kernel throws at them first or if something arrives at the queue.

I saw in a video about ew features in io\_uring that there’s a new event type that lets you send messages directly between rings in the kernel. This would eliminate the need for the mutex protected queue between main thread and workers, possibly resulting in ever better performance. But Zig’s io\_uring layer in std doesn’t have this yet.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [January 31, 2024, 9:21pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/16 "2024-01-31T21:21:19Z")

</div>

> [@dude\_the\_builder](#):
>
> No, each thread can be serving many clients at once concurrently via its own io\_uring. So it’s like taking your single threaded async model and multiplying it by the number of cores on the machine

Ah, ok, that’s the right architecture! I just did not realized this at first, sorry.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [January 31, 2024, 9:41pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/17 "2024-01-31T21:41:02Z")

</div>

No worries my friend. I still have to test several other methods of handling the events. For example, would it be better to collect several operations (sends, receives, accepts) and then submit them all in batch versus submitting them individually immediately when they are ready to be processed?

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [February 1, 2024, 7:14am UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/18 "2024-02-01T07:14:14Z")

</div>

I guess It depends on how your clients work, I mean:

- connect, send-request, get-reply, disconnect cyclically
- connect then send-request, get-reply cyclically, then disconnect
- do clients use http pipelining? this usually complicates things on the server side

---

<div class="post-metadata">

**Author:** ![mperillo](https://ziggit.dev/user_avatar/ziggit.dev/mperillo/32/1280_2.png) [@mperillo](https://ziggit.dev/u/mperillo)\
**Post date:** [February 7, 2024, 9:37pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/19 "2024-02-07T21:37:49Z")

</div>

> [@dude\_the\_builder](#):
>
> [dude\_the\_builder/subzed: High Performance Linux Static HTTP/1.1 Server - Codeberg.org](https://codeberg.org/dude_the_builder/subzed)
> 
> Experimenting with the build system on one hand and Linux io\_uring on the other. `subzed` is a very basic HTTP/1.1 web server that generates all the HTTP responses for your static site, embeds them into the binary executable, and serves them at blazingly fast speed from memory. I got around 175k requests per second on a not-so-new Linux box.

Looking at the code, I noted that Zig std does not support `recv_multishot`, `recvmsg_multishot` and `read_multishot`.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [February 7, 2024, 10:18pm UTC](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066/20 "2024-02-07T22:18:48Z")

</div>

Yes, I actually was uncertain on whether to use liburing directly to have the latest features at hand, but finally stayed with what’s available in `std` whith hopes those updates will come sooner than later. As a sign of hope, `accept_multishot` is already there!

[Next page](https://ziggit.dev/t/subzed-high-performance-linux-static-http-1-1-server/3066.md?page=2)
