1. Courses
  2. System Design
  3. How the Web Works
  4. Stop 2

30 minBeginner

What happens when you open an app or website

Between a tap on the screen and the data appearing, your request makes several trips across the network. This lesson follows one request step by step, so you can see where the time goes and which steps you can remove.

By the end you can

  • Name the steps of a request in order: DNS, TCP, TLS, HTTP request, server work, response
  • Explain how DNS finds a server and what a DNS TTL does
  • Read the parts of a raw HTTP request and response
  • Explain why HTTP is stateless and how tokens and cookies carry identity
  • Measure each step of a real request with curl

The everyday picture

You want to order dinner from a restaurant you have never called. First you find the phone number. You search for the name and get a number. Next you dial and wait for someone to pick up. Then you check it is really the right restaurant, because you are about to give your address. Then you place the order in the way they expect: dish, quantity, address. Finally the food arrives.

Opening an app works the same way. Finding the number is DNS. Dialling and waiting for "hello" is TCP. Checking it is really them is TLS. Placing the order in the agreed format is the HTTP request. The food is the HTTP response. Every step costs time, and on a bad phone line each step takes longer.

The whole journey in one picture

The app on your phone is the . The program that answers is the . Between them, every step that needs an answer from the other side costs one . On office Wi-Fi a round trip to a nearby data centre can take a few milliseconds. On weak mobile data in a busy market it can take hundreds.

Phone DNS Server DNS 1. look up name 203.0.113.10 TCP 2. SYN SYN-ACK TLS 3. ACK + TLS hello certificate + keys HTTP 4. GET /stock/4711 5. server works 6. 200 OK + JSON 7. app draws the screen
One first request, with time flowing down. DNS, TCP and TLS each cost about one round trip before the app can even ask for data. The request and response cost one more, plus the time the server spends working.

Count the arrows that cross the whole width. Before the app sends its first useful byte, it has already spent around three round trips. If one round trip takes 150 ms, that is roughly 450 ms of waiting with nothing to show. Most of those steps happen only once, though. The next request on the same connection skips steps 1 to 3.

Step 1: DNS turns a name into an address

Computers on the internet find each other by , not by name. is the phone book that turns api.example.com into an address like 203.0.113.10.

The lookup goes through a chain, and every level keeps a copy of answers it has seen. First the app or browser checks its own memory. Then the phone's operating system checks its cache. If both miss, the phone asks a , usually run by the internet provider or a public service. If the resolver has no copy either, it walks the chain itself: the root servers point it to the .com servers, and those point it to the domain's own name server, which gives the final answer.

1. App cache not cached 2. Phone OS cache not cached 3. Resolver not cached, so it asks: 4. Root server ask .com servers 5. .com server ask ns1.example.com 6. Name server 203.0.113.10 TTL 300 s answer is cached at each level until its TTL ends
A DNS lookup with every cache empty. The resolver asks three servers in turn. The answer comes back with a TTL, and each level reuses it until the TTL expires, so most lookups stop at step 1, 2 or 3.

The domain owner sets a on each DNS record, for example 300 seconds. That number is a trade-off. A long TTL means fewer lookups and faster first requests. A short TTL means that when you move your API to a new server, clients find the new address sooner. Before a planned move, teams lower the TTL a day ahead, make the change, then raise it again.

A full lookup with empty caches can take tens to hundreds of milliseconds. A cached one takes almost nothing. This is one reason the first open of an app after a long time feels slower than the second.

Quick check

You used an app two minutes ago, and its API's DNS record has a TTL of 300 seconds. You open it again. Where does the DNS lookup most likely end?

Steps 2 and 3: TCP connects, TLS proves identity

With an address in hand, the phone opens a connection. It sends a "can we talk?" message (SYN), the server replies "yes" (SYN-ACK), and the phone confirms (ACK). That is the . Networking you actually need looks at it in more detail.

