Skip to the page

Testing a feature and estimating work · mid

Sizing pools, partitions, servers and backfills

Capacity numbers come from measured rates and simple laws, not guesses. Size the pools, partitions and pods from throughput, then check the limits of everything behind them.

The notes

What you must know

  • Pool sizing with Little law: connections in use = arrival rate x hold time. 200 requests a second x 5 ms is 1 on average; at a p99 hold of 20 ms it is 4. Pool per pod about 5 to 8, with a short wait timeout.
  • Total connections: 30 pods x 8 = 240, under a limit of 300 with room for admin tools and other services. 90 pods x 8 = 720 is over max_connections; each Postgres connection is a process with its own memory. Options: smaller pools, PgBouncer in transaction mode, read replicas. Database side: 6,000 requests a second x 5 ms is about 30 busy connections. A slow query changes everything: at 200 ms each pod needs 40 connections. Fix the query, not the pool.
  • Kafka partitions: partitions >= max(t / p, t / c), where t is target throughput and p and c are the measured produce and consume throughput of one partition. 50,000 / 2,000 = 25 consumers means at least 25 partitions; pick about 48 to 60 for headroom.
  • Not 1,000 partitions: each costs open files, client memory and slower leader failover. Adding partitions later changes the key hash mapping and breaks per-key order during the change, so plan ahead. Compression (lz4 or zstd) shrinks JSON, so measure it.
  • Servers for a sale: measure per pod (for example one pod holds 150 a second at about 60% CPU with p99 in the SLO). 20,000 / 150 is about 134 pods; with 25 to 30% headroom about 170 to 180. With 3 zones, plan for one zone lost: about 1.5x.
  • Check everything behind the pods: database connections, CPU and replicas, Redis, Kafka, node and cloud quotas, load balancer limits, and partner rate limits agreed in advance.
  • Backfill time: batches by key range of about 5,000 rows taking 0.25 to 0.5 s give 5,000 to 10,000 rows a second. 500 million rows at 10,000 a second is about 14 hours (28 hours at 5,000). Run in quiet hours, for example 1 am to 7 am IST, so 3 to 5 nights.
  • A backfill writes a lot of WAL and raises replica lag and disk use; dead row versions can nearly double the table until vacuum catches up. Pause on lag, use short transactions and a low lock_timeout, store the last id done, and make the update idempotent (WHERE new_col IS NULL).

The red pen

Common traps: mistake → fix

  • Mistake: A bigger pool is always faster

    Fix: the database runs about as many queries as it has cores; more only waits.

  • Mistake: More partitions are free

    Fix: they cost resources and can break ordering when added later.

  • Mistake: Pods needed = peak divided by the pod rate

    Fix: add headroom, zone loss, and the limits of every other part.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you size a database connection pool?
  2. How many Kafka partitions do you need for 50,000 messages a second?
  3. How many servers do you need for a festive sale?
  4. A backfill of 500 million rows: how long will it take and what can go wrong?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview