SkillSprout
Chapter 1 · Lesson 2
Beginner
9 min read

Clients, Servers and APIs

Overview

Nearly every system you will design is built from the same simple idea: a client asks, a server answers, and both agree on the format of the question and the answer. That agreement is the API. If you understand how a request travels over HTTP, what a REST API looks like, which status codes mean what, how to page through large lists, and what "idempotent" means, you can define the APIs for any design in step 3 of the framework.

Detailed Notes

What Is It?

A client is any program that starts a conversation: a mobile app, a web browser, or another service. A server is a program that waits for requests and sends back responses. "Server" describes a role, not a kind of machine; a laptop can be a server and a data center machine can be a client of another server.

An API (application programming interface) is the set of operations a server offers and the exact shape of requests and responses. Most web APIs use HTTP (Hypertext Transfer Protocol), a text-based request-and-response protocol. A request has a method (what to do), a path (which thing), headers (extra information like authentication) and sometimes a body (data). A response has a status code (a number saying what happened), headers and usually a body.

REST (representational state transfer) is a popular style for designing HTTP APIs. In REST you model the world as resources (nouns such as users, posts, orders) with URLs, and you use the HTTP method as the verb:

MethodMeaningExample
GETRead a resourceGET /v1/posts/42
POSTCreate a resourcePOST /v1/posts
PUTReplace a resource completelyPUT /v1/posts/42
PATCHChange part of a resourcePATCH /v1/posts/42
DELETERemove a resourceDELETE /v1/posts/42

Bodies are normally JSON (JavaScript Object Notation), a simple text format of keys and values.

Why Do We Need It?

A server stores data and runs business rules. Clients are many and untrusted: they may be old app versions, scripts or attackers. A clear API lets you:

  • Keep the logic and data in one controlled place, so every client sees consistent results.
  • Change the inside of the system (a new database) without breaking clients, as long as the API stays the same.
  • Scale the server side independently, because clients only know the API.
  • Put checks in one place: authentication (who are you?), authorization (are you allowed?) and validation (is this input sane?).

In an interview, the API is also the fastest way to turn vague features into precise operations. Writing "POST /v1/links" forces you to decide inputs, outputs and errors, and those decide your data model.

How Does It Work?

One HTTP Request, End to End
Request flow
ClientAPI serverDatabase1GET /v1/users/42Authorization: Bearer token2check token and permission3read user 424row5200 OK + JSON body6GET /v1/users/9997404 Not Foundclient error, so not retried

The client only knows the API. The server hides the data store behind it.

Follow one request. A user opens their profile in an app.

  1. The client builds an HTTP request: GET /v1/users/42 with an Authorization header carrying a token (a string proving who the caller is).
  2. The request reaches the server over the network (the next lesson explains how).
  3. The server authenticates the token, runs the business logic, reads the data store, and builds a response.
  4. The client receives 200 OK with a JSON body and displays it.

Status codes you must know

Status codes group by first digit:

  • 2xx success: 200 OK (read or update worked), 201 Created (a new resource exists), 204 No Content (success, nothing to return).
  • 3xx redirect: 301 Moved Permanently, 302 Found (temporary redirect).
  • 4xx the client made a mistake: 400 Bad Request (malformed input), 401 Unauthorized (not logged in), 403 Forbidden (logged in but not allowed), 404 Not Found, 409 Conflict (for example a name already taken), 429 Too Many Requests (you hit a rate limit).
  • 5xx the server failed: 500 Internal Server Error, 503 Service Unavailable (overloaded or down for the moment).

The 4xx versus 5xx split matters operationally: 4xx should not wake up an engineer, 5xx should. Clients also use it: retrying a 400 is pointless; retrying a 503 may succeed.

Designing an endpoint

A good endpoint spec has a method and path, inputs, outputs, and error cases.

http
POST /v1/posts
Authorization: Bearer <token>
Content-Type: application/json

{ "text": "Hello world", "media_ids": ["m_91"] }
json
{ "post_id": "p_123", "created_at": "2026-01-15T10:20:30Z" }

Return 201 Created, 400 when text is empty, 401 when the token is missing. Keep these habits: version the path (/v1/) so you can change it later, use plural nouns, and never put secrets in URLs (they end up in logs).

Pagination

