Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

You have 80 services, each with 20 instances and a pool of 10 connections, connecting to one PostgreSQL cluster with max_connections set to 500. What problems do you foresee and what architecture would you propose?

80 services times 20 instances times 10 connections is 16,000 potential connections against a limit of 500, so deploys and scale-outs will hit too many clients errors and connection storms, and even within the limit each PostgreSQL connection is a separate process with its own memory, so very high counts hurt performance. I would introduce a server-side pooler such as PgBouncer in transaction mode, accepting its limits around session state and prepared statements, split databases by service ownership so not everything shares one cluster, and set each service's pool size from measured concurrency with an enforced connection budget.

Why this question is asked

Connection counts are a classic problem that grows quietly: each team picks a reasonable pool size, each service scales out a little, and one day a routine deploy makes the database refuse connections. Interviewers ask this design question with concrete numbers to see whether we do the arithmetic, know why PostgreSQL in particular suffers from many connections, and can propose an architecture rather than a single setting. In this answer, we will do the arithmetic, see why PostgreSQL connections are expensive, introduce PgBouncer and its limits, split the databases by ownership, and set pool budgets from measurements.

1Doing the Arithmetic

Each service instance has a HikariCP pool. With HikariCP's recommended fixed-size pool, each instance opens all 10 connections soon after startup and keeps them. 80 services with 20 instances and 10 connections each make 80 x 20 x 10 = 16,000 connections. The cluster allows 500, of which a few are reserved for superusers. So the system cannot even start fully: the first few dozen instances take all the slots and the rest fail to connect with FATAL: sorry, too many clients already.

Deploys make it worse. A rolling deploy starts new instances before stopping old ones, so for a while both hold connections. An autoscaler adding instances under load, exactly when the database is busiest, opens new connections at the worst moment. And when a pool cannot connect, it retries, so a crowd of instances can hammer the database with connection attempts, a connection storm, which costs the database CPU for every attempt.

Where the connections come from
ItemValueResult
Services80
Instances per service201,600 instances
Pool size per instance1016,000 connections
PostgreSQL max_connections50032 times oversubscribed
Rolling deploy of one service+20 instances briefly+200 connections
The pools ask for 32 times what the database allows.

2Why Many PostgreSQL Connections Hurt

Raising max_connections to 16,000 is not the answer. PostgreSQL uses a process per connection: each connection is served by its own backend operating-system process, with its own memory for caches, sorting and query state, often several megabytes and more when queries use work_mem. Thousands of mostly idle processes consume memory that would otherwise cache data, and they make some internal operations, such as taking snapshots of running transactions, more expensive as the count grows, although recent versions improved this.

More importantly, the database cannot do more work at once than its cores and disks allow. A 32-core server might run a few dozen queries truly in parallel; 16,000 connections do not make it faster, they make it queue internally and context-switch. So the goal is not to allow more connections but to have a small number of database connections that are kept busy, while thousands of application instances share them.

3A Server-Side Pooler: PgBouncer in Transaction Mode

PgBouncer is a lightweight connection pooler that sits between the applications and PostgreSQL. Applications connect to PgBouncer, which can accept many thousands of client connections cheaply, and PgBouncer keeps a small pool of real server connections. In transaction mode, it assigns a server connection to a client only for the duration of a transaction and returns it to its pool at commit or rollback. So 16,000 mostly idle client connections can share, for example, 300 server connections, because at any moment only a few hundred transactions are actually running.

Step throughConnections with a server-side pooler
  1. 1,600 service instanceseach with a small HikariCP pool, e.g. 4 connections
  2. PgBouncer (transaction mode)accepts ~6,400 client connections, run with redundancy
  3. Server connection poolabout 300 real connections, per database and user
  4. PostgreSQL clustermax_connections 500, headroom for admin and replication

4 layers, from the top down. Step through them one at a time.

Client connections are cheap at the pooler; only active transactions use real server connections.

Transaction mode has limits because consecutive transactions from one client can run on different server connections. Session-level state does not survive between transactions: SET commands outside a transaction, session advisory locks, LISTEN/NOTIFY, temporary tables kept across transactions, and WITH HOLD cursors. Prepared statements were a long-standing problem, because a named statement prepared on one server connection does not exist on another; PgBouncer 1.21 and later can track protocol-level prepared statements with max_prepared_statements, while with older versions teams set pgjdbc's prepareThreshold=0. Check your versions. Services that need session features can use a separate PgBouncer pool in session mode or connect directly.

