GamifiedGamified challengesPracticeRaceLeaderboardPricing

Learn data structures by watching them move, then doing it yourself. Press M anytime to toggle sound.

A product of Amypo Technologies, Coimbatore.

Learn

  • All topics
  • Learning path
  • How it works
  • About dotprep

Practice

  • Practice arena
  • Daily challenge
  • Gamified challenges
  • Race mode
  • Leaderboard

Interview prep

  • Java interview
  • Coding questions
  • Company questions
  • System design
  • Jobs & careers

You

  • Dashboard
  • Settings
  • Pricing
  • Create account

© 2026 dotprep

Content last updated 20 November 2026

GamifiedGamified challengesPracticeRaceLeaderboardPricing
System designAPI design and rate limiting

API design and rate limiting

REST, gRPC and GraphQL, pagination, idempotency keys, versioning, and rate-limiting algorithms.

intermediate 12 min read 5 interview questions

On this page

1/6
  1. 01API styles
  2. 02Good REST habits
  3. 03Pagination
  4. 04Rate limiting algorithms
  5. 05Interview questions
  6. 06Quick check
Back to top

API styles

StyleShapeBest for
RESTResources and HTTP verbs: GET /users/42/ordersPublic and CRUD-style APIs; cacheable over HTTP
gRPCTyped RPC over HTTP/2 with Protocol BuffersFast internal service-to-service calls, streaming
GraphQLOne endpoint; clients ask for exactly the fields they needVaried clients (web, mobile) needing different data
WebSocket / SSELong-lived connections pushing updatesChat, 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, PUT and DELETE are idempotent; POST is 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.

text
SELECT * FROM posts
WHERE created_at < :cursor -- the last item the client saw
ORDER BY created_at DESC
LIMIT 50;

Rate limiting algorithms

AlgorithmIdeaNotes
Token bucketTokens refill at rate r up to capacity b; each request spends oneAllows bursts up to b; the most common choice
Leaky bucketRequests enter a queue drained at a fixed rateSmooth output rate; bursts wait or are dropped
Fixed window counterCount requests per clock minuteSimple; allows 2× bursts at window edges
Sliding window logKeep timestamps of recent requestsExact, but memory grows with traffic
Sliding window counterBlend this and last window's counts by overlapClose to exact with two counters
java
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 Requests
tokens -= 1;
return true;
}
}

See it animated

QueueSliding windowHash functions

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

Question 1

1.Which HTTP status means rate limited?

Question 2

2.Which method is not idempotent?

Question 3

3.Which rate limiter allows bursts while enforcing an average rate?

0 of 3 answered

Message queues and async processingReliability and observability
Log inStart free