Senior LevelExpress JS
How do you calculate database connection pool sizes in multi-instance Express clusters?
❓ Interview Prompt: Explain PostgreSQL/MySQL connection pool sizing formula, preventing DB CPU context-switching thrashing, and sizing across Kubernetes pods.
Adding more database connections does NOT improve throughput. Too many open connections cause PostgreSQL CPU context-switching thrashing!
The Connection Pool Formula:
$$\text{Total DB Pool Size} = (\text{CPU Cores} \times 2) + \text{Effective Spindle Count}$$
•
On a 4-core database server, the optimal pool size across ALL Express pods is ~10-15 total connections.
•
If running 10 Express Kubernetes pods, set each pod's ORM connection limit to 1 or 2 connections ('connection_limit=2')! Use PgBouncer if higher pod counts are required.
Code Implementation Example
javascript
// Prisma / PostgreSQL connection limit configuration for multi-pod Kubernetes clusters
// datasource db { provider = "postgresql", url = "postgresql://user:pass@db:5432/app?connection_limit=2" }💡Restricting ORM connection limits per Express pod to prevent database CPU thrashing.
🎯 Interview Key Takeaway
Size connection pools based on DB CPU cores. Restrict connection_limit per Express pod to prevent database context-switching thrashing.
#Connection Pooling#Database#Scaling#Senior
Back to all Express JS Questions→