Skip to content
All projects

Platform · 2026

Serverless Scale-to-Zero on Kubernetes

Serverless platforms bill you for work, not for capacity. This gets the same behaviour out of plain Kubernetes: a custom resource points a Redis stream at a container image, and a controller creates exactly as many single-shot Jobs as there are messages waiting.

The idea behind serverless is that idle work should cost nothing. Kubernetes does not give you that by default — a Deployment with one replica is one pod burning memory all night whether or not anything needs doing.

This project gets the serverless shape out of ordinary Kubernetes primitives. You declare a QueueWorker: here is a Redis stream, here is a container image, run at most N of them at once. A controller watches how much work is waiting and creates one short-lived Job per message. When the queue drains, the Jobs finish, Kubernetes garbage-collects them, and nothing is left running.

The problem it solves

A queue consumer is the worst possible fit for normal autoscaling. HPA scales on CPU, but a worker blocked on a queue read is using no CPU while being exactly the thing you need more of. So teams pin a minimum replica count and pay for idle capacity, or they under-provision and the backlog grows quietly.

The signal that actually matters is how many messages are waiting, and the correct floor is zero.

How it works

① web form XADD ② Redis Stream consumer group ③ XINFO GROUPS ④ operator poll, then create ⑤ Job → one message ⑤ Job → one message ⑤ Job → one message depth
Five steps, and the operator never touches a message.

Reading the diagram left to right:

  1. Something enqueues work. In the demo it is a Next.js signup form, but the operator neither knows nor cares what the producer is.
  2. Redis holds the backlog in a stream with a consumer group, which is what gives at-least-once delivery and lets a dead worker’s message be picked up by another.
  3. The operator asks how deep the queue is — and only that. It never reads, processes or acknowledges a message.
  4. It compares depth with the Jobs already running and creates the difference, capped by the concurrency limit on the custom resource.
  5. Each Job runs the worker image once, handles a single message, acks it and exits. A TTL reaps the finished Job.

The division of labour in steps 3–5 is the design. The operator owns how many workers exist; the Redis consumer group owns which worker gets which message, including retries and reclaiming from crashed pods. Neither has to know about the other, which is why the CRD contains no mention of email even though the demo sends email.

The one line worth stealing

Everything above collapses into a single subtraction, and the subtlety is in how depth is obtained:

	depth = g.Pending
	// Lag is unknown (reported <= 0) after XDEL/XSETID; fall back
	// to pending-only rather than over- or under-counting.
	if g.Lag > 0 {
		depth += g.Lag
	}

	// …then, once the Jobs already running have been counted:
	desired := int(minInt64(depth, int64(qw.Spec.MaxConcurrency))) - running

Lag is messages nobody has taken yet; Pending is messages delivered to a consumer but not yet acknowledged — work genuinely in flight, plus anything whose worker died before acking. You need both, and you must not use XLEN — XACK does not remove entries from a stream, so its length only ever grows and a controller trusting it would scale up forever. That one mistake is the difference between this working and a runaway Job loop.

What I would tell you before adopting it

There is no scale-down path because there is nothing to scale down, but there is a cost: a message waits up to one poll interval before a Job is even created, plus scheduling and image pull. That is fine for batch work and wrong for anything a user is waiting on. The poll interval is a latency budget, not a tuning knob.

The full controller, CRD and Helm charts are in the repo.