LAB REPOSITORYSTARTER CODE

Sliding Window Rate Limiter

An in-memory limiter that stores recent request timestamps per key and computes allow, remaining, and retry-after decisions.

TypeScriptDifficulty: starterArchive: Design an API Rate Limiter
$Read src/demo.ts and run it with any TypeScript runner.
Key Takeaways & Architecture Lessons
  • 01.The key choice decides what the limiter actually protects.
  • 02.Old hits must be evicted before checking the current request.
  • 03.Shared production limiters usually move this state into Redis or another atomic store.
# Sliding Window Rate Limiter

This limiter keeps recent request timestamps per key and rejects calls once a key has too many hits inside the active window.

Production systems usually store this state in Redis or another shared store. The in-memory version makes the algorithm easy to see first.

Read in order:

1. `src/limiter.ts`
2. `src/demo.ts`

The demo prints numbered steps showing accepted requests, blocked requests, retry timing, and capacity returning as the window moves.