Implementing Connection Pooling
1. Understanding Connection Pooling Benefits
| Benefit | Detail |
|---|---|
| Reuse | Avoid handshake cost |
| Throttle | Cap concurrency |
| Health | Centralized validation |
| Multiplexing | App channels share socket |
Note: In browsers, pooling typically means one shared socket per origin (SharedWorker / BroadcastChannel). In Node, a pool of N outbound WS connections to a backend service.
2. Creating Connection Pool
Example: Simple Node pool
class WsPool {
constructor(url, size = 4) {
this.size = size; this.url = url; this.idle = []; this.busy = new Set();
for (let i = 0; i < size; i++) this.idle.push(this.#create());
}
#create() { return new WebSocket(this.url); }
acquire() {
const c = this.idle.shift() ?? this.#create();
this.busy.add(c); return c;
}
release(c) { this.busy.delete(c); this.idle.push(c); }
}
3. Acquiring Connections
| Strategy | Detail |
|---|---|
| FIFO | Round-robin fairness |
| LIFO | Recently active = warm |
| Least-busy | Lowest bufferedAmount |
| Wait queue | If all busy, queue with timeout |
4. Releasing Connections
| Action | Detail |
|---|---|
| Return to idle | If healthy |
| Discard | If errored / closed |
| Replace | Spawn new one in background |
5. Setting Pool Size Limits
| Side | Concern |
|---|---|
| Browser per-origin | ~6 HTTP/1.1; WS not strictly limited but practical cap |
| Node out-bound | Tune to backend capacity |
| Server in-bound | OS fd limit (ulimit -n) |
6. Implementing Connection Validation
| Check | Detail |
|---|---|
| readyState | Must be OPEN before reuse |
| Heartbeat | Recent pong < threshold |
| Idle age | Discard if > max idle time |
7. Handling Pool Exhaustion
| Strategy | Detail |
|---|---|
| Block + timeout | Wait up to N ms then reject |
| Reject immediately | Fail fast |
| Auto-grow | Up to hard cap |
| Shed load | Drop low-priority |
8. Implementing Connection Lifecycle
Pooled Connection Lifecycle
- Create (connect, await open)
- Validate (handshake + auth)
- Idle (in pool)
- Acquired (in use)
- Release (return / discard)
- Retire (max age / errors)
- Close
9. Monitoring Pool Metrics
| Metric | Use |
|---|---|
| size | Current pool size |
| idle / busy | Utilization |
| wait_queue | Saturation indicator |
| acquire_latency | Pool pressure |
| errors | Connection health |
10. Testing Pool Behavior
| Test | Scenario |
|---|---|
| Burst | N+1 acquires |
| Backend down | All connections fail |
| Slow drain | Hold connections, observe queue |
| Leak | Never release; assert detection |