Join the waitlist

Let us know how we should get in touch with you.

Thank you for your interest! We’re excited to show you what we’re building very soon.

Close
Oops! Something went wrong while submitting the form.

Idle and starving: fixing an overprovisioned Kubernetes fleet

Robert Wells
·
Updated on: December 4, 2026

In September our Kubernetes pods had reserved about 840 CPU cores and were using about 90. Memory was the same story: about 2,600 GiB reserved, about 1,000 in use. Around the same time, 19 of our worker pools were pinned at their maximum replica count while 14 never left their minimum. We had already made the obvious move and raised maximums, and it bought more compute without relieving the pressure. That was what started this work. A fleet using 11% of its reserved CPU already has the capacity, and the problem was that the work couldn't reach it. Idle and starving, on the same dashboard, at the same time.

Bar charts and tallies of our Kubernetes fleet before the rebuild: pods reserved 836 CPU cores and used 90 (11%), reserved 2,622 GiB of memory and used 1,028 GiB (39%), while 14 worker pools never left minimum replicas and 19 were pinned at maximum replicas.

The work always got done, because most of it is asynchronous and Temporal is durable: a pool that fell behind finished its work late and lost nothing. We were just paying a lot of money to do it the slow way.

That left one question: why couldn't the work reach capacity that was sitting idle? The answer had little to do with compute. A workflow was so expensive to hold in memory that pods had to be large, our capacity was partitioned into pools that couldn't share work, and the autoscaler was scaling on a signal that didn't track load.

Diagram of three causes that kept work from reaching idle capacity, each with its fix. 19 worker pools were pinned at maximum replicas while 89% of reserved CPU was unused. The causes were about 60 MiB of memory per cached workflow (fixed with subpath imports, under 2 MiB), capacity partitioned across four priority queues and four worker pools (fixed with one fair queue and one pool), and a scaling signal that tracked cache state, not load (fixed by scaling on queue backlog, plus cache misses where needed).

Together those gave the fleet the wrong shape. Our pods were large and held at high minimums in case of a burst, and they stayed that way around the clock, which is what you get from accepting sensible defaults while a company grows quickly. Three properties of our workload dictate what the shape should have been:

  • Our workers run on Node.js, and a Node.js worker runs workflow code on a single thread. A four-core pod can't use most of its cores, so capacity has to come from scaling horizontally: more pods, not bigger ones.
  • Most of the work is asynchronous and can tolerate the minute or two an autoscaler needs to react. Synchronous services, such as our API servers, can't wait that long, so they stay warm with spare capacity.
  • The load follows the workday. Nobody sends outbound at midnight, so we only need the whole fleet humming during peak business hours, when the east and west coast workdays overlap.

So the shape we wanted was small pods, low minimums, high ceilings, and scaling responsive enough to cover the distance between the two. KEDA adds pods when work arrives and Karpenter provisions the nodes under them. With tools that good, there is no reason to keep big pods idle the rest of the day.

Over September and October we rebuilt the fleet into that shape: about 140 Kubernetes deployments on EKS, running roughly 20 million Temporal workflow tasks on a weekday. The results:

  • Reserved CPU fell by about half, measured both at the pools' minimums and at the daily average.
  • We run about 12 nodes on an average day, down from 32, and our EC2 compute bill is down by nearly a third with 10% more work going through.
  • The pool that runs Plays, our flagship feature, now follows the day: about 30 pods in the early afternoon and 10 overnight, where four pools used to hold 47 around the clock.

And the pools stopped starving:

  • Schedule-to-start latency, the time a task waits for a worker to pick it up, went from about 3.8 seconds to under a tenth of a second on a typical weekday.
Line chart on a log scale: fleet-wide workflow-task schedule-to-start latency fell from a median weekday of 3.8 s (Sep 15 to 25) to 0.09 s (Nov 2 to 13).

