System designby Learnastra

Concept lesson · Foundations

HTTP APIs and request lifecycle

By Anup Rai

Start here

Definition

An application programming interface (API) defines the agreed rules for how programs request data or actions from one another. An HTTP API expresses that contract through methods, resource paths, headers, request bodies, status codes, and response bodies.

Why it matters: Clients and services need to agree on the operation, identity, success meaning, errors, and retry behavior before implementation or scaling.

The visual modelHTTP request lifecycle and latency budget

DNS and connection setup may be cached or reused. The server still authenticates the request and enforces its data contract.

HTTP request lifecycle and latency budgetDNS and connection setup may be cached or reused. The server still authenticates the request and enforces its data contract. A cold request can require DNS lookup, connection establishment and TLS before HTTP reaches the service. The edge routes the request. The service authenticates the caller, authorizes the exact data view/version it reads, and returns the corresponding status and body. A warm pooled connection skips repeated setup. A timeout is the caller waiting limit, not necessarily cancellation of server work.GET /orders/O17: cold setup, then application workDNSresolve hostCONNECTTCPTLSserver identityHTTPsend requestCached DNS and pooled connections can avoid repeated setup.ROUTEgatewayAUTHN + AUTHZcaller + O17EXECUTEread orderRESPOND200 + bodyOne deadline includes all required work.TCP/TLS shown. HTTP/3 integrates TLS 1.3 into QUIC; warm connections reuse setup.
Read the diagram step by step
  1. A cold request can require DNS lookup, connection establishment and TLS before HTTP reaches the service.
  2. The edge routes the request. The service authenticates the caller, authorizes the exact data view/version it reads, and returns the corresponding status and body.
  3. A warm pooled connection skips repeated setup. A timeout is the caller waiting limit, not necessarily cancellation of server work.

Worked example

GET /orders/O17 authenticates user U9 and checks access to O17 before returning HTTP 200 with totalMinor=2500 and currency=USD, an order total of $25.00.

Key takeaways

  • DNS finds an endpoint; it does not fetch the business record.
  • Transport security protects the connection; authentication establishes identity; authorization checks permission.
  • A successful HTTP exchange can still report a business rejection or pending work.

You will learn to

  • Explain each hop of a request without hiding it in a cloud icon.
  • Define a concrete API and error contract.
  • Choose protocol, pagination, and compatibility behavior deliberately.

Practice in this chapter

8 interview questions with model answers and follow-ups.

Go to interview practice

Workload and timing examples are interview assumptions.

01API and HTTP request: definitions

An application programming interface (API) defines the agreed rules for how one program requests data or an action from another. An HTTP API represents that contract using a method, path, headers, optional request body, status code, and response body. The caller sends a request and receives a response; the contract defines the operation, input, identity, result, and failure behavior.

The edge is the service’s public entry point, often a proxy or load balancer. Authentication establishes who the caller is; authorization checks what that caller may access or change. The request lifecycle consists of name resolution, connection establishment, protocol exchange, routing, authentication, authorization, application execution, and response handling. Some stages can be cached or reused. A GET https://shop.example/orders/O17 example shows their order and distinct responsibilities; the API must identify user U9 and authorize access before returning order data.

A successful network exchange does not automatically mean a successful business operation. A server might return a valid “order not found,” an authentication error, or an infrastructure error. State what each result means before deciding which results are retryable.

02DNS and service discovery

DNS, the Domain Name System, maps names to records such as IP addresses. A client checks usable cached results or asks a recursive resolver. If necessary, the resolver follows the naming hierarchy to authoritative servers and returns an address for shop.example. A TTL specifies the record’s permitted cache lifetime under the protocol rules.

Concept in focusDNS resolution: names to addresses

The authoritative lookup is simplified: root and TLD referrals may be needed. DNS resolves names; it does not process the API request.

DNS resolution: names to addressesThe authoritative lookup is simplified: root and TLD referrals may be needed. DNS resolves names; it does not process the API request. Client to Resolver: Ask for api.example; a usable cache entry can end the lookup. Resolver to Authority: On a miss, follow referrals to the authoritative answer. Authority to Resolver: Return the address record and its TTL. Resolver to Client: Return the result; the client can now connect.ClientResolverAuthorityAsk for api.example; a usable cache entry can end the lookup.On a miss, follow referrals to the authoritative answer.Return the address record and its TTL.Return the result; the client can now connect.