4Split Databases by Service Ownership

The pooler fixes the connection count, but the deeper issue is that 80 services share one cluster. That creates a single point of failure, a single noisy-neighbour problem where one service's bad query slows everyone, and a schema that every team can couple to. A better long-term architecture gives each service, or each group of closely related services owned by one team, its own database, and groups several small databases onto a few clusters sized for them.

Splitting also divides the connection problem. If the 80 services are spread across eight clusters, each cluster serves about ten services' pools, which is manageable even before a pooler, and a problem in one cluster affects only its services. The cost is migration work, including untangling cross-service joins into APIs or events, so this is a plan over quarters, starting with the busiest or most problematic services.

5Set Pool Sizes From Measured Concurrency and Enforce Budgets

Finally, we replace the default of 10 with sizes based on evidence. HikariCP's metrics show each service's actual concurrent use; by Little's law, an instance handling 50 requests per second that each hold a connection for 20 ms needs about one connection on average, so a pool of 3 or 4 leaves room for bursts. Many services will need far fewer than 10, and a few busy ones might need more.

Then we make it a budget. Each database or PgBouncer pool has a known capacity, and each service gets an allocation in connections, recorded in configuration and reviewed when it scales. A check in the deployment pipeline can compare instances times pool size against the allocation, and alerting on the database's connection count and PgBouncer's waiting clients shows when a budget is too tight. Setting minimumIdle lower than the maximum lets instances release idle connections at quiet times, which matters when there are 1,600 of them. Autoscaling policies then take the connection budget into account, rather than discovering it during an incident.

6Follow-up questions

Could we just lower every pool to 2 connections?

That brings 1,600 instances to 3,200 connections, still over 500, and starves busy services while wasting nothing on idle ones. Pool sizes must differ by service according to measured demand, and even then, at this scale a server-side pooler or a split into several clusters is needed to stay within the limit.

Where should PgBouncer run?

Commonly as a small, redundant tier in front of each cluster, behind a load balancer or service name, so it scales independently and survives the loss of one node. Some teams run it as a sidecar next to each application instead, which avoids an extra network hop but does not reduce server connections across instances in the same way. Managed cloud databases often offer a built-in proxy that plays the same role.

Does HikariCP still make sense behind PgBouncer?

Yes, but with a different purpose. The application pool keeps a few cheap connections to PgBouncer open to avoid reconnecting on each request, while PgBouncer does the real sharing of server connections. Pool sizes can stay small, maxLifetime should be shorter than PgBouncer's own idle limits, and the driver settings must respect transaction mode, such as prepared statement handling.

7Conclusion

In this answer, we saw that 80 services with 20 instances and pools of 10 ask for 16,000 connections from a cluster that allows 500, which causes failures at startup, deploys and scale-outs, and that PostgreSQL's process-per-connection model makes very high counts costly even when allowed. We proposed PgBouncer in transaction mode to share a few hundred server connections, with care around session state and prepared statements, splitting databases by service ownership, and pool sizes set from measured concurrency within enforced budgets.

What to say in the interviewAbout 100 seconds aloud

Open with

First the arithmetic: 80 services times 20 instances times 10 connections is 16,000 connections, against a max_connections of 500. So we will see too many clients errors as soon as most instances are up, and every rolling deploy or autoscale adds hundreds more, with pools retrying and causing connection storms.

Then, if they want more

Raising max_connections is not a real fix, because PostgreSQL runs a separate process per connection, each with its own memory, and the server can only run a few dozen queries in parallel anyway; thousands of connections just waste memory and add contention. My architecture would have three parts.

First, a server-side pooler, PgBouncer in transaction mode, run redundantly in front of the cluster. Applications can hold thousands of cheap client connections to it, and it lends a few hundred real server connections only for the length of each transaction.

I would be clear about its limits: session state like SET outside transactions, advisory locks, LISTEN and temp tables do not carry across transactions, and prepared statements need PgBouncer 1.21 or later with max_prepared_statements, or prepareThreshold=0 on the driver.

Second, over time, split the databases by service ownership, so each team's services use their own database and clusters are smaller and isolated from noisy neighbours.

Third, stop using ten everywhere: size each service's pool from its measured concurrency using Little's law, often three or four, give each service a connection budget, and check instances times pool size against it in the pipeline and in autoscaling.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
java.sql, package documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You have 80 services, each with 20 instances and a pool of 10 connections, connecting to...": https://scrivio-interview-library.netlify.app/databases/jdbc/9360395e83/