A request such as "list all posts of a user" could return millions of rows. Pagination returns results in small pages. There are two common styles.

  • Offset pagination: GET /posts?offset=40&limit=20. Simple, and supports "jump to page 5". But the database must walk past the first 40 rows each time, so deep pages get slow, and if a new post arrives while the user pages, rows shift and the user sees duplicates or misses items.
  • Cursor pagination: GET /posts?limit=20&cursor=eyJpZCI6MTAwfQ. The cursor is an opaque token that marks the position (typically the last item's id or timestamp). The server continues after that position. It stays fast on deep pages and is stable when data changes, but you cannot jump to an arbitrary page.

Feeds and timelines use cursors; admin tables with page numbers may use offsets.

Idempotency

An operation is idempotent if doing it several times has the same effect as doing it once. GET, PUT and DELETE are idempotent by definition: deleting post 42 twice leaves the same final state. POST is not: sending "create order" twice may create two orders.

Why care? Networks fail. If a client sends a payment request and the connection drops before the response arrives, the client cannot know whether the server did the work. The safe action is to retry, and retries must not charge the customer twice. The standard fix is an idempotency key: the client generates a unique id for the intent ("this one purchase") and sends it in a header.

http
POST /v1/payments
Idempotency-Key: 7f3a9c2e-5b1d-4e0a-9a77-0c1d2e3f4a5b

The server stores the key with the result. If the same key arrives again it returns the stored result instead of repeating the work. The retries lesson in Chapter 3 goes deeper.

Interview tip. Common mistakes: putting verbs in URLs (/createPost), returning 200 with an error message in the body, using offset pagination on a feed with millions of rows, and forgetting that a payment or booking API needs idempotency.

Interview discussion. "For the feed I'll use cursor pagination because the list changes constantly and users scroll deep. For create-order I'll require an idempotency key so a client retry after a timeout cannot place a duplicate order."

Offset vs Cursor Pagination
Comparison

Offset

?offset=40&limit=20

  1. 1

    Client sends page number

    offset = page x 20

  2. 2

    DB skips 40 rows

    reads and discards them

  3. 3

    DB returns next 20

  • Easy to build
  • Can jump to any page
  • Slow on deep pages
  • Duplicates or gaps if data changes

Fine for admin tables.

Cursor

?cursor=abc&limit=20

  1. 1

    Server returns next_cursor

    encodes last item position

  2. 2

    Client sends cursor back

  3. 3

    DB seeks directly after it

    uses an index

  • Constant speed at any depth
  • Stable while data changes
  • No jump to page N
  • Cursor must be opaque and validated

Default for feeds and timelines.

Key point. Use cursors for large or fast-changing lists such as feeds; offsets only for small, stable tables.

Both return 20 items per page; they differ in how the next page is located.

Real-World Example

A food delivery app. The phone is the client. It calls GET /v1/restaurants?city=pune&limit=20 to show restaurants, POST /v1/orders (with an idempotency key) to order, and GET /v1/orders/o_77 to track it. The server checks the token, validates the cart, charges the card, stores the order, and answers 201 Created. If the phone loses signal after tapping "Pay" and retries, the idempotency key ensures one charge.

System Design Example

For a URL shortener, your step 3 might read:

  • POST /v1/links body {long_url, custom_alias?, expires_at?} returns 201 with short_url; 409 if the alias is taken; 400 for an invalid URL.
  • GET /{short_key} returns 302 with a Location header, or 404/410 Gone for unknown or expired keys.
  • DELETE /v1/links/{short_key} returns 204; idempotent, so deleting twice is fine.

Seven lines of API already tell you the system is read-heavy (the redirect), needs unique key handling (the 409) and stores an expiry field.

Trade-off thinking. REST is simple and cache-friendly, but a screen needing data from five resources makes five calls (or an over-fetching combined endpoint). Alternatives such as RPC or GraphQL change that balance. The trade-offs chapter compares REST and RPC. For most interviews, REST with JSON is a safe default; mention alternatives only if the problem demands it.

Key Interview Takeaway

Define APIs as nouns plus HTTP verbs, spell out inputs, outputs and error codes, paginate large lists (cursor for changing data), and make any operation that must not be repeated idempotent with a client-supplied key. The API is where requirements become operations that shape your data model.

Common Interview Questions

Finished this lesson?

0 of 44 lessons done · 0% of the course