Remember: Resolve the address, then connect to it.

Read the diagram
  1. Client to Resolver: Ask for api.example; a usable cache entry can end the lookup.
  2. Resolver to Authority: On a miss, follow referrals to the authoritative answer.
  3. Authority to Resolver: Return the address record and its TTL.
  4. Resolver to Client: Return the result; the client can now connect.

The address may point to an edge or load balancer rather than the database host. DNS does not authenticate U9 or return O17. It enables the next communication step. A cached address also explains why changing a DNS record does not instantly move every client during failover.

Internal service discovery may use DNS or a registry of healthy instances. It finds a server to contact. That server must still check the request and whether it is allowed to read or change the requested data.

03TCP, TLS, HTTP/2, and HTTP/3

For a typical HTTPS request using HTTP/1.1 or HTTP/2, the Transmission Control Protocol (TCP) provides a reliable ordered byte stream between endpoints. Transport Layer Security (TLS) authenticates the server and encrypts the conversation. The browser checks that the certificate is valid for the requested name. Existing connections may be reused, avoiding repeated setup.

Concept in focusRead the HTTPS stack from top to bottom

The arrows mean “uses the layer below.” QUIC integrates TLS security with its transport.

Read the HTTPS stack from top to bottomThe arrows mean “uses the layer below.” QUIC integrates TLS security with its transport. Compare the two protocol stacks by their vertical layering. HTTP/2 sits above TLS, TCP and IP. HTTP/3 sits above QUIC with TLS, then UDP and IP.Two HTTPS protocol stacksHTTP/2TLSTCPIPHTTP/3QUIC + TLSUDPIPRead downward: each layer uses the one beneath it.QUIC supplies reliable streams; UDP does not supply them.

Remember: HTTP/2 uses TCP; HTTP/3 uses QUIC over UDP.

Read the diagram
  1. Compare the two protocol stacks by their vertical layering.
  2. HTTP/2 sits above TLS, TCP and IP.
  3. HTTP/3 sits above QUIC with TLS, then UDP and IP.
Try from memoryDoes HTTP/3 get its reliable streams from UDP?

No. QUIC supplies its reliable streams and uses UDP as the underlying transport.

HTTP/2 multiplexes requests into separate application streams on one connection. It improves reuse, but TCP packet loss can stall delivery across those streams. HTTP/3 carries HTTP over QUIC, which uses User Datagram Protocol (UDP) packets and provides reliable streams and integrated cryptographic setup. QUIC avoids that particular cross-stream TCP head-of-line blocking; it does not remove all congestion, loss, or application queueing.

In an interview, start with the transport actually needed. An HTTPS API is usually enough to describe an order read. Choose streaming, bidirectional communication, or a different transport when the user interaction requires it, rather than listing protocols without a reason.

04HTTP request lifecycle: worked order read

A simplified request contract is:

GET /orders/O17 HTTP/1.1
Host: shop.example
Authorization: Bearer <access-token>
Accept: application/json
  1. The edge accepts the connection and routes /orders/O17 to the order API.
  2. The API validates the credential and derives userId=U9. It never trusts a caller-provided user ID as proof of identity.
  3. It performs an authorized lookup of O17 under U9’s verified scope, returning the row and version to which the access decision applies. Knowing the order ID is not authorization.
  4. It serializes the permitted fields from that same authorized version and returns 200 with {"orderId":"O17","state":"paid","totalMinor":2500,"currency":"USD"}.
  5. The browser parses the result and renders the order. Its total latency includes network, queueing, application, database, and rendering time.

The HTTP/1.1 notation is a readable contract illustration; HTTP/2 and HTTP/3 frame the same method/path/status semantics differently. A trace ID propagated through the servers helps diagnose where this particular request spent time.

