Scalable REST API Design: Versioning, Pagination and Idempotency

Last month we witnessed a decision whose bill arrived two years late. A client's backend team removed a field from an API response that they said was "no longer used." It wasn't used — in the new app version. In the field, though, a third of users were still carrying the version from eighteen months earlier, and that version crashed on launch when the field didn't arrive. Thousands of crashes overnight, a shower of one-star reviews in the store, and a deploy rolled back in a hurry.

API design is that kind of field: the price of decisions isn't paid right away but years later. When an old version of the mobile app breaks, when pagination crawls at the millionth record, when the support line jams over a payment charged twice. As a team that has spent years maintaining APIs, both ones we wrote and ones we inherited, we've put our three most expensive lessons into this post.

Old versions live in the field for years

On the web, you deploy and everyone gets the new code. On mobile, there's no such luxury: the user does the updating — or doesn't. Phones with auto-update turned off, corporate devices that can't reach the store, users who don't update because they're "out of space"... The reality is that every API version you ship is a contract that will live in the field for years.

That's why our core rule fits in one sentence: you can add fields; you can't change, delete or alter the meaning of anything that already exists. Adding a new field is safe, because an old client ignores fields it doesn't know about. If a breaking change is unavoidable, a new version is opened — /v2/ — and the old one is retired on a plan whose dates are announced up front: first the logs are used to track who is still on v1, then come warnings, and finally the shutdown. Keeping the version in the URL may not strike everyone as elegant; header-based versioning is neater on paper. But a version in the URL is visible to the naked eye in logs, in the browser, in a curl command — and when you're debugging, that visibility is worth more than elegance.

Four rules in API versioning for not breaking old clients in the field: add fields, open a new version, monitor first, build an update gate.
Every API version you ship is a contract that will live in the field for years.

Communicating the retirement process matters as much as the technical decision itself. Announce the shutdown date to the internal and external teams using your API months in advance, through more than one channel; add a warning header to responses saying the old version is going away, so that attentive clients can see it in their logs. In our experience, stories that begin with "we shut it down, nobody will have noticed" never end well.

And then there's the mechanism nobody thinks of in the first version: a forced update gate. On launch, the app checks a lightweight endpoint with a single question — "is my version still supported?" If the answer is no, the user is shown a polite update screen. Without this gate, the only way to retire antique versions in the field is to break them; if the gate is there from the start, retirement is controlled and respectful. Two hours of work for years of flexibility.

The written form of the contract matters at least as much as its rules. We tie every API to a single source of truth with an OpenAPI schema: the documentation is generated from it, the mobile client's network layer is derived from it, and the contract test in CI checks against it too. When the schema and the actual behavior drift apart, the pipeline turns red. The best answer to "is the documentation up to date?" is documentation that has no choice but to stay up to date.

How offset pagination collapses at the millionth record

Every API starts out with pagination, and nearly all of them fall into the same trap: OFFSET. The query "OFFSET 100000 LIMIT 20" tells the database: read one hundred thousand and twenty rows, then throw away the first hundred thousand. As the table grows, every page gets linearly slower; the deeper the user goes, the more the API starts to crawl. And there's a second problem: when new records are inserted between page loads, the list shifts — the user sees the same record twice or never sees it at all.

The right tool is cursor (keyset) pagination: you tell the client "here's a pointer to the last record you saw," and the next request comes in with "id < son_gorulen ORDER BY id DESC LIMIT 20" (where son_gorulen is the last-seen ID). The database jumps straight to that point via the index; the first page and the millionth page come back at the same speed, and shifting is no longer a thing. This is the natural partner for every mobile interface with infinite scroll. In our new projects, we keep offset out of the API contract from the start; ripping it out later is ten times more work than never putting it in, because by then clients have gotten used to the concept of a "page number."

In fairness: cursors have their costs too. There's no longer a concept of "jump to page 37," and the "4,812 results total" figure doesn't come for free. But look at the reality of mobile interfaces — users navigate with their thumbs, not page numbers; if a total count is needed, it's served from a separate, cached count endpoint. In places where page numbers genuinely make sense, like the table view in an admin panel, we allow offset; we're not in favor of rigid rules but of knowing where each tool belongs.

Comparison of how OFFSET pagination slows down and shifts the list on large tables with the advantages and costs of cursor pagination.
Cursors have a cost, but on mobile, users navigate with their thumbs.

If the same payment is charged twice

The third lesson is about the whims of mobile networks. The scenario is a classic: the client sent the payment request, the server processed it, but the response got lost on the way — say the signal dropped in a tunnel. What does the client see? A timeout. What does it do? It retries. What does the server do? It charges the payment again. What does the customer do? Calls their bank, then you, then social media.

The insurance for this scenario is the idempotency pattern: the client attaches a unique Idempotency-Key to every critical POST request. The server stores that key along with the result of the operation; when a request with the same key arrives a second time, it doesn't process it again but returns an exact copy of the first response. Let the client retry as often as it likes — let the network be as fickle as it likes — the operation happens once.

Four steps showing that when a request sent with an Idempotency-Key is retried, it is not processed again and the first response is returned.
No matter how fickle the network gets, the operation happens once.

On endpoints like payments, order creation and sending notifications, this pattern isn't optional; it's mandatory. And setting it up isn't as hard as it's made out to be: a table or Redis for key-response pairs, and a check at the start of the request. Two days of work, and years of zero complaints about double processing.

When implementing it, watch for two subtleties: decide up front how long keys are retained (twenty-four hours is enough for most scenarios; keeping them forever is needless overhead), and don't forget the race condition — if two requests with the same key arrive in the same millisecond, both may go ahead on the assumption that "this key is new." The fix is a database-level unique constraint on the key column; the second request can't write its record, so it waits for the first one's result and returns that. We once examined the logs of a payment integration that skipped this detail; every one of its double charges had slipped through this race window during peak hours.

The compound interest of boring decisions

If you noticed, there's nothing flashy in this post: version discipline, correct pagination, retry safety. That's the nature of API design — decisions nobody notices when they're done well, and everybody feels when they're done badly. But they work like compound interest: the right boring decision made today comes back two years later as the sentence "our API keeps up with our growth without a hitch."

This trio has siblings too — a consistent error body format, rate limit headers that tell the client how much quota it has left, field-level partial responses — but these three pay for themselves on every project, at every scale. They're the first page of our checklist whenever we start a new API.

The crash incident from the beginning got off cheaply; the field was added back and the lesson was written down. But that team now opens every API change with the question "who breaks in the field?" If your API needs that kind of stress test, let's talk — a contract review is always cheaper than a production incident.