Next comes , the S in HTTPS. The server sends its , which proves it really owns api.example.com. The two sides agree on encryption keys. From now on, nobody on the café's Wi-Fi or the mobile network can read or change the data. TLS 1.3 needs one round trip for this. The older TLS 1.2 needs two.

Here is the most useful fact in this section: a connection can be reused. HTTP/1.1 keeps connections open by default (called ), and HTTP/2 can send many requests at once over one connection. A reused connection skips DNS, TCP and TLS completely. The server decides how long an idle connection stays open. Node's HTTP server, which NestJS runs on, keeps it for 5 seconds by default (in Node 22; check your version), and you will see that in a header below.

Quick check

Which request can skip DNS, TCP and TLS completely?

Steps 4 to 6: the HTTP request and response

is the agreed format for the order. In HTTP/1.1 it is plain text you can read. HTTP/2 and HTTP/3 send the same parts in a compact binary form, but the parts are the same.

Request: app to server POST /orders HTTP/1.1 Host: api.example.com Authorization: Bearer eyJ… Content-Type: application/json {"articleId":4711,"qty":2} method, path, HTTP version headers blank line body: the data Response: server to app HTTP/1.1 201 Created Content-Type: application/json Content-Length: 30 Connection: keep-alive {"id":9001,"status":"PENDING"} version and status code headers blank line body: the result
The same four parts in both directions: a first line, headers, an empty line, then the body. The first line of the request says what you want; the first line of the response says what happened.

The request has four parts:

  • The method says what kind of action: GET reads, POST creates, PUT or PATCH changes, DELETE removes. The path says which thing: /orders or /sites/12/stock/4711.
  • Headers are extra facts as Name: value lines. Host says which website, because one server can host many. Authorization says who is asking. Content-Type says how the body is written.
  • An empty line marks where the headers end.
  • The body carries data. A GET usually has none. A POST carries the new order as JSON.

The response has the same shape. Its first line has a : 200 means OK, 201 means created, 404 means not found, 500 means the server broke. APIs done right covers which codes to use when.

A website and a mobile app use HTTP the same way. The difference is what happens after the response. A browser gets HTML, then makes more requests for CSS, JavaScript and images, then draws the page. A mobile app such as the Mangrove app usually gets JSON and draws native screens from it. It skips the HTML, but it pays the same DNS, TCP, TLS and round-trip costs.

Quick check

A response starts with HTTP/1.1 404 Not Found. What does the app know before it reads the body?

HTTP forgets you after every request

HTTP is . The server does not remember that this phone logged in a minute ago. So every request must carry its own identity. Web apps usually send a . Mobile apps usually send a token in a header: Authorization: Bearer eyJ…. The server checks it on every request.

This sounds wasteful, but it is a gift for scaling. Since no server remembers anything about you, any copy of the server can answer your next request. That is what lets you run three API servers behind a load balancer later (Scaling up vs scaling out). How to make and check tokens safely is in Security essentials.

Think first: a user opens the app, waits 3 seconds, then taps "refresh". Why is the refresh much faster than the first load?

On the first load, the app paid for a DNS lookup, a TCP handshake and a TLS handshake before it could send the request. On the refresh, the DNS answer is cached and the connection is probably still open, because the 5-second keep-alive has not run out. So the refresh pays only one round trip plus the server's work.

If the user waits 10 seconds instead, the server has likely closed the idle connection, and the app pays for TCP and TLS again (DNS is probably still cached).

Build it