Worked example diagramThe DNS lookup discovers the endpoint. The order read follows a separate connection and authorization path.
HTTP APIs and request lifecycle: architecture diagram1. Client: GET /orders/O17 to 2. DNS resolver: resolve shop.example; 1. Client: GET /orders/O17 to 3. Edge: TLS and routing: HTTPS request; 3. Edge: TLS and routing to 4. Order API: authenticate and authorize: forward to order handler; 4. Order API: authenticate and authorize to 5. Order database: authorized lookup of O17; 5. Order database to 4. Order API: authenticate and authorize: authorized row + version; 4. Order API: authenticate and authorize to 3. Edge: TLS and routing: response through edge; 3. Edge: TLS and routing to 1. Client: GET /orders/O17: 200 permitted representation1 → 2: resolve shop.example1 → 3: HTTPS request3 → 4: forward to order handler4 → 5: authorized lookup of O175 → 4: authorized row + version4 → 3: response through edge3 → 1: 200 permitted representation01Client: GET/orders/O1702DNS resolver03Edge: TLS androuting04Order API:authenticate andauthorize05Order database
  1. 1 → 2resolve shop.exampleClient: GET /orders/O17 → DNS resolver
  2. 1 → 3HTTPS requestClient: GET /orders/O17 → Edge: TLS and routing
  3. 3 → 4forward to order handlerEdge: TLS and routing → Order API: authenticate and authorize
  4. 4 → 5authorized lookup of O17Order API: authenticate and authorize → Order database
  5. 5 → 4authorized row + versionOrder database → Order API: authenticate and authorize
  6. 4 → 3response through edgeOrder API: authenticate and authorize → Edge: TLS and routing
  7. 3 → 1200 permitted representationEdge: TLS and routing → Client: GET /orders/O17

05HTTP methods, idempotency, and status codes

The method tells the server what kind of operation the client intends. A safe method requests read-only behavior; an idempotent method has the same intended effect when repeated as when performed once. These properties help decide whether repeating an interrupted request is compatible with the API contract.

Operation Example Meaning
Read a resource GET /orders/O17 Retrieve a representation without requesting a state-changing purchase
Create a logical resource POST /orders Validate and create; use an operation key for safe retries
Replace a named representation PUT /profiles/U9 Repeating the same intended replacement has idempotent method semantics
Delete a resource DELETE /orders/O17 Enforce the resource's deletion/cancellation policy

Use distinct results so the caller can decide what to do next:

Result Meaning in this contract
400 Invalid input
401 Authentication is required or invalid
403 Forbidden access, where disclosure is appropriate
404 Resource unavailable or deliberately not disclosed
409 State conflict
429 Rate limit
Relevant 5xx Server-side failure
202 Accepted for processing; not completed. Return an operation ID the client can inspect

The exact privacy and retry policy belongs in the API contract.

A conditional request adds a precondition about the current representation. A reader can ask whether its cached version is unchanged; an editor can require that the version it edited is still current before replacing it. Both use a server-issued version identifier rather than assuming nothing changed between requests.

Conditional requests connect HTTP to versioned data:

Request condition Purpose Outcome
GET with If-None-Match for a cached ETag Revalidate without downloading unchanged bytes If unchanged, 304 lets the client reuse its cached representation
PUT with strong If-Match for the edited version Prevent overwriting a representation changed since it was read 412 rejects an unmet version precondition

06Resource-oriented HTTP, RPC, and pagination

The same order operation can be exposed by naming a resource and an HTTP method, or by naming a remote procedure with typed arguments. These are interface choices: they determine how the caller expresses its request, while the service still defines ownership, permissions and success.

Contract style Example Useful when Limit
Resource-oriented HTTP GET /orders/O17, POST /orders Broad web-client support and familiar HTTP semantics URLs and JSON alone do not satisfy every REST constraint
Typed RPC ReserveSeats(show, seatIds) Explicit service operations and generated client schemas Requires compatible clients, deadlines, and error contracts
gRPC implementation of RPC Typed unary or streaming call Internal typed calls and supported streaming clients Browser/intermediary compatibility may need a gateway; no storage guarantee is implied

A resource-oriented HTTP API exposes orders and profiles through URLs and standard methods; REST is an architectural style with additional constraints, not merely a synonym for JSON. RPC means remote procedure call. An RPC API names an operation on another service, such as ReserveSeats, with typed input and output. gRPC commonly uses protocol buffers and HTTP/2 for typed calls and streaming. Browser clients and intermediaries may require compatible gateways or gRPC-Web support.

