# Pagination, Versioning and Rate Limits

> Handle large result sets, change APIs without breaking clients and protect them from abuse. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/api-evolution-and-limits
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/backend/rest-api-design)

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

- [MDN: 429 Too Many Requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (MDN)
- [Stripe API reference (pagination and idempotency examples)](https://docs.stripe.com/api) (Stripe)

Previous: [Resource-Oriented REST APIs](https://www.internhack.in/roadmaps/tracks/backend/rest-api-design) · Next: [API Contracts with OpenAPI](https://www.internhack.in/roadmaps/tracks/backend/openapi-docs)

## More in Designing and Documenting APIs

- [Resource-Oriented REST APIs](https://www.internhack.in/roadmaps/tracks/backend/rest-api-design)
- [Pagination, Versioning and Rate Limits](https://www.internhack.in/roadmaps/tracks/backend/api-evolution-and-limits)
- [API Contracts with OpenAPI](https://www.internhack.in/roadmaps/tracks/backend/openapi-docs)
- [GraphQL APIs](https://www.internhack.in/roadmaps/tracks/backend/graphql)
- [gRPC and Protocol Buffers](https://www.internhack.in/roadmaps/tracks/backend/grpc)
- [SOAP and Legacy Integrations](https://www.internhack.in/roadmaps/tracks/backend/soap-legacy-apis)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