We will run a tiny NestJS API, read its raw HTTP, then measure each step of a real request.

  1. A minimal NestJS controller that returns JSON

    Add this to a new Nest project (npx @nestjs/cli new stock-api) and register the controller in AppModule. It uses a fake array, so you need no database yet. In Mangrove's data a site is a store or a DC, and an article is a product, so this endpoint returns the stock of one product at one store.

    TypeScript
    // src/stock.controller.ts
    import { Controller, Get, NotFoundException, Param, ParseIntPipe } from '@nestjs/common';
    
    type StockDto = { siteId: number; articleId: number; qty: number; updatedAt: string };
    
    const fakeStock: StockDto[] = [
      { siteId: 12, articleId: 4711, qty: 42, updatedAt: '2026-10-06T09:15:02.118Z' },
    ];
    
    @Controller('sites/:siteId/stock')
    export class StockController {
      // GET /sites/12/stock/4711
      @Get(':articleId')
      findOne(
        @Param('siteId', ParseIntPipe) siteId: number,       // "12" becomes 12, "abc" gives 400
        @Param('articleId', ParseIntPipe) articleId: number,
      ): StockDto {
        const row = fakeStock.find((s) => s.siteId === siteId && s.articleId === articleId);
        if (!row) throw new NotFoundException(`No stock row for article ${articleId} at site ${siteId}`);
        return row; // Nest turns the object into JSON and sets status 200
      }
    }
    
    Terminal
    npm run start:dev    # listens on http://localhost:3000
    
  2. See the request and response with curl -v

    The -v flag prints what curl sends (lines starting with >) and what it receives (<).

    Terminal
    curl -v http://localhost:3000/sites/12/stock/4711
    
    Output
    *   Trying 127.0.0.1:3000...
    * Connected to localhost (127.0.0.1) port 3000        <- TCP done
    > GET /sites/12/stock/4711 HTTP/1.1                    <- request line
    > Host: localhost:3000                                 <- headers
    > User-Agent: curl/8.5.0
    > Accept: */*
    >                                                      <- blank line
    < HTTP/1.1 200 OK                                      <- status line
    < Content-Type: application/json; charset=utf-8
    < Content-Length: 78
    < Connection: keep-alive
    < Keep-Alive: timeout=5                                <- idle connection kept 5 s
    <
    {"siteId":12,"articleId":4711,"qty":42,"updatedAt":"2026-10-06T09:15:02.118Z"}
    

    Output trimmed a little; you will also see X-Powered-By, ETag and Date headers. Try /sites/12/stock/999 to get a 404 with a JSON error body, and /sites/abc/stock/1 to get a 400 from ParseIntPipe.

  3. Send HTTP by hand

    To prove HTTP/1.1 is only text, type the request yourself with nc (netcat). The \r\n pairs are the line endings HTTP requires, and the final empty line ends the headers.

    Terminal
    printf 'GET /sites/12/stock/4711 HTTP/1.1\r\nHost: localhost:3000\r\nConnection: close\r\n\r\n' \
      | nc localhost 3000
    

    You get back the same status line, headers and JSON body. A phone, a browser and curl all send text like this. With HTTPS, the same text travels inside the TLS encryption.

  4. Look up a name with dig

    Replace api.example.com with your own API's domain. +noall +answer shows only the answer. The second column is the TTL in seconds.

    Terminal
    dig +noall +answer api.example.com
    dig +trace api.example.com    # walk the chain yourself: root, .com, name server
    
    Output
    api.example.com.     300     IN      A       203.0.113.10
    

    Run the first command again a minute later. If your resolver cached the answer, the TTL counts down (for example to 241), because the resolver is telling you how long its copy stays valid. dig comes in the dnsutils or bind-utils package on Linux.

  5. Time every step with curl -w

    Save this format file. Each variable is a time in seconds, counted from the start of the request, not from the previous step.

    Output
       dns lookup:  %{time_namelookup}s\n
      tcp connect:  %{time_connect}s\n
    tls handshake:  %{time_appconnect}s\n
       first byte:  %{time_starttransfer}s\n
            total:  %{time_total}s\n
           status:  %{http_code}\n
    
    Terminal
    # save the lines above as curl-timing.txt, then:
    curl -s -o /dev/null -w "@curl-timing.txt" https://api.example.com/health
    
    Output
       dns lookup:  0.152s
      tcp connect:  0.311s
    tls handshake:  0.498s
       first byte:  0.874s
            total:  0.951s
           status:  200
    

    Read it by subtracting each line from the next (these numbers are an example from a slow link):

    • DNS: 152 ms.
    • TCP: 311 − 152 = 159 ms, which is about one round trip.
    • TLS: 498 − 311 = 187 ms, about one more round trip (TLS 1.3).
    • Waiting for the first byte: 874 − 498 = 376 ms. This is one round trip plus server work, so the server took roughly 376 − 160 ≈ 216 ms.
    • Download: 951 − 874 = 77 ms.
  6. Watch connection reuse

    Give curl the same URL twice in one command. It reuses the open connection for the second request.

    Terminal
    curl -s -o /dev/null -o /dev/null -w "@curl-timing.txt" \
      https://api.example.com/health https://api.example.com/health
    

    For the second request, tcp connect and tls handshake show 0.000000s, and the total drops to about one round trip plus server time. That difference is what keep-alive saves on every request after the first.

    Steps 1 to 6 need a terminal on your own machine. The subtraction from step 5, which is where most people slip, is also in the Practice section as a JavaScript exercise you can run in your browser.

Bring it to your team

Ask these when a screen feels slow, before anyone starts optimising the database.

  • "Have we measured this with curl timing or a network trace from a real store or a real phone, not only from the office?"
  • "How many requests does this screen make before it shows anything, and do they run one after another?"
  • "Do we reuse connections, or does each call open a new one?"
  • "How much of the time is the server working, and how much is the network?"
  • "What is the TTL on our API's DNS record, and who changes it before a server move?"
  • "How big is the response for this screen, and is it compressed?"

Red flags

  • A screen makes five or more API calls in a row before it can render.
  • Speed is only tested on office Wi-Fi next to the server.
  • A client library creates a new HTTP client, and so a new connection, for every request.
  • A server move is planned while the DNS TTL is still set to a day or more.

Practice

Turn curl's timings into steps

curl's -w timings are all counted from the start of the request. Write phaseTimes(t), which takes those timings in seconds and returns how long each step took, in whole milliseconds (use Math.round): dns, tcp, tls, wait (from the end of setup to the first byte) and download. One detail: for a plain http:// address there is no TLS, and curl prints 0 for appconnect. Then the TLS step is 0 and the wait starts when TCP finished.

JavaScript exerciseRuns in your browser with tests
// t = { namelookup, connect, appconnect, starttransfer, total } in seconds
function phaseTimes(t) {
  const dns = 0;
  const tcp = 0;
  const tls = 0;
  const wait = 0;
  const download = 0;
  return { dns, tcp, tls, wait, download };
}
Ctrl + Enter runs
Show a solution
JavaScript
function phaseTimes(t) {
  const ms = (seconds) => Math.round(seconds * 1000);
  const setupDone = t.appconnect > 0 ? t.appconnect : t.connect; // no TLS: setup ends after TCP
  return {
    dns: ms(t.namelookup),
    tcp: ms(t.connect - t.namelookup),
    tls: t.appconnect > 0 ? ms(t.appconnect - t.connect) : 0,
    wait: ms(t.starttransfer - setupDone),
    download: ms(t.total - t.starttransfer),
  };
}
Pass the exercise to finish this task.

Plan a faster home screen

The Partner app's home screen takes about 3 seconds in Ravi's corner shop, which runs on mobile data. Here are the facts. One round trip from the shop is about 200 ms. The server works about 100 ms per call. The screen makes 4 calls, one after another. The app's HTTP client opens a new connection for every call (TCP, then TLS 1.3), while DNS stays cached after the first lookup. Count each of DNS, TCP and TLS as one round trip. Work out the current time, then the time after each of two fixes, and say which fix you would do first and how you would prove it helped.

  • You get about 3 seconds today: one DNS lookup, then 4 calls that each pay TCP, TLS, the request and the server's work.
  • You show that reusing one connection removes two round trips from every call after the first.
  • You show that running the calls in parallel, or combining them into one endpoint, removes most of what is left.
  • You do not start with database tuning, because the server's work is only 400 ms of the total.
  • You say how you would measure before and after, for example curl timings or a network trace from a real phone on mobile data.
Show a model answer

Today: DNS 200 ms once, then each call pays TCP 200 + TLS 200 + request 200 + server 100 = 700 ms. 200 + 4 × 700 = 3,000 ms.

Fix 1, reuse the connection: DNS 200 + TCP 200 + TLS 200 once, then 4 × (200 + 100) = 1,200. Total 1,800 ms. Fix 2, parallel or one combined endpoint, on top of fix 1: 600 ms of setup plus one round of 300 ms = about 900 ms.

Do fix 1 first: it is usually a one-line change (share one HTTP client in the app) and cuts 1.2 seconds. Then combine or parallelise the calls. Prove it with the same measurement before and after, taken on a real phone on mobile data, not on office Wi-Fi.

Check yourself

curl shows time_connect 0.30 s and time_appconnect 0.48 s. How long did the TLS handshake take?

Your team moves the API to a new server, but some stores keep reaching the old one for hours. What is the most likely cause?

Why can any of three identical API servers answer a user's next request?

Flashcards

Open my review

Key takeaways

  • A first request goes DNS, TCP, TLS, HTTP request, server work, response. DNS, TCP and TLS each cost about one round trip.
  • DNS answers are cached at many levels for the TTL. Lower the TTL before moving servers.
  • HTTP messages have a first line, headers, an empty line and a body. The status code says what happened.
  • HTTP is stateless. Tokens and cookies carry identity, which lets any server copy answer.
  • On slow networks, round trips cost more than server work. Reuse connections and make fewer calls in a row.

Sources

Episode transcriptWhere two seconds really go · about 13 minutes · tap any line to play from there
  1. NadiaImran, imagine one of our stores when its internet drops to mobile data. A staff member opens the Store app, and the home screen takes about two seconds to appear. Where would you look first?
  2. ImranThe database, probably. Two seconds sounds like a slow query to me.
  3. NadiaThat's where most teams look. By the end of this episode, I think you'll guess differently. In that example, the server is busy for only about six hundred milliseconds of the two seconds.
  4. ImranSo what's happening in the rest of the time?
  5. NadiaWaiting on the network. Let's follow one request from the tap to the screen and see where the time goes.
  6. NadiaThe lesson starts with ordering dinner from a restaurant you've never called. What's the first thing you do?
  7. ImranFind the phone number. I search for the name.
  8. NadiaThat's DNS. Next, you dial and wait for someone to say hello. That's TCP. Then you check it's really the right restaurant, because you're about to give your address. That's TLS.
  9. ImranThen I place the order the way they expect: item, quantity, address. And the food arrives.
  10. NadiaThe order in the agreed format is the HTTP request, and the food is the HTTP response. Every step costs time, and on a bad phone line each one takes longer.
  11. ImranYou said "costs time". How do you measure that?
  12. NadiaIn round trips. A round trip, or RTT, is how long a small message takes to go to the server and for the reply to come back. On office Wi-Fi near a data centre, a few milliseconds. On weak mobile data in a busy store, it can be hundreds.
  13. NadiaIf you can see your screen, the first diagram shows it. Three columns: the phone on the left, DNS in the middle, the server on the right, with time flowing down the page.
  14. NadiaIf your eyes are closed, count the trips with me. One for DNS. One for the TCP handshake. One for the TLS handshake. Only then does the app ask for data, and that's one more.
  15. ImranSo about three round trips before the app sends a single useful byte.
  16. NadiaRight. If a round trip is a hundred and fifty milliseconds, that's roughly four hundred and fifty milliseconds of waiting with nothing to show.
  17. ImranLet's go step by step. DNS first.
  18. NadiaComputers find each other by IP address, a number, not by name. DNS, the Domain Name System, is the phone book that turns a name like api dot example dot com into an address.
  19. NadiaThe second diagram shows the chain, as a column of six boxes. First the app checks its own memory. Then the phone's operating system checks its cache. Then it asks a resolver, usually run by the internet provider.
  20. NadiaIf the resolver has no copy either, it walks the chain itself. The root servers point it to the dot com servers, and those point it to the domain's own name server, which gives the final answer.
  21. ImranThat sounds slow.
  22. NadiaWith every cache empty, it can take tens to hundreds of milliseconds. But each level keeps a copy, so most lookups stop at the app, the phone or the resolver, and a cached one costs almost nothing.
  23. ImranHow long do they keep the copy?
  24. NadiaFor the TTL, the time to live, which the domain owner sets on each record. For example, three hundred seconds. It's a trade-off. A long TTL means fewer lookups. A short TTL means that when you move your API to a new server, clients find the new address sooner.
  25. ImranI've seen that go wrong. We moved a server once, and some stores kept hitting the old one for hours. I assumed the new certificate wasn't working.
  26. NadiaA certificate problem would show errors, not traffic to the old server. The likely cause is a long TTL. Resolvers and phones kept using their cached old address until it expired.
  27. ImranSo how should we have done it?
  28. NadiaLower the TTL a day ahead, make the change, then raise it again.
  29. ImranFine. Steps two and three, TCP and TLS.
  30. NadiaTCP guarantees every byte arrives, in order, or the sender is told it failed. To open a connection, the phone sends a "can we talk?" message called SYN. The server replies yes, with SYN-ACK. The phone confirms with ACK.
  31. ImranThe three-way handshake. About one round trip.
  32. NadiaYes. Then TLS, the S in HTTPS. The server sends its certificate, which proves it really owns that name. The two sides agree on encryption keys, and from then on nobody on the store's Wi-Fi or the mobile network can read or change the data.
  33. NadiaTLS one point three needs one round trip for this. The older TLS one point two needs two.
  34. ImranSo every request pays for all of that?
  35. NadiaNo, and that's the most useful fact in the lesson. A connection can be reused. HTTP one point one keeps connections open by default, which is called keep-alive. HTTP two can send many requests at once over one connection.
  36. NadiaA reused connection skips DNS, TCP and TLS completely. The server decides how long an idle connection stays open. Node's HTTP server, which NestJS uses, keeps it for five seconds by default.
  37. ImranThen the actual request. What's inside it?
  38. NadiaThe third diagram shows a request on top and a response below. Both have the same four parts: a first line, headers, an empty line, then the body.
  39. NadiaThe first line of the request has the method and the path. GET reads, POST creates, PUT or PATCH changes, DELETE removes. The path says which thing, like orders, or the stock of one article at one site.
  40. NadiaHeaders are extra facts, one per line. Host says which website. Authorization says who's asking. Content-Type says how the body is written. Then a blank line, then the body, for example the new order as JSON.
  41. ImranAnd the response starts with a status code.
  42. NadiaYes. Two hundred is OK, two oh one is created, four oh four is not found, and five hundred means the server broke. The lesson on APIs covers which code to use when.
  43. ImranIs a mobile app different from a website here?
  44. NadiaNot in how it uses HTTP. A browser gets HTML, then makes more requests for styles, scripts and images. A mobile app like ours usually gets JSON and draws native screens. It skips the HTML, but pays the same DNS, TCP, TLS and round-trip costs.
  45. ImranOne more thing I've never fully understood. Why does every request carry a token? The server already knows I logged in a minute ago.
  46. NadiaIt doesn't, actually. HTTP is stateless. The server doesn't remember earlier requests. So web apps send a cookie, and mobile apps send a token in the Authorization header, and the server checks it every time.
  47. ImranThat sounds wasteful.
  48. NadiaIt's a gift for scaling. Since no server remembers you, any copy of the server can answer your next request. That's what lets you run three API servers behind a load balancer later in the track.
  49. ImranNow back to the two-second home screen.
  50. NadiaThe lesson's numbers are illustrative, so measure your own. Say one round trip is about a hundred and fifty milliseconds, and the server needs about a hundred and twenty per request. DNS, TCP and TLS cost roughly four hundred and fifty.
  51. NadiaThen the app calls five endpoints, one after another: check login, load profile, load store, load tasks, load notices. Each costs a round trip plus server work, about two hundred and seventy milliseconds. Five of those is one thousand three hundred and fifty.
  52. ImranSo about one point eight seconds in total, and the server worked for only six hundred milliseconds of it.
  53. NadiaExactly. A faster database would hardly help. The fixes follow from the picture. Send the five calls in parallel, or combine them into one home screen endpoint. Make sure connections are reused.
  54. NadiaAnd compress large responses, and cache things that rarely change, like the store details.
  55. ImranThat changes the conversation with my team. I'd have asked them to tune queries.
  56. NadiaAnd the Build-it section shows you how to prove it. First you run a tiny NestJS API that returns the stock of one article as JSON, from a fake list, so you need no database yet.
  57. NadiaThen you call it with curl in verbose mode, and you see the raw request line, the headers, the blank line, and the response with its keep-alive header saying timeout five.
  58. ImranI've watched my engineers do that. What else?
  59. NadiaYou type an HTTP request by hand with netcat, to prove it's only text. You look up a name with dig and watch the TTL count down. Then the important part: you ask curl to print the time of each step.
  60. NadiaHere's the catch. curl measures every time from the start of the request, not from the previous step. So you subtract each line from the next one.
  61. NadiaLet's try one. Take a second. curl says TCP connect finished at point three zero seconds, and the TLS handshake finished at point four eight. How long did TLS take?
  62. ImranPoint four eight seconds. It says so right there.
  63. NadiaThat's the common mistake, and it's in the quiz. Point four eight includes DNS and TCP too. TLS started when TCP finished, so it took point one eight seconds.
  64. ImranRight, subtract.
  65. NadiaAnd that subtraction is the first practice task. You write a small function that turns curl's numbers into steps, and the page checks it for you. The second task plans a faster home screen for a partner shop.
  66. ImranAnd the last Build-it step?
  67. NadiaGive curl the same link twice in one command. For the second request, TCP connect and TLS handshake show zero, and the total drops to about one round trip plus server time. That's what keep-alive saves on every request after the first.
  68. ImranFor my team, then. When a screen feels slow, what do I ask before anyone touches the database?
  69. NadiaHave we measured it from a real store or a real phone, not only from the office? How many requests does this screen make before it shows anything, and do they run one after another? Do we reuse connections?
  70. ImranAnd how much of the time is server work, and how much is network.
  71. NadiaGood. Red flags: a screen making five or more calls in a row before it renders. Speed tested only on office Wi-Fi. And a server move planned while the DNS TTL is still a day or more.
  72. NadiaOne more to think about. A user opens the app, waits three seconds, then taps refresh. Why is the refresh much faster? Take a second.
  73. ImranThe DNS answer is cached, and the connection is still open, because three seconds is inside the five-second keep-alive. So the refresh pays one round trip plus server work.
  74. NadiaExactly. And if they wait ten seconds, the server has likely closed the idle connection, so the app pays for TCP and TLS again. DNS is probably still cached.
  75. ImranMy three takeaways. One: a first request pays for DNS, TCP and TLS, about one round trip each, before it asks for anything.
  76. ImranTwo: DNS answers live for the TTL, so lower it before moving servers. Three: HTTP is stateless, so each request carries its token, and any server copy can answer.
  77. NadiaI'd add one nuance. On slow networks, round trips cost more than server work. So the cheapest speed-up is often fewer calls in a row and reused connections, before anyone touches a query.
  78. NadiaNext episode, we zoom into the network itself: IP addresses, ports, and why a store's connection behaves the way it does.
  79. ImranTalk soon.
Finished reading?Mark it complete to fill in its stop on your course line.

Use it at work

3-tier web app

Clients, a load balancer, app servers and a database.

Open in the System Design Studio
Next stopNetworking you actually need
Was this lesson helpful?
Questions and discussion

Loading the discussion…