API Design: REST, GraphQL, gRPC
System Design · Foundations · Basics · 13 min
How apps talk to servers, and how to design clean endpoints.
A long bookshelf you read three books at a time: the librarian counts 'skip 3, hand over the next 3, tell me if any are left'. If someone slips a new book in at the front while you read, a counting librarian hands you one you already saw; a bookmark-keeping librarian never does.
The idea in everyday words
An API is a restaurant menu. You don't walk into the kitchen; you order from the menu ('GET /pizza/42') and the waiter brings it. REST = a fixed menu with numbered dishes. GraphQL = tell the chef exactly which ingredients you want on one plate. gRPC = a super-fast walkie-talkie between kitchens.
REST in 5 rules
- Nouns for resources:
/users,/users/42/orders - Verbs from HTTP: GET read, POST create, PUT/PATCH update, DELETE remove
- Return proper status codes
- Paginate lists:
GET /posts?limit=20&cursor=abc - Version:
/v1/...
Pagination you can run
posts = [f"post-{i}" for i in range(1, 12)]
def get_page(items, page, size):
start = (page - 1) * size
return {"items": items[start:start + size], "has_more": start + size < len(items)}
print(get_page(posts, 1, 5))
print(get_page(posts, 3, 5))
Offset pages are simple; cursor pagination ("give me 20 after id X") stays correct when new items arrive.
REST vs GraphQL vs gRPC
| REST | GraphQL | gRPC | |
|---|---|---|---|
| Best for | Public APIs | Flexible mobile/web UIs | Service-to-service |
| Data | JSON | JSON, client picks fields | Protobuf (binary) |
| Pros | Simple, cacheable | No over/under-fetching | Very fast, typed |
| Cons | Many round trips | Complex caching | Not browser-friendly |
Idempotency: PUT/DELETE can be retried safely; for POST payments send an Idempotency-Key header so a retry doesn't charge twice.
Step by step
- Client:
POST /v1/orders {item: 'pizza'} + Idempotency-Key: abc
- Server:
validate → save → 201 Created {id: 77}
- Retry:
network blip → same key → server returns the same order (no double charge)
- Read:
GET /v1/orders/77 → 200
Code example
routes = {
("GET", "/users"): "list users",
("POST", "/users"): "create user",
("GET", "/users/{id}"): "get one user",
("DELETE", "/users/{id}"): "delete user",
}
for (method, path), what in routes.items():
print(f"{method:6} {path:12} -> {what}")Interview tips
Write the API before drawing boxes in a design interview.
Mention pagination, rate limiting, auth (tokens), and versioning.
Explain idempotency keys for payments.
Challenge: Paginate a List
Return the items for 1-based page with size items per page, plus whether more pages exist: {"items": [...], "has_more": bool}.
Example: paginate([1,2,3,4,5], 2, 2) → {"items": [3, 4], "has_more": true}
Solve it in the browser with tests, hints and an AI coach.
Practice problems
More in Foundations
- What Is System Design?
- 10,000 users at once: what really happens
- How to Approach a System Design Interview
- How the Internet Works: DNS, HTTP, TCP
Previous: How the Internet Works: DNS, HTTP, TCP · Next: AWS Fundamentals: Regions, AZs & IAM