mufassall

Cheat sheet · Foundations

The life of a request

10 min · Beginner · mufassall.dtwin.uz/en/l/request-lifecycle/

← Open the lesson

The problem

A user opens a page. Before a single byte of it arrives, the request looks up an address, opens a connection, agrees on encryption, passes a load balancer and an app server, and asks the database. Where do the milliseconds go?

What you learn

  • What DNS, TCP and TLS each cost before the request is even sent
  • What a load balancer adds on the way to the app server
  • Why one missing index can cost more than the whole network
  • Why distance hurts, and how a reused connection hides it
Final scene

Diagram as code

The lesson's final design as text: paste it into Jira, GitLab, GitHub, Notion or your docs, where Mermaid renders by itself. There are D2 and PlantUML (Confluence, IntelliJ) versions too.

Mermaid: flowchart

flowchart LR
  n_client(["Browser"])
  n_resolver>"DNS resolver"]
  n_lb{"Load balancer"}
  n_app["App server"]
  n_db[("Database")]
  n_client --> n_resolver
  n_resolver -.-> n_client
  n_client --> n_lb
  n_lb --> n_app
  n_app --> n_db
  n_db -.-> n_app
  n_app -.-> n_lb
  n_lb -.-> n_client

Mermaid: request sequence

sequenceDiagram
  participant n_client as Browser
  participant n_resolver as DNS resolver
  participant n_lb as Load balancer
  participant n_app as App server
  participant n_db as Database
  Note over n_client,n_resolver: First, find the address
  n_client->>n_resolver: QUERY shop.example.com IN A
  n_resolver-->>n_client: ANSWER shop.example.com
  Note over n_client,n_lb: Open a connection
  n_client->>n_lb: SYN 198.51.100.7:52114 -#62; 203.0.113.10:443
  n_lb-->>n_client: SYN-ACK 203.0.113.10:443 -#62; 198.51.100.7:52114
  Note over n_client,n_lb: Agree on encryption
  n_client->>n_lb: ClientHello
  n_lb-->>n_client: ServerHello
  Note over n_lb,n_app: The request reaches an app server
  n_lb->>n_app: GET /orders?page=1 HTTP/1.1
  Note over n_app,n_db: The database reads every row
  n_app->>n_db: SELECT id, status, total FROM orders
  n_db-->>n_app: Seq Scan on orders
  Note over n_app,n_db: An index finds the rows directly
  n_app->>n_db: CREATE INDEX orders_by_user
  n_db-->>n_app: Index Scan using orders_by_user
  Note over n_lb,n_client: The answer travels back
  n_lb->>n_client: HTTP/2 200
  Note over n_client,n_lb: The same page, from far away
  n_client->>n_lb: GET https://shop.example.com/orders?page=1
  Note over n_client,n_lb: The second request skips the handshakes
  n_client->>n_lb: GET https://shop.example.com/orders?page=2

D2

direction: right

n_client: "Browser" {
  shape: rectangle
}
n_resolver: "DNS resolver" {
  shape: cloud
}
n_lb: "Load balancer" {
  shape: diamond
}
n_app: "App server" {
  shape: rectangle
}
n_db: "Database" {
  shape: cylinder
}

n_client -> n_resolver
n_resolver -> n_client {
  style.stroke-dash: 3
}
n_client -> n_lb
n_lb -> n_app
n_app -> n_db
n_db -> n_app {
  style.stroke-dash: 3
}
n_app -> n_lb {
  style.stroke-dash: 3
}
n_lb -> n_client {
  style.stroke-dash: 3
}

PlantUML: request sequence

@startuml
participant "Browser" as n_client
boundary "DNS resolver" as n_resolver
participant "Load balancer" as n_lb
participant "App server" as n_app
database "Database" as n_db
note over n_client, n_resolver : First, find the address
n_client -> n_resolver : QUERY shop.example.com IN A
n_resolver --> n_client : ANSWER shop.example.com
note over n_client, n_lb : Open a connection
n_client -> n_lb : SYN 198.51.100.7:52114 -> 203.0.113.10:443
n_lb --> n_client : SYN-ACK 203.0.113.10:443 -> 198.51.100.7:52114
note over n_client, n_lb : Agree on encryption
n_client -> n_lb : ClientHello
n_lb --> n_client : ServerHello
note over n_lb, n_app : The request reaches an app server
n_lb -> n_app : GET /orders?page=1 HTTP/1.1
note over n_app, n_db : The database reads every row
n_app -> n_db : SELECT id, status, total FROM orders
n_db --> n_app : Seq Scan on orders
note over n_app, n_db : An index finds the rows directly
n_app -> n_db : CREATE INDEX orders_by_user
n_db --> n_app : Index Scan using orders_by_user
note over n_lb, n_client : The answer travels back
n_lb -> n_client : HTTP/2 200
note over n_client, n_lb : The same page, from far away
n_client -> n_lb : GET https://shop.example.com/orders?page=1
note over n_client, n_lb : The second request skips the handshakes
n_client -> n_lb : GET https://shop.example.com/orders?page=2
@enduml

PlantUML: components

@startuml
left to right direction
agent "Browser" as n_client
cloud "DNS resolver" as n_resolver
node "Load balancer" as n_lb
component "App server" as n_app
database "Database" as n_db
n_client --> n_resolver
n_resolver ..> n_client
n_client --> n_lb
n_lb --> n_app
n_app --> n_db
n_db ..> n_app
n_app ..> n_lb
n_lb ..> n_client
@enduml

Recap

A request pays for DNS, TCP and TLS before it is even sent, and each of those is a round trip.

1) Distance sets the price of every round trip. 2) Reused connections and cached DNS skip most of them. 3) Inside the data centre, one slow query can outweigh the whole network, so measure with Server-Timing and an index where it counts.

Numbers: at the start and at the end

MetricStartEnd
Time so far38 ms168 ms
Round trips11
Database time0 ms4 ms

Steps

  1. Case First, find the address

    The user types shop.example.com.

  2. Case Open a connection

    With the address known, the browser opens a TCP connection to port 443.

  3. Case Agree on encryption

    Over the new connection, the browser and the load balancer run a TLS 1.3 handshake.

  4. Case The request reaches an app server

    Now the browser sends GET /orders.

  5. Problem The database reads every row

    To list the user's orders, the app asks the database for the last 20.

  6. Fix An index finds the rows directly

    An index on (user_id, created_at) keeps each user's orders together and already sorted by date.

  7. Case The answer travels back

    The rows go back to the app, the app builds JSON, the load balancer compresses and encrypts it and sends it to the browser.

  8. Problem The same page, from far away

    Another user opens the page from another country over mobile data.

  9. Fix The second request skips the handshakes

    When she opens page 2, the browser still has the DNS answer and an open, encrypted HTTP/2 connection.

  10. Recap Recap

    A request pays for DNS, TCP and TLS before it is even sent, and each of those is a round trip.