Very little of this came from tuning Kubernetes. The causes were in how our applications used the capacity, and we could only turn the settings down once the code needed less. The rest of this post works through the three causes in turn: pod size, partitioned capacity, and the scaling signal. Then it shows the new shape running, what it exposed, and the method we used to get there.

What sets the size of a pod

Start with why the pods were large. A pod has to be sized for its worst case. For one of our workers that is a fixed baseline, plus the memory per cached workflow multiplied by the number of workflows it caches, plus headroom for the largest single piece of work that can land on it. A small pod means reducing the two terms you control.

Diagram of what sets the memory size of a worker pod, before and after: memory per cached workflow fell from about 60 MiB to under 2 MiB, headroom shrank as outliers were bounded and concurrency per pod was reduced, and the fixed baseline stayed close to 1 GiB.

The memory per workflow was the surprise. We expected it to be dominated by payloads, the data our workflows pass between steps. We built a small test rig to measure it, and payloads accounted for a small fraction. The rest was our own code. The call was coming from inside the house.

The Temporal TypeScript SDK loads a fresh copy of your workflow code for every workflow a worker holds in its cache, and ours imported a single enum through the root barrel of a shared package, which pulled in the entire package. Each cached workflow cost about 60 MiB, where Temporal's own reference figure is about 1 MB. Anywhere else that import is harmless, because a server loads a package once. Here the cost was paid once per cached workflow, and tree shaking doesn't help, because the SDK bundles workflow code in webpack's development mode, which eliminates nothing.

// before: pulls the whole package into every cached workflow
import { FeatureFlag } from '@unifygtm/shared';

// after: a subpath export with only what the workflow needs
import { FeatureFlag } from '@unifygtm/shared/flags';

We changed the imports and nothing else, then replayed the recorded histories of 67 production workflows against the new code to verify that it behaved identically. We also ran a deliberately broken version to prove the check could fail. On the first service, memory per cached workflow went from about 60 MiB to under 2. Across all our workers, memory use fell about 60%. A test in every service now fails the build if one of those imports comes back.

Bar chart of heap per cached Temporal workflow for 18 worker services before and after the import change, for example Service A falling from 60.7 MiB to 1.7 MiB, against Temporal's reference figure of 1 MB.

Concurrency came next: how much each pod takes on at once. We were running the SDK's defaults of 40 workflow tasks and 100 activities per pod. At peak hours, fewer than three of those slots were in use per pod on nearly every pool, so the limits were inert on a normal day. On a bad one they let a burst concentrate on a single pod, because Temporal routes most tasks back to the worker that already holds the workflow, and a pod with spare slots keeps accepting them. The Plays pool now takes 10 and 20, and the other pools we rebuilt were reduced the same way.

The worst case takes the most patience. Pods are sized for the tail of the distribution, so a single outlier sets the memory limit for every pod in its pool. We hunted the outliers down one at a time. A 21 MB email, processed in a batch of ten like every other message, could push a mail-sync pod past its memory limit. We changed that pool's concurrency from a message count to a byte budget, so small messages still run ten at a time and a very large one runs alone. In a test with that message, peak memory went from 2.6 GiB to 620 MiB. An AI step re-parsed its whole response each time a new fragment streamed in, so its memory grew quadratically with the length of the response until we removed the re-parsing. Neither was typical, but each was setting a pod size.

Any pod should be able to take any work

Small pods only help if the work can reach them. Plays ran on four priority queues, each with its own pool of workers, all running the same code. We wrote about those queues in June. They did isolate one busy customer from everyone else, at the price of partitioning our capacity four ways. During the busiest hours of October 1, three of the pools used 1 to 13% of the CPU they had reserved. The fourth, the smallest, took an average of two minutes to start a task.

Diagram: four Plays priority queues fed four pools of about 47 replicas in total, three idle at 1% to 13% CPU utilization and one with a schedule-to-start latency of two minutes, now consolidated into one queue and one pool of about 17 replicas on average.

