{
  "id": 22455417,
  "title": "Implementing a Fixed‑Window Postgres Rate Limiter for a Public API",
  "url": "https://urgent.news/2026/11/28/implementing-a-fixed-window-postgres-rate-limiter-for-a-public-api",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-28T21:57:29.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/zaerohell/implementing-a-fixed-window-postgres-rate-limiter-for-a-public-api-22mk"
  },
  "original_language": null,
  "account": "The issue arose when the public API started returning 429 Too Many Requests on every call after the introduction of a new endpoint for the AI Business Academy pilot. The logs indicated that the rate limit was being exceeded for user_id=42, endpoint=/v1/predict. The root cause was the absence of a rate-limit layer in the system. Initially, a placeholder file named src/infra/postgresRateLimiter.ts was created, which simply returned true without persisting counters. This approach had limitations in production, such as lack of persistence, no audit capabilities, and resetting counters on process restarts.\n\nTo resolve the issue, a fixed-window rate limiter was implemented using PostgreSQL. The solution involved adding a schema table to track request counts per fixed window. The migration file, located at migrations/20261126_create_rate_limit_table.sql, created a table named api_rate_limits with columns for key, window_start (TIMESTAMPTZ), and count (INTEGER), with a composite primary key of key and window_start.\n\nThe implementation code was placed in src/infra/postgresRateLimiter.ts. It utilized the pg library to connect to the PostgreSQL database. The function canProceed(key: string, limit: number, windowSeconds: number = 60) returned a boolean value indicating whether the request was allowed or not. It calculated the start of the current fixed window based on the windowSeconds parameter (defaulting to 60 seconds) and used an atomic upsert statement with INSERT … ON CONFLICT … DO UPDATE to increment the count. If the count exceeded the limit, the transaction was rolled back, and false was returned to indicate that the request was not allowed. Otherwise, the transaction was committed, and true was returned, allowing the request to proceed. This implementation ensured that the rate limiter persisted counters in the database, provided audit capabilities, and persisted across process restarts.",
  "summary": "Implementing a Fixed‑Window Postgres Rate Limiter for a Public API TL;DR: I added a deterministic fixed‑window rate limiter backed by PostgreSQL to stop the API from spitting out 429 on every request. The change lives in src/infra/postgresRateLimiter.ts and required a tiny schema tweak plus a few atomic SQL statements. The Problem Our public API (Node + TypeScript) started returning 429 Too Many…",
  "summary_source": "publisher",
  "key_points": [],
  "editors_take": null,
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}