Rate limits and safe errors
Local fixture: Node.js 22.16.0, ECMAScript modules, synthetic data only. Run node main.mjs in a disposable directory. This is not a production security implementation.
Test a bounded fixed-window model
let windowStart = 0, used = 0;function take(now) { if (now >= windowStart + 1000) { windowStart = Math.floor(now/1000)*1000; used=0; } if (used >= 2) return 429; used++; return 200;}// Monotonic synthetic timestamps for one trusted key.console.log([0,10,999,1000,1001,1002].map(take).join(' '));Expected output:
200 200 429 200 200 429Two requests fit each interval. At 1000 the new window begins; no distributed guarantee is inferred.
How it works
A fixed-window counter allows a specified number of operations in a time interval. The next interval resets the counter; requests near a boundary may form a burst. Production multi-instance limits need coordinated atomic accounting, bounded key cardinality and a documented policy when storage is unavailable. Use authenticated identity where possible and trust forwarded client addresses only from explicitly configured proxies. HTTP 429 indicates too many requests; Retry-After can describe when retry is appropriate. Distinguish public error codes from private diagnostic events. Log controlled metadata with a request correlation ID, not arbitrary request bodies, secrets or stack traces in public output. The following single-key clock model and error mapping are not distributed middleware.
Map internal errors to public contracts
import assert from 'node:assert/strict';function publicError(kind, requestId) { const known = new Map([['invalid',{status:400,code:'INVALID_INPUT'}], ['limited',{status:429,code:'RATE_LIMITED'}]]); return {...(known.get(kind) ?? {status:500,code:'INTERNAL_ERROR'}), requestId};}const response = publicError('synthetic internal details','req-1');assert.deepEqual(response,{status:500,code:'INTERNAL_ERROR',requestId:'req-1'});assert.equal(publicError('invalid','req-2').status,400);console.log(JSON.stringify(response));Expected output:
{"status":500,"code":"INTERNAL_ERROR","requestId":"req-1"}Unknown failures become a generic public error; the synthetic internal string is not returned.
Common mistakes
A counter per unbounded arbitrary user-supplied key can consume memory. Avoid exposing raw exception messages. Do not disable authorization or limits during a dependency outage without a reviewed failure policy.
Practice and next steps
Change the fixed window allowance to three and test the fourth request, exactly at the reset boundary and one request after reset. Discuss whether burst behavior fits the service.
Further reading: Primary documentation. Related staged reference: Rate limit and error contract.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Rate limits and safe errors — knowledge check
Apply the security boundary and distinguish the example from a production control.
What is a limitation of fixed windows?
Which status represents a rate limit denial?
What should an unexpected public error include?