Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

API
APIs: REST & GraphQL
0/11 completed
Beginner
What is an API?HTTP methods, status codes and headersJSON
Intermediate
REST design principlesErrors and validationPagination, filtering and sortingGraphQL basics
Advanced
Versioning and cachingRate limiting and idempotencyOpenAPI documentation
Pro
Webhooks, real-time APIs and API security
Tracks / APIs: REST & GraphQL / Advanced

Rate limiting and idempotency

A public API will meet buggy scripts in infinite loops, aggressive scrapers, password-guessing bots and enthusiastic customers who click "Pay" three times. Rate limiting protects your servers from too many requests; idempotency makes it safe for clients to retry.

Rate limiting

A rate limit caps how many requests a client may make in a period: say, 100 per minute per API key. Beyond that the API answers 429 Too Many Requests, ideally telling the client when to come back.

The token bucket

The most widely used algorithm is the token bucket. Each client has a bucket holding up to capacity tokens; tokens refill at a steady rate; every request takes one token; with an empty bucket, the request is refused. It allows short bursts while enforcing an average rate:

Node.js · editable

Other algorithms you'll meet: fixed window (count per calendar minute: simple, but allows double bursts at window edges), sliding window (smooths that out) and leaky bucket (processes requests at a constant rate).

Rate limiting in Express

The express-rate-limit package does this for you and sends the standard headers:

Node.js · editable

RateLimit: "60-in-1min"; r=0; t=58 means: policy "60-in-1min", remaining 0, resets in t seconds. Well-behaved clients read these headers and back off.

Production notes

  • With several server instances, keep the counters in a shared store (Redis), or each instance enforces its own separate limit.
  • Choose who a limit applies to: per API key or user ID for authenticated calls, per IP for anonymous ones (but many users can share one IP behind an office or mobile network).
  • Use stricter limits on expensive or sensitive endpoints: login, password reset, search, sending SMS.
  • Rate limits are often enforced at the edge as well: API gateways, load balancers and CDNs (Cloudflare, AWS API Gateway, nginx limit_req).

Retrying safely

Networks fail. A client sends POST /payments, the connection drops, and it never sees the response. Did the payment happen? If the client retries and it did, the customer pays twice.

GET, PUT and DELETE are idempotent, so retrying them is safe. POST is not. The fix is an idempotency key: the client generates a unique ID for each logical operation and sends it with every attempt. The server remembers the response for each key and replays it for repeats:

Node.js · editable

Stripe, Adyen and most payment APIs work exactly like this. A production version must also handle two attempts arriving at the same moment (lock the key while the first is processing) and expire old keys after a day or so.

Retrying on the client side

Retry only what's safe (idempotent methods, or requests with an idempotency key), and only on errors that may be temporary: network failures, 429, 502, 503, 504. Wait longer each time with exponential backoff plus random jitter, so thousands of clients don't retry at the same instant:

Node.js · editable

Respect Retry-After when the server sends it.

Practice

  1. Change the token bucket to allow bursts of 10 and 2 requests per second, and print when request 13 would be allowed.
  2. Add a stricter rateLimit (5 per 15 minutes) to a POST /login route only.
  3. Make withRetries stop immediately on a 400 error by marking only some errors as retryable.
✎ Check your understanding

1.Which status code means "rate limit exceeded"?

2.What does a token bucket allow that a strict fixed rate doesn't?

3.Why does a payment API need idempotency keys?

← Versioning and caching