Egres: A Postgres-compatible database for heavy, multi-tenant workloads, written in Zig

Egres is a Postgres-compatible database designed to tackle heavy workloads and run thousands of tenants on the same server. It works with psql and drivers unchanged. Right now the query ability is limited, and would grow with future releases. The following post goes into details about the what, why and how; which I hope you’d find interesting.

Before Egres, I was the founder of Dgraph – author of Dgraph and Badger. ex-Google, ex-Uber. So, been working on databases for a while now. The idea for Egres all started with a question: “What would Postgres look like if it were to be designed today?” After years of pondering and a year of work, the result is Egres.

For Egres, I wrote a new storage engine called zdb, which is pioneering LSM forest concept – a tree of LSM trees. zdb can run on local disk or object storage, has native roaring bitmaps integration, can do instant zero-copy branches for database or table.

All of this runs on a custom co-routine system I wrote called zfiber, so all queries execute on that. Egres meters CPU ticks, disk IOPS and memory used per query, per tenant, so it can run thousands of tenants on the same server without noisy neighbor problem.

Zig has been instrumental. The explicit memory allocator makes it possible to meter the memory usage of the query and error out if needed, without crashing/panicing the whole process. I also really like the look and feel of the codebase, and ability to easily integrate C libraries.

This release is single node, but Raft replication is already baked in. Future releases would allow multi-node clusters. Give it a try! Would love to know your thoughts.

Supported Zig versions

Zig v0.16

AI / LLM usage disclosure

Most of the Egres code base was written using AI agents: Claude Opus and then Fable. I reviewed every live code path myself, line by line, and iterated on it until it was correct and struck the right balance between performance and readability.

5 Likes

Congrats on the first release. What is the target use case for this? Is it really aiming to build a full relational DB?

That is the goal, yes. A full relational DB that speaks Postgres, so existing tools and drivers keep working, making it’s easy to adopt Egres or switch from Postgres. The work is in getting the right design to avoid the various issues Postgres faces in production, and also pushing the frontier of latest technology like object storage and multi-tenancy, for example, with metered resource usage, replicated compactions, LSM forest, etc.

Oh.. and thanks for building zio! I started with it, and after building zfiber, am still using the coro module from zio.

2 Likes