The replacement was one queue. Temporal can share a queue fairly between customers, so one busy customer can't crowd out the rest. We moved the workers that send email sequences onto a fair queue soon after Temporal released the feature, but Plays stayed where it was. A Play can be almost anything a customer builds, and at 60 MiB per workflow that workload was too variable for a single pool, however fairly the queue was shared, because a fair queue decides who goes next and does nothing about what a workflow costs to hold. Once a workflow was cheap to hold, one pool had room for everyone, and we added a gate that lets work already in progress go ahead of work that hasn't started.

The import fix and the single queue ran side by side and shipped a day apart. We validated the single queue in staging, then on our own accounts, then migrated every customer in a day behind a feature flag.

Scale on a signal you can trust

Responsive scaling depends on the autoscaler watching the right signal. Ours had been adding pods whenever memory utilization ran high, which is one of those sensible defaults: for most services, high memory means busy. But a worker with a full cache holds a lot of memory whether it is busy or idle, so for our workers memory was a proxy for cache state, not for load. We've removed that trigger from more than a third of our pools so far, wherever the measurements supported it.

The obvious replacement is queue depth: add pods when a backlog forms. However, that has a blind spot. About 80% of workflow tasks are what Temporal calls sticky: it routes them straight back to the worker that holds the workflow, so nothing has to be replayed. Those tasks never appear in the queue the autoscaler watches.

Diagram: about 80% of workflow tasks are sticky and are routed directly to the worker that has the workflow cached, bypassing the task queue that a queue-based autoscaler scales on.

The lower concurrency limits from earlier closed most of that blind spot. When a pod has only a few slots, the sticky tasks it can't take overflow into the shared queue, where the autoscaler can see them. For most of our pools that is enough to make queue backlog a trustworthy signal, and it is how the Plays pool scales. One pool needed more. Scaled on the queue alone, it sat at its minimum of four pods with a full cache, while up to a tenth of its tasks landed on a worker that had evicted the workflow and had to replay its history to rebuild it. The queue looked empty while the pods thrashed. That pool now has a second trigger alongside the queue: when cache misses exceed 75 per pod over ten minutes, it scales out.

We scale out as soon as the signal fires and scale in slowly, because a worker that is removed takes its cache with it, and every workflow it held has to be replayed on another pod. Giving capacity back too eagerly would recreate the cache misses we had just removed.

The shape, running

The Plays pool shows the pattern: 5 to 90 pods of 1.5 cores each. It scales in by a tenth at a time, after fifteen quiet minutes.

Line chart by hour of day: before, four Plays pools held 45 to 52 replicas at every hour; after, one Plays pool runs about 30 replicas in the early afternoon and about 10 overnight.

The day we migrated every customer onto it, a burst of 5,700 Play executions arrived, and the pool went from 5 pods to 40 without a single pod restarting. In a larger burst in November, 45,000 tasks arrived in two minutes. The pool went from 5 pods to 90 in about three minutes, and by then tasks were starting in 11 milliseconds.

Step diagram: when 45,000 workflow tasks arrived in two minutes, the Plays worker pool scaled out from 5 to 90 replicas in about three minutes, with a schedule-to-start latency of 11 ms.

We started with Plays because it was the largest block of idle capacity we had. Then we applied the same recipe to every deployment, servers and consumers as well as workers, under rules we wrote down first: size each pod from its peak hours rather than its average, add 25% headroom, never remove more than half of a deployment's minimum in one step, and observe every change for 48 hours. Six deployments didn't qualify for a smaller size under those rules, and we left them alone. One change did bite: lowering a fleet-wide memory default left one server short.

Across the fleet, reserved CPU fell from about 650 cores to about 290 at the pools' minimums, and from about 840 to about 430 on average. Fewer reservations became fewer nodes: the small burst nodes went away while the large base nodes stayed, and the EC2 bill followed.

What low minimums exposed

When we halved the minimum on our background-jobs pool from eight pods to four, tasks on its queue began waiting seconds to start where they had previously waited about 12 milliseconds. The delay came in short bursts, and a cache alert on that pool pointed us to it.

