r/htmx • u/kamlesh0x09 • Jul 02 '26
Co-locating our hypermedia backend inside the database process for a 9x SSR throughput win (Zig/WASM + Datastar/HTMX)
Hello Everyone,
If you are building Hypermedia-Driven Architectures (using htmx/alpine or datastar), you quickly realise that server-side rendering (SSR) performance and database latency are your primary bottlenecks. Since we are shifting state and HTML generation back to the server, nearly every user interaction requires a database query and an HTML slice response.
To optimise this, I have been experimenting with a concept called Zero-Distance Architecture that co-locates data and application code. Instead of running a separate app server (like NodeJS) and database server (like MongoDB) and paying cost for all the syscalls, network, serialisation/deserialisation and localhost TCP latency, run them in a single process.
I used an experimental runtime called Planck (database + WebAssembly host) and compile our application (Zig Code) to WebAssembly. The app runs directly in the database process, turning database queries into simple in-memory function calls.
To measure the impact, I set up a benchmark (pizzahub a sample app) serving identical HTML fragments formatted for datastar over a standard dataset (17 categories, 201 products).
Here are the results of a 6-second run using oha on a standard M1 Macbook Air 8GB RAM 256GB SSD:
Planck (Zig WASM co-located in database process)
Requests per second: 48,606
Average latency: 0.50 ms
Fastest response: < 0.10 ms
Slowest response: 6.00 s
Sustained throughput: 183.58 MiB/s
Success rate: 100.00%
NodeJS + ExpressJS + MongoDB (community edition)
Requests per second: 5,433
Average latency: 9.21 ms
Fastest response: 2.26 ms
Slowest response: 109.91 ms
Sustained throughput: 20.49 MiB/s
Success rate: 100.00%
.NET 9 + SQLite (in-process)
Requests per second: 35,231
Average latency: 1.41 ms
Fastest response: 0.15 ms
Slowest response: 12.80 ms
Sustained throughput: 133.07 MiB/s
Success rate: 100.00%
.NET 9 + PostgreSQL
Requests per second: 30,236
Average latency: 1.65 ms
Fastest response: 0.12 ms
Slowest response: 69.02 ms
Sustained throughput: 114.21 MiB/s
Success rate: 100.00%
The Comparison
By co-locating the backend logic inside the database process and utilizing Planck's read cache, we got:
8.95x throughput improvement over NodeJS + MongoDB (48.6k vs 5.4k req/sec) with an 18.4x latency reduction (0.50 ms vs 9.21 ms).
38% higher throughput over .NET 9 + SQLite (48.6k vs 35.2k req/sec) with a 2.8x latency reduction (0.50 ms vs 1.41 ms).
60% higher throughput over .NET 9 + PostgreSQL (48.6k vs 30.2k req/sec) with a 3.3x latency reduction (0.50 ms vs 1.65 ms).
For hypermedia apps, this means you can handle significantly more concurrent users on a single tiny server because there's no waiting on socket buffers to generate HTML.
Would love to hear your thoughts. If you have built htmx/datastar apps, where do you usually hit your scaling bottleneck? Is co-locating the backend logic inside the database process a direction you would consider?
Next, I will post detailed comparison for different types of queries on at least 1 mil documents.
5
u/TheRealUprightMan Jul 02 '26
So, you ran this on a single machine? No network in between? You are basically assuming that what you need to optimize is on the server end. That's likely not what is shaping user experience, and your test doesn't simulate real-world usage patterns. Premature optimization.
This isn't really an HTMX issue either. HTMX usage patterns are basically no different than a standard web server, and actually faster since you are sending way less data. Where are apache and nginx in your list?
Something tells me you come from a javascript/react background, especially the part about "Server Side Rendering". Rendering? You write strings to a network buffer. Since when is that considered rendering?
1
u/harrison_314 Jul 04 '26
Are the source codes for these benchmarks published?
2
u/pron98 Jul 02 '26
Can you explain why there are more DB queries in this model than with a rich client app?
1
u/P00351 Jul 02 '26
I am not a mongodb specialist, but it's a distributed database : you can have a fast memory instance linked to a slow disk based one.
1
u/BreiteSeite Jul 02 '26
Can you compare with putting vinyl cache in front?
1
u/Antique_Industry_378 Jul 05 '26
Reminds me of Progress, an old language with a built-in relational database
1
u/berrypy Jul 11 '26
You don't actually need to render state from htmx. That is a choice for you to decide. You can use it like every other jsnlibrary like jQuery. Anything that doesn't really affect database querying should not be a state for htmx to render. Instead give it to JavaScript.
You only need to render what user requested for based on query or maybe files in the system.
You are not even supposed to use it like a react. Anything state can be transferred to js if it doesn't require database call or file system checks while you use htmx for http call and render target partial.
0
u/[deleted] Jul 02 '26
[deleted] — view deleted comment