跪拜 Guibai
← All articles
Backend

A Zig Web Skeleton That Ships with MySQL, Auth, and OpenAPI — and Hits 60K RPS on an M1

By 唐青枫 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A 10× throughput gap over a mainstream framework on the same hardware changes the cost calculus for high-throughput services. The skeleton also demonstrates that Zig’s comptime can replace entire categories of runtime machinery — DI containers, reflection-based doc generators, and dynamic middleware ordering — with compile-time checks that fail fast.

Summary

wing-app bundles zio, talon, wing, and mantle into a layered backend template that handles routing, typed request extraction, database connection pooling, migrations, and server-side sessions. Dependencies are assembled explicitly in AppState and projected into handlers by type at compile time — no runtime DI container. The project also generates OpenAPI 3.1 specs directly from handler signatures and serves an interactive Scalar doc page.

A quick benchmark on an Apple M1 shows the skeleton pushing over 60,000 requests per second, roughly 10× the throughput of a comparable .NET Minimal API running on the same machine. The codebase is still pre-release and depends on sibling-directory builds of wing and mantle, but the architecture is production-shaped: config via environment variables, cookie security attributes set, and error-to-status-code mapping centralized in one file.

Takeaways
wing-app layers routes, controllers, services, and repositories in a conventional backend structure that stays readable as the project grows.
Handlers declare dependencies as typed function parameters; the framework projects them from AppState at compile time, so wiring is explicit and auditable.
MySQL access uses the pure-Zig mantle driver with a configurable connection pool, and table migrations are included as SQL files run at startup.
Server-side sessions are created on login, returned as HttpOnly/Secure/SameSite=Strict cookies, and enforced through an Auth extractor in handler signatures.
Swapping wing.Router for openapi.Router and adding a doc parameter generates an OpenAPI 3.1 spec from handler signatures, with a Scalar UI served at /docs.
On an Apple M1, the skeleton hits 60K+ RPS versus 5,856 RPS for a .NET Minimal API, a 10× difference.
Middleware ordering is validated at compile time, and error-to-HTTP-status mapping is centralized in app.zig rather than scattered across controllers.
The project currently depends on sibling-directory builds of wing and mantle and requires Zig 0.16.0, placing it in a pre-release co-development phase.
Conclusions

Zig’s comptime lets a web framework absorb work that other ecosystems hand to runtime reflection — DI assembly, OpenAPI schema generation, and middleware ordering checks all happen before the binary runs.

Explicit dependency projection by type is a pragmatic middle ground: it avoids both manual wiring boilerplate and the opaque magic of runtime containers, at the cost of requiring distinct wrapper types when two dependencies share a type.

Deriving API docs from handler signatures treats code as the single source of truth, which directly attacks the doc-rot problem that plagues projects where specs are maintained separately.

The 10× throughput delta isn’t just a language benchmark — it suggests that for I/O-bound services, Zig’s lack of a heavy runtime and its async model can translate into real hosting-cost differences.

Shipping with real MySQL, migrations, and session management signals that the Zig web ecosystem is moving past ‘can it serve HTTP?’ toward ‘can it run a production service?’ — even if the dependency story hasn’t stabilized yet.

Concepts & terms
comptime
Zig’s compile-time execution mechanism. Code marked comptime runs during compilation, enabling type introspection, code generation, and validation that other languages defer to runtime reflection.
typed extractors
Framework components that parse HTTP request parts (path, query, body, headers) into typed Zig structs based on a handler’s parameter types, making request structure explicit and compile-time-checkable.
AppState projection
A pattern where a handler declares a pointer to a dependency type, and the framework resolves it from a central AppState struct at compile time by matching the type, avoiding a runtime DI container.
mantle
A pure-Zig MySQL driver that provides connection pooling and query execution without C dependencies, used in wing-app for database access.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