Our first hypothesis was that the cache was too small, so we doubled it as a test. Two of the next three bursts improved and the largest didn't, which implicated the burst itself. Two background jobs, one of them two years old, each fanned out a separate workflow per record, up to 2,000 at once. Eight pods had always absorbed that. Four did not.

We capped both fan-outs at a concurrency of 25. In a test with 2,000 records, the average wait went from 11 seconds to 31 milliseconds. In production the next morning the queue was back to about 12 milliseconds. We have since halved the minimum again, to two pods, and it has held there, balancing cache warmth, availability, and cost. We also replaced the cache alert with one on late pickup, which is the symptom we care about.

Step diagram: a background-jobs worker pool went from minimum replicas of 8 with a schedule-to-start latency of about 12 ms, to 4 with latency in seconds, to 2 at about 12 ms once fan-out concurrency was capped at 25.

You only find out what your slack was paying for once it's gone. That is why we lower minimums in steps.

The method

Thinking about cost can be a distraction when the goal is to cut it. A bill only tells you what you spent, so we studied the system.

The foundation was alerting we could trust. We established a simple alerting doctrine:

  1. Every deployment gets the same standard set of monitors by default. They are defined in code, in Helm charts and Terraform, so humans and agents alike can inspect them.
  2. Every alert that fires is actionable. A noisy alert is a defect with exactly two honest resolutions: if the condition is unacceptable, fix the system, and if it is acceptable, fix the monitor.
Decision diagram: when an alert fires and the underlying condition is not acceptable, fix the system; when it is acceptable, fix the monitor; tolerating the noise is never an accepted end state.

Burning down alerts under that doctrine gave us positive control of the fleet, and it kept handing us work worth doing, because every alert we ran to ground was an inefficiency fixed on its own merits. One odd line in the logs, traced to its source, gave back a few hundred database connections and retired a worry about connection limits that we had never set out to address. If you want to cut costs, do good engineering.

The mantra I kept repeating to the team was "the COGS are in the code." Our pods got small because the code changed first: an import, a byte budget, a capped fan-out. You eat your vegetables in the code, then you enjoy your cake in the Helm charts.

We didn't have a map for any of this, and you don't need one. We had a machete and a method: measure first, form a hypothesis, find the cheapest test that could falsify it, and only then build on it. Two things made that affordable. One was coding agents, which did most of the legwork at every turn of the loop: pulling the measurements, building test scaffolding such as the memory rig and the workflow replay checks, writing the changes, and watching each one after it shipped. Testing a hypothesis cost little. The other was shipping risky changes in two steps: first a change that only adds tests pinning down how the code behaves today, then the change itself behind a feature flag.

Cycle diagram of the method: measure, form a hypothesis, run the cheapest test that could falsify it, ship in steps, observe for 48 hours, then return to measure, never removing more than half of a minimum in one step.

It took a small group about two months, and the engineers who own each service reviewed every change to it. The sizing rules now live in a runbook that every sizing change follows.

The fleet rebuild was one piece of a larger effort. The same method ran across our databases, our Temporal usage, and our vendors at the same time, and together they took about 30% off our total hosting bill.

If you want to copy it

If your fleet is idle and starving at the same time, more compute won't fix it. Find what keeps the work from reaching the capacity you already have.

Your numbers will not be ours. Every pod has a fixed cost before it does any work, and every autoscaler takes time to react, so small and responsive both have floors. Measure yours before you set a target.

The rules we'd start from:

  • Establish observability first. It is the difference between testing a hypothesis and making a guess.
  • Then go to the code. A pod can only be as small as the code running in it allows.
  • Size a pod for its worst case, then go and shrink the worst case.
  • Let any pod take any work before you tune anything else.
  • Check what your autoscaler is reacting to.
  • Lower a minimum by half at a time, and look at what it exposes.

We'll outgrow this shape too. When it starts to bend, we'll measure it and go again.

We're always looking for engineers who like this kind of problem. Come join us.

Robert Wells leads platform engineering at Unify.