blazingly fast backends in zig
Thank you to all contributors! Whether you contributed code, documentation, bug reports, testing, ideas, or support for other users, you helped make Zap what it is. Thank you for helping build and improve Zap.
But fear not! An alternative is on the way: Baz — Bounded Async Zap (website & documentation), a pure Zig successor built for Zig 0.16.0, based on bounded/http (website & documentation).
Zap is the zig replacement for the REST APIs I used to write in python with Flask and mongodb, etc. It can be considered to be a microframework for web applications.
What I needed as a replacement was a blazingly fast and robust HTTP server that I could use with Zig, and I chose to wrap the superb evented networking C library facil.io. Zap wraps and patches facil.io - the C web application framework.
After having used ZAP in production for years, I can confidently assert that it proved to be:
zig-master branch. Please note that the zig-master branch is not
the official master branch of ZAP. Be aware that I don't provide tagged
releases for it. If you know what you are doing, that shouldn't stop you
from using it with zig master though.zig build run-docserver to serve them locally.-Dopenssl flag or the environment variable ZAP_USE_OPENSSL=true:.openssl = true, (in dependent projects' build.zig,
b.dependency("zap" .{...}))ZAP_USE_OPENSSL=true zig build httpszig build -Dopenssl=true httpsI recommend checking out the new App-based or the Endpoint-based examples, as they reflect how I intended Zap to be used.
Most of the examples are super stripped down to only include what's necessary to show a feature.
To see API docs, run zig build run-docserver. To specify a custom
port and docs dir: zig build docserver && zig-out/bin/docserver --port=8989 --docs=path/to/docs.
See the other examples for specific uses of Zap.
Benefits of using zap.App:
/slug
of the requested URL and provides a callback for each supported request method
(get, put, delete, options, post, head, patch).try./users endpoint for performing PUT/DELETE/GET/POST operations and listing
users, together with a simple frontend to play with. It also introduces a
/stop endpoint that shuts down Zap, so memory leak detection can be
performed in main().GeneralPurposeAllocator to report memory leaks when ZAP is shut down.
The StopEndpoint just stops ZAP when
receiving a request on the /stop route.zap.Middleware.EndpointHandler. Mixing endpoints
in your middleware chain allows for usage of Zap's authenticated endpoints and
your custom endpoints. Since Endpoints use a simpler API, you have to use
r.setUserContext() and r.getUserContext() with the request if you want to
access the middleware context from a wrapped endpoint. Since this mechanism
uses an *anyopaque pointer underneath (to not break the Endpoint API), it is
less type-safe than zap.Middleware's use of contexts.setUserContext() and getUserContext(), you can, of course use those two in
projects that don't use zap.Endpoint or zap.Middleware, too, if you
really, really, absolutely don't find another way to solve your context
problem. We recommend using a zap.Endpoint inside of a struct that
can provide all the context you need instead. You get access to your
struct in the callbacks via the @fieldParentPtr() trick that is used
extensively in Zap's examples, like the endpoint
example.zap.Router to dispatch to handlers by HTTP path.r.sendError(err, status_code) when you catch an error and a stack trace
will be returned to the client / browser.-Dopenssl=true or the environment
variable ZAP_USE_OPENSSL set to true and requires openssl dev dependencies
(headers, lib) to be installed on the system.ZAP_USE_OPENSSL=true zig build run-https
OR like this: zig build -Dopenssl=true run-httpsClaiming to be blazingly fast is the new black. At least, Zap doesn't slow you down and if your server performs poorly, it's probably not exactly Zap's fault. Zap relies on the facil.io framework and so it can't really claim any performance fame for itself. In this initial implementation of Zap, I didn't care about optimizations at all.
But, how fast is it? Being blazingly fast is relative. When compared with a simple GO HTTP server, a simple Zig Zap HTTP server performed really well on my machine (x86_64-linux):
So, being somewhere in the ballpark of basic GO performance, zig zap seems to be ... of reasonable performance .
I can rest my case that developing ZAP was a good idea because it's faster than both alternatives: a) staying with Python, and b) creating a GO + Zig hybrid.
I used to have some micro-benchmarks in this repo, showing that Zap beat all the other things I tried, and eventually got tired of the meaningless discussions they provoked, the endless issues and PRs that followed, wanting me to add and maintain even more contestants, do more justice to beloved other frameworks, etc.
Case in point, even for me the micro-benchmarks became meaningless. They were just some rough indicator to me confirming that I didn't do anything terribly wrong to facil.io, and that facil.io proved to be a reasonable choice, also from a performance perspective.
However, none of the projects I use Zap for, ever even remotely resembled anything close to a static HTTP response micro-benchmark.
For my more CPU-heavy than IO-heavy use-cases, a thread-based microframework that's super robust is still my preferred choice, to this day.
Having said that, I would still love for other, pure-zig HTTP frameworks to eventually make Zap obsolete. Now, in 2025, the list of candidates is looking really promising.
ZAP is very robust. In fact, it is so robust that I was confidently able to only work with in-memory data (RAM) in all my ZAP projects so far: over 5 large online research experiments. No database, no file persistence, until I hit "save" at the end .
So I was able to postpone my cunning data persistence strategy that's similar to a mark-and-sweep garbage collector and would
No open issues yet, or sync has not completed.