Five copies, sixty connections
We shipped two things this week. Your services can scale out to five copies, and each postgres size now has a fixed connection limit of 60, 105 or 119. Put the two together without checking the arithmetic and you get "too many clients", at the worst possible time: the API is busy, and you're deploying a fix.
This week a service on Gagarin can run up to five copies, and a postgres sizes its memory and connections by the size you pick. Each change is useful by itself. Used together, they give you a sum that you should work out before production does.
The setup
Take a Node API on node-postgres with its default pool of 10, deployed with
--replicas 1-5, talking to a postgres at size S. Each copy opens its own
pool, so the database sees ten connections from each copy.
On a quiet day that is one copy and ten connections against a limit of 60. Then lunch comes, CPU climbs past 70%, and the autoscaler takes the API to five copies, which is 50 connections. That still fits.
Then you notice a bug and deploy the fix. A rollout starts one new copy before it stops an old one, so for a moment the API runs six copies with sixty connections between them. The migration job you ran first may still be holding its connection, and the nightly backup may be running. Postgres answers the 61st connection the way it always has:
FATAL: sorry, too many clients alreadyIf your app connects at startup, the new copy crashes and the rollout stalls. If it connects lazily, it's worse: the readiness probe only checks that the port is open, so the new copy goes into rotation and returns 500s for every request that needs the database. Either way it happens under load, while you are watching the deploy.
Your app can use the spare slots too
Postgres keeps three slots for superusers, so an administrator can still log
in when the application has taken everything else. On Gagarin your app's role
is a superuser in its own database. So the reserve doesn't protect you: your
pools can fill all 60 slots. When they do, your own psql, a migration job
and the backup can't get in either.
The arithmetic
Count the most connections your app can hold at once, and leave a few spare:
(max copies + 1) × pool size ≤ max_connections − headroomThe + 1 is the extra copy a rollout starts. Leave headroom of about five,
for a job, the backup and you. With a range of 1-5, here is the largest pool
that fits each size:
| postgres | max_connections | largest pool per copy, 1-5 |
|---|---|---|
| S | 60 | 9 |
| M | 105 | 16 |
| L | 119 | 19 |
Note how little L adds over M: fourteen connections. That was the trade-off in the postgres post. An L buys sort memory and a 1 GB buffer cache. It doesn't buy many connections.
Know your pool's default
Most drivers pick a pool size for you, and most people never look at it:
- node-postgres (
pg.Pool): 10. - SQLAlchemy: 5, plus up to 10 overflow, so 15 under load.
- Go's
database/sql: unlimited. With noSetMaxOpenConns, one busy copy can use up the whole database by itself.
Make the pool size an environment variable so you can change it at deploy time. Then pass it on every deploy, because a Gagarin deploy restates the whole environment:
gg deploy shop/api:8080 api:v5 --replicas 1-5 --env DB_POOL_MAX=8Three ways out
Shrink the pool. This is cheaper than it sounds. A pool of 8 on each of five copies is 40 connections to a database that has one core at S, and one core can't run forty queries at once anyway. Past a few connections per core, a bigger pool doesn't add throughput. It only gives queries a place to queue, and moves that queue from your app into postgres.
Move the database up a size. gg resource add shop/db postgres --size m
gives you 105 connections and a real cache, at the cost of one restart.
Put a pooler in front. PgBouncer in transaction mode, deployed as a
service of its own with one copy, lets every API copy open as many client
connections as it wants. PgBouncer then shares 20 or so real connections among
them. Watch for the usual catch: anything that depends on session state,
such as prepared statements on older drivers or SET without LOCAL, needs
attention in transaction mode.
When adding copies doesn't help
The autoscaler adds copies when your API's CPU is busy. If the API is busy because it waits on the database, which looks like CPU on JSON serialization around slow queries, then five copies mean five times the concurrent queries against a single postgres, and it gets slower.
Before you give a service a range, open the Metrics tab and look at both graphs. If the database's CPU is the one at the ceiling, resize the database, not the API. Copies scale the part of your system that holds no state, and the database still runs as a single copy.