proomt

Search

Search posts, papers, and topics

All posts

SitePoint18 min readtutorialintermediate

Building Distributed Sliding-Window Rate Limiters in TypeScript and Redis

Summary

This guide details building a distributed sliding-window rate limiter using TypeScript and Redis. It leverages Redis sorted sets and an atomic Lua script to prevent race conditions and over-admission under high concurrency, supporting dual RPM/TPM quotas.

  • Atomic operations via Lua scripts are critical for distributed rate limiting to avoid time-of-check-to-time-of-use (TOCTOU) race conditions.
  • The sliding-window log uses Redis sorted sets, storing request timestamps as scores and encoding costs within member strings.
  • A single Lua script atomically prunes expired entries, sums current costs, conditionally admits new requests, and sets key expiry.
  • Use `EVALSHA` for script execution performance, with a fallback to `EVAL` if the script isn't cached; `MULTI/EXEC` is insufficient for read-then-write logic.

Engineers building robust distributed services need to understand how to implement correct rate limiting without race conditions, which this article addresses with a concrete, production-ready solution.

7/10

Related reading

  1. Build a Typed Context Compaction Gate for AI Agents

    A step‑by‑step tutorial showing how to build a typed context‑compaction gate for AI agents in TypeScript using LangChain and Zod. It defines a discriminated‑union schema for keep/summarize/discard actions, implements a fast gpt‑4o‑mini classifier, validates decisions at runtime, and wires the gate as middleware in an agent loop, with testing and production‑grade tuning advice.

    SitePointsitepoint.com16 min
  2. Building Deterministic Multi-Agent State Machines in TypeScript

    The article shows how to build a deterministic, checkpoint‑backed finite state machine engine in TypeScript for orchestrating multi‑agent AI workflows. It uses Zod for schema validation, better‑sqlite3 for atomic persistence, and a pure transition function to make workflows traceable and recoverable in serverless environments.

    SitePointsitepoint.com18 min
  3. Worker Backpressure (Part 1)

    Canva added a lightweight, local backpressure loop to its queue worker library that monitors per‑message success/failure, computes a backoff factor against a configurable failure‑rate set‑point, and throttles the worker’s concurrency. In two real incidents the mechanism kept failure rates under 2 % fleet‑wide, limited DLQ growth to a handful of messages, and maintained throughput without manual i…

    Canvacanva.dev10 min
  4. Migrating the GitHub Copilot runtime to Rust, using Copilot

    The Copilot agent runtime was rewritten from a 130‑k‑line TypeScript/Node.js codebase into a native Rust library (~830 k lines of Rust) to reduce startup latency, memory use, and improve reliability. The migration was done incrementally (in‑place) across 128 PRs, with AI‑generated code handling most of the work. The new runtime exposes a C ABI for in‑process embedding by all six Copilot SDK langu…

    GitHub Oldgithub.blog65 minHN188