MB·04
Networking on Mobile Networks
Designing app–server communication for networks with high and variable latency, loss and frequent disconnection: what a mobile round trip costs and why round trips dominate, connection reuse, HTTP/2 and HTTP/3 (QUIC), API design for mobile (aggregated endpoints, pagination, partial responses, versioning for old app versions), payload size and compression, caching and conditional requests, timeouts and retries with backoff, connectivity handling, and image and media delivery; with a lab that puts a slow "mobile network" in front of an API and times the same screen loaded four ways at 20, 150 and 600 ms round trips.
Start here: round trips, not bandwidth
On a phone, the network is the slowest and least predictable part of most screens. Bandwidth is often fine; latency isn't: a round trip from a phone in a busy area to a cloud region can take 50–150 ms on 4G, several hundred ms on a congested or weak connection, and the connection drops when the user steps into a lift. What matters is how many round trips a screen needs.
One screen, four ways, three networks
The lab served TiffinBox's "my orders" data (20 orders) through a proxy that adds network delay, and loaded it four ways:
| Loading the 20 orders as… | RTT 20 ms (good Wi-Fi) | RTT 150 ms (typical 4G) | RTT 600 ms (weak signal) |
|---|---|---|---|
| 20 requests, one after another, new connection each | 0.84 s | 6.07 s | 24.1 s |
| 20 requests, one after another, kept-alive connection | 0.44 s | 3.19 s | 12.7 s |
| 20 requests, 6 in parallel | 0.11 s | 0.76 s | 3.0 s |
| 1 request returning all 20 | 0.04 s | 0.30 s | 1.2 s |
The data was identical; only the number of round trips changed, by up to 20×. And the 20 orders were 4,640 bytes of JSON, 264 bytes gzipped: size was never the problem.
How deep to go
Volume 17 defines six mastery levels. For mobile networking:
| Level | What you can do |
|---|---|
| L1 | Explain why latency and round trips dominate mobile performance |
| L2 | Use an HTTP client with keep-alive, timeouts, caching and retries |
| L3 | Explain TCP/TLS/QUIC handshakes, HTTP/2 multiplexing and radio effects |
| L4 | Diagnose slow screens from network traces on real networks |
| L5 | Design mobile-facing APIs, caching and sync for poor networks and old app versions |
| L6 | Run them: monitoring real-user network performance, CDN and edge strategy |
Where the time goes
A request on a new HTTPS connection over TCP needs: DNS (if not cached), TCP handshake (1 RTT), TLS 1.3 handshake (1 RTT), then the request (1 RTT) and the response transfer. That's 3+ round trips before the first byte of data; at 150 ms each, almost half a second. On a kept-alive connection, it's 1 round trip.
Mobile specifics:
- Radio wake-up: an idle cellular radio takes tens to hundreds of milliseconds to reach full power before the first packet (chapter 07).
- Variable latency and loss: congestion, handovers between cells, weak signal; TCP treats loss as congestion and slows down.
- Network changes: moving from Wi-Fi to cellular changes the IP address and breaks TCP connections; QUIC can migrate connections.
- Data costs: many users watch data usage; respect it.
Connections and protocols
- Reuse connections: one HTTP client instance per app (OkHttp, URLSession, Ktor), with keep-alive and connection pooling.
- HTTP/2 multiplexes many requests over one connection, avoiding per-request connections and head-of-line blocking at the HTTP layer (still present at the TCP layer).
- HTTP/3 (QUIC) removes TCP head-of-line blocking, combines transport and TLS handshakes, supports 0-RTT resumption for repeat connections, and migrates connections across network changes: particularly valuable on mobile. Enable it at the CDN and in clients that support it (Cronet on Android; URLSession supports HTTP/3).
- TLS resumption and DNS caching shave round trips on reconnects.
API design for mobile
- Screen-shaped responses: a backend-for-frontend or aggregated endpoint returns what one screen needs in one request (the lab: 20× faster at high latency). GraphQL is another way to let clients ask for exactly what they need.
- Pagination with cursors, and prefetching the next page before the user reaches it.
- Partial responses and field selection to keep payloads small.
- Compression (gzip or Brotli; zstd where supported) for responses; JSON compresses very well (the lab: 4,640 → 264 bytes).
- Binary formats (Protocol Buffers) for strict schemas, faster parsing and smaller uncompressed sizes, especially on high-frequency endpoints; not mainly for size once compression is on.
- Versioning for old clients: evolve additively (new optional fields), never change meanings, keep old endpoints until usage falls below a threshold, and use server-side minimum-version checks only for security or broken builds.
- Idempotency keys on writes (Volume 19), because mobile retries are common.
Caching and conditional requests
- HTTP caching on the client (
Cache-Control,ETag): revalidating a cached menu withIf-None-Matchcosts one round trip but no body (304). - An app-level cache in the local database (chapter 05) serves screens instantly and refreshes in the background: stale-while-revalidate for apps.
- CDN for static and cacheable API responses (menus, images), close to users.
Timeouts, retries and connectivity
- Timeouts: connect (e.g. 10 s on mobile, more generous than servers), read and overall call timeouts, tuned from real-user data.
- Retries: idempotent or keyed requests only; exponential backoff with jitter; small limits; don't retry 4xx except 408/429.
- Connectivity: observe the network state (ConnectivityManager / NWPathMonitor); queue writes while offline and send when connected (WorkManager with a network constraint; background URL sessions); show the user what's pending.
- Don't trust "connected": captive portals and dead networks look connected; treat request failures as the truth.
Images and media
- Serve images resized for the device and slot (a CDN image service with width parameters), in modern formats (WebP, AVIF), with caching; never download a 12-megapixel photo to show a 360-pixel card (chapter 06).
- Lazy-load images below the fold; use placeholders (blurhash, dominant colour) for perceived speed.
- Upload large media in the background with resumable uploads.
Measuring real networks
- Test with network conditioning (Android emulator network profiles, iOS Network Link Conditioner, a delaying proxy like the lab's).
- Collect real-user network timings (OkHttp event listeners, URLSession metrics) by network type and region, with percentiles.
- Watch p95 and p99, not averages: the slow tail is where users give up.
Common traps
- Chatty screens with many small sequential requests.
- A new HTTP client per request (no connection reuse).
- Retrying non-idempotent writes.
- Breaking APIs that old app versions still call.
- Full-size images in lists.
Lab: one screen over three networks
Setup. On the lab machine: an aiohttp API serving 20 orders (each with kitchen, dish, status, ETA, amount, timestamp) either one per request (/orders/{i}) or all at once (/orders), and a user-space "mobile network" proxy in front of it that delays every chunk of data by half the round-trip time in each direction, and delays each new connection by one round trip (the TCP handshake; no TLS). Client: Python aiohttp, measuring the time to fetch all 20 orders. Round-trip times: 20 ms (good Wi-Fi), 150 ms (typical 4G), 600 ms (weak or congested signal).
Results
| Loading pattern | RTT 20 ms | RTT 150 ms | RTT 600 ms |
|---|---|---|---|
| 20 sequential requests, new connection each | 0.84 s | 6.07 s | 24.08 s |
| 20 sequential requests, one kept-alive connection | 0.44 s | 3.19 s | 12.65 s |
| 20 requests over 6 parallel connections | 0.11 s | 0.76 s | 3.01 s |
| 1 request returning all 20 orders | 0.04 s | 0.30 s | 1.20 s |
Payload sizes for the 20 orders:
| Encoding | Bytes |
|---|---|
| JSON | 4,640 |
| JSON, gzip | 264 |
| compact binary (integers and enum codes instead of repeated strings, fixed-width fields) | 420 |
What it shows
- Time is round trips × RTT. New connections cost 2 RTTs per request (handshake + request), kept-alive 1 RTT, and the aggregated request 2 RTTs total. On a weak network that's 24 s versus 1.2 s for the same data.
- Parallelism helps but doesn't replace good API design: 6 connections cut the time by ~4×, but one request was still 2.5× faster, and it wakes the radio once.
- Compression made size irrelevant here. Repetitive JSON compresses superbly; binary encodings are worth it for parsing cost and strict schemas, not because JSON is too big.
- The proxy omits TLS (add 1 RTT per new connection), bandwidth limits and packet loss: real numbers are worse, and the ranking is the same.
Try it yourself
- Add a TLS round trip to the proxy's connection setup and recompute the first row.
- Add 2% packet loss (drop a chunk and resend after 200 ms) and compare kept-alive with parallel connections.
- Serve the aggregated endpoint with Brotli and with zstd and compare sizes.
- Build the same screen against a GraphQL endpoint that returns only the fields shown, and measure size and time.
Revision sheet: networking on mobile networks
Key ideas
- Latency and round trips dominate; mobile RTTs are high and variable; radios wake slowly; networks change and drop.
- Reuse connections; HTTP/2 multiplexing; HTTP/3/QUIC for loss, 0-RTT and migration; TLS resumption, DNS caching.
- Screen-shaped APIs, cursor pagination, prefetching, partial responses, compression, binary formats for parsing and schemas.
- Evolve APIs additively for old app versions; idempotency keys on writes.
- HTTP caching, ETags, app-level caches with background refresh; CDNs.
- Mobile-appropriate timeouts, safe retries with jitter, connectivity-aware queues.
- Images sized for slots in modern formats; real-user network telemetry by percentile.
Numbers to remember
- Lab: 20 requests at 600 ms RTT: 24.1 s (new connections) vs 1.2 s (one request); JSON 4,640 B → 264 B gzipped.
- New HTTPS connection over TCP: ≥ 3 RTTs before data (TCP, TLS 1.3, request).
Glossary
- RTT: round-trip time.
- Keep-alive: reusing a TCP connection for several requests.
- QUIC: UDP-based transport under HTTP/3.
- 0-RTT: sending data in the first flight on resumed QUIC/TLS connections.
- BFF: a backend shaped for a specific client.