Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Foundations of Scale
Lesson 6 of 16

API basics: REST, pagination and idempotency

Design endpoints that are predictable, cheap to serve and safe to retry.

15 min read

Resources and verbs

A REST API models the system as resources identified by URLs and manipulated with HTTP methods. GET /posts/42 reads a post, POST /posts creates one, PUT or PATCH /posts/42 updates it and DELETE /posts/42 removes it. Status codes carry meaning that clients, proxies and caches understand: 200 and 201 for success, 400 for bad input, 404 for missing resources, 429 for rate limiting and 503 when the service is overloaded. Following these conventions is not about style. GET responses can be cached by CDNs and browsers, and well-chosen status codes tell clients whether retrying makes sense.

  • GET: read, safe and cacheable
  • POST: create or trigger an action, not idempotent by default
  • PUT and DELETE: idempotent, repeating them has the same effect
  • PATCH: partial update

Pagination

Never return an unbounded list. A user with 50 000 orders would turn one request into a multi-megabyte response and a slow query. Offset pagination (?limit=20&offset=4000) is simple but gets slower as the offset grows, because the database still scans and discards the skipped rows, and items shift if new ones arrive between pages. Cursor pagination returns an opaque cursor, usually encoding the last item's sort key, and the next request asks for items after it. With an index on the sort key, every page costs the same no matter how deep the user scrolls.

Idempotency and safe retries

Networks drop responses, so clients retry. If a POST /payments times out, the client cannot know whether the charge happened. An idempotency key solves this: the client generates a unique ID per logical operation and sends it in a header. The server stores the key with the result, and when the same key arrives again it returns the stored result instead of doing the work twice. Keep keys for a sensible window, such as 24 hours, and enforce uniqueness in the database so two concurrent retries cannot both succeed.

Designing for scale at the edge

A few API choices make the backend dramatically cheaper to run. Return only the fields the client needs, compress responses, set Cache-Control headers on public GETs so the CDN can serve them, and version the API (/v1/) so you can evolve it without breaking old clients. Put rate limits on expensive endpoints and return 429 with a Retry-After header rather than letting abusive clients consume capacity. In a SystCode design, these decisions show up as fewer requests reaching your API tier and lower database utilisation.

Key takeaways

  • Use HTTP methods and status codes as intended; caches and clients depend on them
  • Paginate every list, and prefer cursors for deep or changing lists
  • Idempotency keys make retried writes safe
  • Cache headers, compact responses and rate limits reduce load before it reaches your servers

Sign in to track lessons and earn badges.

Stateless services and sessionsDNS and content delivery networks

Coming up

Course: 0/16 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 3/10Design Static Website HostingCombine DNS, a CDN and object storage so millions of static sites load fast without touching an origin server.Easy
  2. 4/10Design a Product CatalogServe read-heavy product pages with cache-aside over SQL and keep them fresh when a product is edited.
Easy
All 10 challenges →