Implementing Rate Limiting

1. Understanding Rate Limiting Need

ThreatMitigation
DoS (msg flood)Per-connection msg/sec cap
Connection stormPer-IP connect/sec cap
SlowlorisHandshake timeout
Bandwidth abuseBytes/sec cap
Auth brute forceLogin attempts/min

2. Implementing Connection Rate Limiting

Example: Per-IP connect counter

const conn = new Map();
function allow(ip, max = 5, windowMs = 60000) {
  const now = Date.now();
  const arr = (conn.get(ip) ?? []).filter(t => now - t < windowMs);
  arr.push(now);
  conn.set(ip, arr);
  return arr.length <= max;
}

3. Implementing Message Rate Limiting

WindowDetail
FixedReset every N sec
SlidingLast N sec rolling
Token bucketBurst + steady rate
Leaky bucketSmooth output rate

4. Using Token Bucket Algorithm

Example: Token bucket

class TokenBucket {
  constructor(capacity, refillPerSec) {
    this.capacity = capacity; this.tokens = capacity;
    this.rate = refillPerSec; this.last = Date.now();
  }
  take(n = 1) {
    const now = Date.now();
    this.tokens = Math.min(this.capacity, this.tokens + (now - this.last) / 1000 * this.rate);
    this.last = now;
    if (this.tokens < n) return false;
    this.tokens -= n; return true;
  }
}
ParamEffect
capacityMax burst size
refillSustained rate

5. Using Leaky Bucket Algorithm

AspectDetail
BehaviorConstant drain rate
BurstLimited to bucket size
Best forSmoothing output to downstream
vs Token bucketNo burst above drain rate

6. Implementing Sliding Window

Example: Sliding count

const hits = [];
function allow(limit = 100, windowMs = 60000) {
  const now = Date.now();
  while (hits.length && now - hits[0] > windowMs) hits.shift();
  if (hits.length >= limit) return false;
  hits.push(now); return true;
}

7. Storing Rate Limit State

StoreUse
In-memory MapSingle node
Redis (INCR + TTL)Cluster-wide
Redis sliding-logAccurate sliding window
Sharded countersHot-key avoidance

8. Handling Rate Limit Exceeded

Example: Reject with hint

ws.send(JSON.stringify({ "type":"err","code":429,"retryAfter":1000 }));
// or close on repeated abuse
ws.close(1008, "rate-limit");
StrategyWhen
Soft dropBest-effort traffic
Send 429API-style request
Close 1008Sustained abuse
Block IPRepeat offenders

9. Implementing Per-User Limits

TierLimit Example
Anonymous10 msg/min
Free user60 msg/min
Paid600 msg/min
InternalUnlimited (still bounded)

10. Monitoring Rate Limit Metrics

MetricPurpose
ws_rate_limit_hits_totalCounter by reason
ws_rate_limit_close_totalForced closes
Top offendersIP / userId
AlertSudden spikes