StoicBuilder

HandbookXIII the AqueductScaling out

XIIIthe Aqueduct, Scale it

Scaling out

1 min readUpdated 28 November

In plain words

Scaling out means adding more identical instances of a service behind a load balancer, which only works when no instance keeps anything in its own memory or disk that the others need.

Where you see it

Harbor's autoscaler grows the API from 4 containers at night to 20 on a Monday morning. It worked well until someone added an in-memory rate limiter, and every new container handed each user a fresh allowance.

k8s/api-hpa.yamlconfig
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: { name: api }spec: scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: api } minReplicas: 4 maxReplicas: 20 metrics: [{ type: Resource, resource: { name: cpu, target: { type: Utilization, averageUtilization: 60 } } }]
Between 4 and 20 copies, aiming for 60 percent average CPU. The maximum also protects the database from a crowd of new connections.

What goes wrong

  1. State kept on one instance

    Sessions in memory, uploads on local disk and in-process rate limits break as soon as there are two copies. Move them to shared places: Redis, object storage, the database.

  2. Scaling the app into the database

    Twenty instances with a pool of 20 connections each want 400 connections from a database that allows 100. Count connections before raising the maximum, and put a pooler in between.

  3. Scaling on the wrong signal

    Workers waiting on slow API calls use little CPU, so a CPU-based autoscaler never adds more while the queue grows. Scale workers on queue depth or job age instead.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Check this service for anything that breaks with more than one instance: in-memory sessions, caches, rate limits, scheduled jobs and files on local disk. Move each to a shared store, make sure cron jobs run once rather than once per instance, and tell me how many database connections we'd open at the autoscaler's maximum.

Quick check

Sign in to earn +10 XP for it

Harbor scales from 4 to 12 API instances during a traffic spike, and users start being signed out at random. What's the likely cause?

Got a prompt that touches scaling out?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.