Choose a style for clients, tooling, and communication needs. A public web API benefits from familiar HTTP behavior and broad client support. Internal typed service calls may benefit from generated clients and schemas. Neither choice determines database consistency or business correctness.

Pagination contract

Bound response sizes. For order history, use a page size and cursor based on a stable ordering such as (createdAt, orderId). Validate the cursor and preserve tie-breaking semantics. Pagination is part of the API; it must match the query/index design rather than being added after the storage choice.

For descending order history, the continuation predicate is (createdAt, orderId) < (lastCreatedAt, lastOrderId) with the same ORDER BY createdAt DESC, orderId DESC and a bounded limit. Include the verified tenant, filters and ordering version in the cursor's validated scope. A cursor is not permission to switch tenants.

GraphQL: client flexibility and server cost

GraphQL provides a typed API schema and lets a client request a particular selection of fields. A query such as an order with selected item fields can reduce over-fetching and combine related reads. Field resolvers may call databases or services; one client request can still trigger many backend requests. Batch related lookups to avoid an N+1 pattern, where fetching N items adds N individual calls.

Use object/field authorization and bounded pagination. Limit expensive query shapes with complexity, depth and execution budgets or an approved operation set. A shared endpoint does not make all queries equally cheap, and authentication alone does not authorize nested objects. Prefer this flexibility when clients genuinely need different composed views; a small REST or gRPC contract is often simpler for a fixed workflow.

07API retries, deadlines, and version compatibility

A create-order timeout leaves the result unknown: the server may already have committed. A stable request key and status lookup recover the original outcome. A deadline bounds the caller’s wait; it does not roll back work committed elsewhere.

Concept in focusA timeout leaves the outcome unknown

The crossed message stops before reaching the client. A timeout describes what the caller observed, not whether the server committed.

A timeout leaves the outcome unknownThe crossed message stops before reaching the client. A timeout describes what the caller observed, not whether the server committed. Client to Service: Create order with operation key K. Service to Database: Commit order O17 and the result for K. Service to Client: Reply is lost; the client deadline expires. Client to Service: Retry K or query its status. Service to Client: Return the saved O17 outcome rather than creating another order.ClientServiceDatabaseCreate order with operation key K.Commit order O17 and the result for K.Reply is lost; the client deadline expires.Retry K or query its status.Return the saved O17 outcome rather than creating another order.

Remember: No reply does not mean no effect.

Read the diagram
  1. Client to Service: Create order with operation key K.
  2. Service to Database: Commit order O17 and the result for K.
  3. Service to Client: Reply is lost; the client deadline expires.
  4. Client to Service: Retry K or query its status.
  5. Service to Client: Return the saved O17 outcome rather than creating another order.

Version the contract when making incompatible changes. Additive optional fields are often easier to roll out than renaming a required field, but clients must actually tolerate unknown fields and defaults. Deploy producers and consumers in an order that supports mixed versions. For a breaking change, define an explicit migration/version policy rather than assuming all clients upgrade at once.

Spoken answer: “I define the order operation and its success meaning first. Then I trace DNS, connection, edge routing, authentication, authorization, database lookup, and response. I measure the time spent in each stage, limit request size and execution time, and make timeout recovery and compatibility part of the contract.”

Practise the interview questions

Say your answer aloud before opening the model answer. Then answer the follow-up and compare the reasoning.

Foundation · Question 1

What is an API, and what happens in a GET /orders/O17 request?

Reveal a model answer

An API is a contract between programs for an operation and its inputs, results, and failures. For GET /orders/O17, the client resolves the service name and establishes or reuses a protected connection. The edge routes the request; the order service validates the credential, derives user U9, checks U9’s permission for O17, and returns an authorized representation.

What the answer must demonstrate: Define the contract before tracing the complete request path.

Foundation · Question 2

Why use TLS if the API already checks a token?

Reveal a model answer

“The token identifies or authorizes the caller, but a plaintext network could expose or alter it. TLS protects the communication and authenticates the server endpoint. I still validate the token and resource permission inside the service.”

What the answer must demonstrate: Separate transport protection from access checks.

Applied · Question 3

Does HTTP/3 eliminate head-of-line blocking everywhere?

Reveal a model answer

