Buildfest Learn System DesignRead and experiment freely · Chapter activities stay in this tab
Practice librarySystem DesignRequests networking and APIs

LESSON 02 • SYSTEM DESIGN

Requests networking and APIsFollow one request.
Understand every boundary.

Requests networking and APIs. Learn the concepts, explore a model and defend your design.

DEFINITION

A request is a message asking a service for information or an action. Networking carries it between machines. An API is the contract defining accepted inputs, actions, outputs and errors. A good API makes ownership, limits and failure behavior clear rather than assuming that a valid URL or a successful connection implies permission.

Finding a service is different from asking it for data
  1. STEP 11. DNS lookupFind the service address
  2. STEP 22. TLS connectionProtect communication
  3. STEP 33. HTTP requestAsk the application

Connections can be reused. DNS is a lookup step, not a database hop for every request.

CONCEPT 1

How your browser finds a server

DNS is a name lookup

Typing a website name is like looking up a business in a contact list. The Domain Name System, or DNS, helps turn a name into records used to locate a service. The answer may be cached for a while, so changing a server address does not instantly change every user's view. DNS is not the server that stores your orders; it helps the client find where to send requests.

Connections and TLS

A client establishes a communication connection using an appropriate transport. TLS provides encryption in transit and helps the client verify the server's identity. An analogy is using a sealed conversation with someone whose identity you checked. That analogy has a limit: encryption cannot stop an authorized server from making a programming mistake or deciding the wrong user may see a receipt.

The request still has more work to do

A reverse proxy can accept the request and forward it to the application. The application may call a database or another service. The response then travels back. Slow DNS, connection setup, server computation, storage waits and large downloads can all contribute to a slow user experience. Measuring only the application function misses part of the journey.

CONCEPT 2

HTTP is the language of the exchange

Methods express intent

HTTP defines request and response behavior. GET asks for a representation; POST commonly asks a resource to process submitted data. PUT describes creating or replacing a resource at the chosen address. DELETE asks to remove a resource association. These names do not implement correctness by themselves. The application must make its actual behavior match the contract it presents.

Status codes communicate a result

A 200 response usually means the request succeeded. A 201 can identify creation. A 400 indicates a problem with the request, 401 concerns missing or invalid authentication, 403 indicates refusal and 404 indicates the resource was not found. A 429 is useful for rate limiting. A 500-class result indicates a server-side failure. Exact error details must avoid leaking private information.

Version and transport are separate from your business rules

HTTP/1.1, HTTP/2 and HTTP/3 differ in how communication is carried and organized. Newer transport behavior can help some workloads, but does not repair a slow database query or broken permission check. HTTP is described as stateless because each request has its own meaning; a web application can still maintain account and session state outside that protocol exchange.

Read an HTTP exchange
  1. CONCEPTRequestMethod + path + headers + body
  2. CONCEPTProcessingValidate input and permission
  3. CONCEPTResponseStatus + headers + body

A status code summarizes the result; the API contract explains what the result means.

CONCEPT 3

An API is a contract between programs

Specify inputs and outputs

An application programming interface, or API, says how one program can use another. For POST /orders, describe accepted item identifiers, quantity limits, response fields and errors. The server determines the authenticated customer and trusted price. It must not accept a client-supplied owner or total as proof. TypeScript types help developers, while server validation protects the running system.

Keep list responses bounded

Suppose GET /my-orders returns one page of orders. A maximum page size prevents a single request from asking for everything. Offset pagination skips a number of rows; it is simple for small lists, but concurrent inserts can shift positions. Cursor pagination continues after an ordering position. Use a stable tie-breaker, such as creation time plus an order ID, so two orders with the same timestamp can still be ordered.

What a cursor does not do

A cursor is a bookmark, not an access pass. Even if it contains an order ID or is signed, the server must restrict the results to records the caller may read. Changing a contract needs care because old clients may keep running. Adding an optional response field is usually easier to manage than silently changing what an existing field means.

Identity is not the same as permission
  1. STEP 1AuthenticateWho is requesting?
  2. STEP 2AuthorizeMay they read order 52?
  3. STEP 3ReturnOnly permitted fields

Knowing the user identity is the start of the access check, not its conclusion.

CONCEPT 4

Choose how the client receives updates

Request-based communication

With polling, the client repeatedly asks whether an order is ready. It is easy to understand, but frequent checks create repeated work. Long polling lets a request wait for an update or a timeout before the client asks again. Server-Sent Events can stream server-to-client updates over HTTP. These choices fit different update frequency, connection and deployment requirements.

Two-way and service communication

WebSockets support a continuing two-way connection, useful for chat or interactive applications. A connection still needs authentication, limits and reconnection behavior. REST-style APIs organize operations around resources. GraphQL lets clients request a structured set of fields, while gRPC uses defined service contracts and commonly generated clients. None is automatically best for every application.

A canteen example

If order status changes only twice in ten minutes, polling every 15 seconds may be an acceptable first version. If thousands of users need near-immediate delivery updates, a streaming connection could reduce pointless checks. Decide using update frequency, user expectations, operating cost and mobile network behavior. Reconnection should ask for missed state; an open connection alone does not make messages durable.

Three ways to deliver updates
  1. CONCEPTPollingClient asks repeatedly
  2. CONCEPTSSEServer sends one-way events
  3. CONCEPTWebSocketBoth sides exchange messages

Choose using update frequency, acceptable delay and recovery behavior, not novelty.

Key learning points

  • DNS locates a service; it is not the application database.
  • TLS protects communication in transit but does not decide business authorization.
  • Trace connection setup, proxy, application, storage and response transfer.
  • Validate inputs at the server and derive ownership from authenticated identity.
  • Bound list sizes and give pagination a stable ordering.

Compare the important ideas

Requests networking and APIs: concepts and trade-offs
ChoiceHow it worksGood fitMain caution
PollingClient asks repeatedlyInfrequent status updatesRepeated checks add load
Server-Sent EventsServer streams updates to a clientOne-way live updatesReconnect and missed-state policy still needed
WebSocketsContinuing two-way connectionInteractive chat or collaborationConnection state is not durable message storage
Cursor paginationContinue after an ordered positionLarge changing listsCursor does not authorize access

WORKED EXAMPLE

Trace an order-status request

Riya asks for order 52. The browser uses DNS as needed, establishes or reuses a secure connection and sends a request. The application verifies identity and checks that Riya may read order 52 before returning the current status. If the status changes only twice in ten minutes, polling every 15 seconds may be a reasonable first version. The interval is a product decision, not a universal recommendation.

Further reading

Original teaching explanations by Buildfest Learn. Check these primary references for the underlying concepts and implementation details.