The Real Fullstack

The Exhausted Connection Pool

PostgreSQL CPU sits at 22%. Why are requests timing out waiting for a database connection?

高级工程师10 分钟性能实验室流程模拟器
javaspring-boothikaricppostgresqlkubernetes

此挑战尚未翻译为简体中文,你正在阅读英文版本。

任务:Serve 500 req/s with ≤ 0.5% errors and the pool at most half utilised, without exceeding the database's 100 connections.背景 · 学习目标 ▾

Brightcart's order service runs 6 pods with a HikariCP pool of 10 each, against a PostgreSQL server allowing 100 connections. At 500 orders per second, requests fail with “Connection is not available”. The team's first idea: bigger pools, more pods.

任务: Serve 500 req/s with ≤ 0.5% errors and the pool at most half utilised, without exceeding the database's 100 connections.

学习目标

  • Read pool metrics and pg_stat_activity to find hold time.
  • Keep transactions short: no remote calls while holding a connection.
  • Understand how replicas × pool size meets max_connections.
模拟
模拟数据
Load balancerOrder service podsConnection poolsPostgreSQLFraud check API
运行模拟
@Transactional                               // connection borrowed here …public Order place(OrderRequest r) {  Order o = orders.save(Order.pending(r));  FraudVerdict v = fraudClient.check(r);     // … held during a 120 ms HTTP call  o.apply(v);  return o;                                  // … and returned only at commit}

简化说明

Steady-state queueing. Connection hold time is fixed per request; the database is modelled as 16 cores. 教学模型——数字用于说明行为,并非基准测试。

概念

建议先完成

相关挑战

参考资料