“No. QUIC avoids TCP’s cross-stream loss-delivery blockage, but each stream still has ordering requirements and the application, queues, or shared resources can block progress. I choose it for actual transport needs, not as a blanket latency guarantee.”

What the answer must demonstrate: Name the specific bottleneck that changes.

Applied · Question 4

How are safe and idempotent requests different?

Reveal a model answer

“Safe methods do not ask for a state-changing action. Idempotent methods have the same intended effect when repeated. A deletion can be idempotent while still changing state. I do not use a GET to trigger a purchase merely because it is easy to call.”

What the answer must demonstrate: Explain intended effect, not identical response bytes.

Applied · Question 5

What does 202 Accepted tell the caller?

Reveal a model answer

The request was accepted for processing, not completed. For our recoverable API, I durably commit an operation record and outgoing intent before 202, then return an operation ID and status location. HTTP 202 alone does not establish that storage guarantee.

What the answer must demonstrate: Distinguish acceptance and completion.

Applied · Question 6

Would you choose REST or gRPC for every service?

Reveal a model answer

“I choose from client compatibility, schema tooling, and streaming needs. A public browser-facing API may use resource-oriented HTTP/JSON; internal typed calls may use gRPC. Both still need deadlines, authorization, and a defined retry contract.”

What the answer must demonstrate: Avoid assigning storage guarantees to a protocol.

Applied · Question 7

What belongs in a cursor for order history?

Reveal a model answer

“A stable position in the chosen order, such as the last creation timestamp plus a unique order ID. The service validates it, applies the same ordering, and caps page size. I also define whether new or deleted records can change later pages.”

What the answer must demonstrate: Match the cursor to the index and contract.

Applied · Question 8

How do you rename a required response field safely?

Reveal a model answer

“I cannot assume all clients update together. I might serve both fields during migration or introduce a versioned contract, measure adoption, and retire the old field under an explicit policy. I test mixed client/server versions.”

What the answer must demonstrate: Describe a mixed-version rollout.

Blank-page exercise · 20 minutes

Build the answer yourself

Explain a browser-to-order read, then define an asynchronous create-order API that survives a lost response.

  • Name each hop and responsibility.
  • Distinguish TLS, identity, and permission.
  • Specify accepted versus completed results.
  • Define request identity, pagination, and one compatibility change.

Check that each component and design decision follows from your requirements and workload.

Recall the key ideas

Answer from memory before opening each card. Explain why the choice works and what it costs. Revisit missed cards tomorrow.

HTTP APIs and request lifecycleOne request pathRecall first, then reveal

Name lookup → protected connection → routing → identity/permission → data operation → response.

Trace the request.

Return to lesson
HTTP APIs and request lifecycleDoes 202 Accepted mean the order is complete?Recall first, then reveal

No. It means the server accepted the work. Return an operation ID or status URL so the client can check progress.

Accepted → check progress → completed.

Return to lesson
HTTP APIs and request lifecycleAPI designRecall first, then reveal

Define success, error, retry, size, pagination, and compatibility behavior.

A URL is not the whole contract.

Return to lesson

Final revision

Summary and interview notes

An API defines behavior as well as URLs and JSON. Trace name lookup, connection, routing, access checks and the data operation. Specify success, errors, result-size limits, retry behavior and compatibility with older clients.

Remember these points

  • DNS discovers an endpoint; TLS protects a connection; the application still authenticates and authorizes the caller.
  • Safe and idempotent describe intended method effects, not identical responses or unlimited retry safety.
  • 202 means accepted, not complete; a durable operation record is an explicit application mechanism.
  • ETags support representation validation and conditional writes but never replace authorization.
  • A page cursor needs a unique tie-breaker and must preserve the query’s tenant and filters. To reproduce an export, also keep a fixed snapshot of its results.

Interview tips

  • Walk one request through actual boundaries and account for reused connections and caches.
  • Define one asynchronous response, one conflict response and one lost-response retry.
  • Show a concrete pagination predicate and a mixed-version client rollout.

Important qualifications

  • HTTP/3 avoids TCP cross-stream loss blocking, not every source of queueing or stream delay.
  • Authorization must apply to the returned representation version; a later unrelated body read can invalidate an earlier permission decision.

Technical references

Practice marks stay in this browser.