Implementing Connection Pooling

1. Understanding Connection Pooling Benefits

BenefitDetail
ReuseAvoid handshake cost
ThrottleCap concurrency
HealthCentralized validation
MultiplexingApp 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

StrategyDetail
FIFORound-robin fairness
LIFORecently active = warm
Least-busyLowest bufferedAmount
Wait queueIf all busy, queue with timeout

4. Releasing Connections

ActionDetail
Return to idleIf healthy
DiscardIf errored / closed
ReplaceSpawn new one in background

5. Setting Pool Size Limits

SideConcern
Browser per-origin~6 HTTP/1.1; WS not strictly limited but practical cap
Node out-boundTune to backend capacity
Server in-boundOS fd limit (ulimit -n)

6. Implementing Connection Validation

CheckDetail
readyStateMust be OPEN before reuse
HeartbeatRecent pong < threshold
Idle ageDiscard if > max idle time

7. Handling Pool Exhaustion

StrategyDetail
Block + timeoutWait up to N ms then reject
Reject immediatelyFail fast
Auto-growUp to hard cap
Shed loadDrop low-priority

8. Implementing Connection Lifecycle

Pooled Connection Lifecycle

  1. Create (connect, await open)
  2. Validate (handshake + auth)
  3. Idle (in pool)
  4. Acquired (in use)
  5. Release (return / discard)
  6. Retire (max age / errors)
  7. Close

9. Monitoring Pool Metrics

MetricUse
sizeCurrent pool size
idle / busyUtilization
wait_queueSaturation indicator
acquire_latencyPool pressure
errorsConnection health

10. Testing Pool Behavior

TestScenario
BurstN+1 acquires
Backend downAll connections fail
Slow drainHold connections, observe queue
LeakNever release; assert detection