Pagination, Versioning and Rate Limits

Handle large result sets, change APIs without breaking clients and protect them from abuse.

Intermediate · about 6 hours · difficulty 3/5

Learn first: Resource-Oriented REST APIs

Once real clients depend on your API, three practical problems appear: returning large lists, changing things without breaking anyone, and stopping one client from overwhelming the service.

Pagination

  • Offset pagination (?limit=20&offset=40) is simple but slow on deep pages and can skip or duplicate items when data changes.
  • Cursor (keyset) pagination (?limit=20&after=eyJpZCI6...) uses the last seen sort key. It is stable and fast at any depth; prefer it for feeds and large tables.
  • Always cap limit on the server.

Versioning

  • Additive changes (new optional fields, new endpoints) are safe. Removing or renaming fields, or changing meaning, breaks clients.
  • Common strategies: URL prefix (/v1/), a header, or date-based versions. URL versioning is the easiest to reason about.
  • Deprecate with warning headers and a timeline, and measure who still calls the old version.

Rate limiting

Limit requests per client (API key, user or IP) with algorithms like token bucket or sliding window, usually backed by Redis so all instances share counters. Return 429 Too Many Requests with a Retry-After header.

Idempotency

For POST endpoints that create payments or orders, accept an Idempotency-Key header and return the stored result on retries, so a network retry never charges twice.

You are ready to move on when your API has cursor pagination, a versioning policy and a Redis-backed rate limiter that returns 429 correctly under a load test.

Key concepts

  • Offset pagination
  • Cursor / keyset pagination
  • Breaking vs non-breaking changes
  • Versioning strategies
  • Token bucket and sliding window
  • 429 and Retry-After
  • Idempotency keys
  • Filtering and sorting parameters

Mini project

Add cursor pagination, filtering and an idempotency key to a create-order endpoint, plus a per-API-key rate limiter. Test retries and throttling with a script.

Check yourself

Why does offset pagination become slow and inconsistent on a busy table with millions of rows?

Free resources