T0ha666

T0ha666

The GenServer is a cache, Postgres is the queue — why my agent queue survives deploys

I run a kanban of AI coding agents, and one afternoon the board showed three tasks running while the profile page on the same screen said two running and one queued. Both were correct: one card was waiting in line, and nothing in the interface said so. That UI bug turned out to sit on top of a design decision I think is worth sharing.

An agent run costs tokens and a container with a real toolchain, so concurrency has to be capped. The obvious way is to refuse work when you are full and let every caller retry. I did not want that: the pool always accepts a dispatch and makes the session wait its turn. But a queue that never rejects has to live somewhere — and a GenServer holding a :queue in its state is gone the moment you deploy. I deploy several times a week, so every deploy would silently drop every queued session. Not crash, just drop.

So the queue lives in Postgres, as Session rows with status: :queued, and the GenServer state is a fast index over those rows, rebuilt on boot by a reconciler. The state still does real work — a per-user :queue map for O(1) pops, a MapSet of active sessions for cap checks, waiter pids, monitor refs — but none of it is information you could not reconstruct. Delete the whole struct, replay the rows, and you are back where you were.

The one OTP feature I actually rely on: every waiter is monitored on enqueue, and handle_info({:DOWN, ...}) releases its slot and dispatches the next in line. Inside the VM, “notice when the thing holding a resource dies” is four lines, and they are exactly right.

Two rules I took out of it: if it must survive a restart, it is a row — kill the process and ask what was lost. And a queue that never rejects has to be visible, or your users think the app is broken while it works exactly as designed.

Full write-up with the code:
https://t0ha.dev/en/elixir/2026/10/29/genserver-is-a-cache.html

Where do you keep your queues — have you ever lost one to a deploy, or do you just refuse work when you are full?

Where Next?

Popular Backend topics Top

dimitarvp
Apparently he decided to live-stream how he’s going to create a semver library.
New
CommunityNews
Is Zig the Long Awaited C Replacement. Comparison with previous C contenders such as C++, D, Java, C#, Go, Rust and Swift https://erik...
New
AstonJ
Just listening to this now… Totally agree with @FrancescoC’s and @thompson_si’s comment “learn to learn” :sunglasses: In our talk we’...
New
First poster: bot
This post is a spiritual successor to Loris Cro’s Go cross-compilation. The encounter During a recent stage 2 meeting Jakub Konka wanted...
New
New
pablocostass
Todos coñecemos os focos de Erlang/Elixir máis renomeados do mundo, como a Suecia, o Brasil, a California ou Londres. Mais a comunidade, ...
New
axelson
I describe how we use Hot Reloading with Webpack to develop faster and show how to integrate Webpack 5, webpack-dev-server, and Phoenix f...
New
chikega
Mark Hoffman, the author of Programming WebAssembly in Rust, is a pretty hilarious lecturer if you like a dry sense of humor.
New
fullstackplus
The Ruby ecosystem is rich with tools that make us developers more productive at what we do. Both Rails and Sinatra have been used to bui...
New
penelopa
Learn how set up an RTMP server for free using the open-source Red5 software. This tutorial covers all steps from downloading the code fr...
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
503 15149 280
New
PragmaticBookshelf
Tailwind CSS is an exciting new CSS framework that allows you to design your site by composing simple utility classes to create complex e...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
RobertRichards
Hair Salon Games for Girls Fun Girls Hair Saloon game is mainly developed for kids. This game allows users to select virtual avatars to ...
New
PragmaticBookshelf
This book forgoes the abstract and instead provides concrete examples to help you better leverage the unique properties of Elixir, Erlang...
New
PragmaticBookshelf
Use advanced functional programming principles, practical Domain-Driven Design techniques, and production-ready Elixir code to build scal...
New
pragdave
Build robust LLM-powered apps, chatbots, and agents while mastering AI engineering principles that will help you outlast the tools and th...
New
PragmaticBookshelf
Write elegant Ruby code—not just correct, but idiomatic. Go beyond the vocabulary and syntax and learn to express yourself with eloquence...
New