API styles
| Style | Shape | Best for |
|---|---|---|
| REST | Resources and HTTP verbs: GET /users/42/orders | Public and CRUD-style APIs; cacheable over HTTP |
| gRPC | Typed RPC over HTTP/2 with Protocol Buffers | Fast internal service-to-service calls, streaming |
| GraphQL | One endpoint; clients ask for exactly the fields they need | Varied clients (web, mobile) needing different data |
| WebSocket / SSE | Long-lived connections pushing updates | Chat, live scores, notifications |
Good REST habits
- Nouns for resources, verbs from HTTP:
POST /orders,GET /orders/7,PATCH /orders/7. - Correct status codes: 201 Created, 400 bad input, 401 not authenticated, 403 not allowed, 404, 409 conflict, 429 too many requests, 5xx server errors.
GET,PUTandDELETEare idempotent;POSTis not. For payments, accept an Idempotency-Key header so a retried POST doesn't charge twice.- Version the API (
/v1/…or a header) so you can change it without breaking clients.
Pagination
Offset pagination (?offset=200&limit=50) is simple but slow for deep pages (the DB still walks the skipped rows) and shifts when rows are inserted. Cursor pagination (?after=<last id>&limit=50) uses an indexed position: fast at any depth and stable under inserts. Feeds and infinite scroll use cursors.
SELECT * FROM postsWHERE created_at < :cursor -- the last item the client sawORDER BY created_at DESCLIMIT 50;
Rate limiting algorithms
| Algorithm | Idea | Notes |
|---|---|---|
| Token bucket | Tokens refill at rate r up to capacity b; each request spends one | Allows bursts up to b; the most common choice |
| Leaky bucket | Requests enter a queue drained at a fixed rate | Smooth output rate; bursts wait or are dropped |
| Fixed window counter | Count requests per clock minute | Simple; allows 2× bursts at window edges |
| Sliding window log | Keep timestamps of recent requests | Exact, but memory grows with traffic |
| Sliding window counter | Blend this and last window's counts by overlap | Close to exact with two counters |
class TokenBucket {private final double rate, capacity;private double tokens;private long last = System.nanoTime();TokenBucket(double perSecond, double burst) { rate = perSecond; capacity = burst; tokens = burst; }synchronized boolean allow() {long now = System.nanoTime();tokens = Math.min(capacity, tokens + (now - last) / 1e9 * rate);last = now;if (tokens < 1) return false; // respond 429 Too Many Requeststokens -= 1;return true;}}
See it animated
Interview questions
Try answering out loud first, then open the model answer.
Q1PUT vs POST vs PATCH?beginner
POST creates a resource (or triggers an action) and isn't idempotent. PUT replaces a resource at a known URL and is idempotent. PATCH partially updates a resource; it's idempotent only if designed that way.
Q2How do you stop a retried payment request charging twice?intermediate
The client sends a unique Idempotency-Key with the request. The server stores the key with the result; a repeat with the same key returns the stored result instead of charging again. Keys expire after a day or so.
Q3Why is cursor pagination better than offset for feeds?intermediate
Offset makes the database skip N rows, which gets slow for deep pages, and new posts shift the offsets so users see duplicates or miss items. A cursor (the last seen id or timestamp) uses an index to start exactly where the client left off.
Q4Explain the token bucket algorithm.intermediate
A bucket holds up to b tokens and refills at r tokens per second. Each request takes a token; with none left it's rejected (HTTP 429). It enforces an average rate r while allowing short bursts up to b, and needs only two numbers per client, so it's easy to keep in Redis.
Q5When would you pick gRPC over REST?intermediate
For internal service-to-service calls where performance and strict contracts matter: binary Protocol Buffers are compact, HTTP/2 multiplexes calls on one connection, it supports streaming, and clients are generated from the schema. REST remains easier for public APIs, browsers and HTTP caching.
Quick check
0 of 3 answered