# We made our dashboard 35% faster without making any query faster

Our API (the server code that hands the dashboard its data) answered in 40 ms. Our dashboard still took over a second to open. The difference was spent crossing the Atlantic, and four small changes won a third of it back.

## Where the second went

Our servers are in Frankfurt and we're in Halifax. A round trip between them, a message there plus the reply back, takes about 140 ms, and no code can make light go faster. That fixed wait is called *latency*, and you pay it on every crossing, however small the request.

So the useful question isn't "how fast is each request?" but "how many times does the page cross the ocean?" Every request that waits for another one to finish pays the 140 ms again. Shaving 20 ms off a query saves 20 ms. Removing one crossing saves 140.

*Figure: Two request waterfalls. Before: connect, HTML, a 311 KB bundle taking five round trips, then the Site's details, then stats, ten round trips in all. After: the bundle gzipped to 97 KB takes three, and the Site's details and stats start together, seven round trips in all. Counted in round trips: compression took two off the bundle, and starting the stats alongside the Site's details took one more.*

> Light in glass fibre travels at about two thirds of its top speed, along whatever route somebody was allowed to dig. It has no intention of hurrying for a dashboard.

## Measure with distance, not on localhost

On localhost, where browser and server sit on the same machine, a round trip costs nothing, so a staircase of requests looks fine. Add some distance:

- **Chrome DevTools:** in the Network tab's throttling menu, choose *Add…* and make a profile with 140 ms of latency and no bandwidth limit. You're testing distance, not a slow connection.
- **Linux:** `sudo tc qdisc add dev lo root netem delay 70ms` adds 70 ms each way to traffic on your own machine, using [netem](https://man7.org/linux/man-pages/man8/tc-netem.8.html), Linux's built-in network delay simulator. Undo it with `sudo tc qdisc del dev lo root`, or spend tomorrow wondering why everything is slow.

Then look for staircases in the waterfall, DevTools' chart that draws every request as a bar along a timeline: requests that only start once another finishes. We found four.

## 1. Compress the bundle

Our 311 KB JavaScript bundle (all the dashboard's code, packed into one file) went out uncompressed. Gzipped, it's 97 KB. A new connection starts cautiously: it sends about 14 KB, waits to hear it arrived, then sends twice as much, and so on ([TCP slow start](https://en.wikipedia.org/wiki/TCP_congestion_control#Slow_start)). Every wait is a round trip, so 311 KB takes five and 97 KB takes three. That's two fewer round trips, not just fewer bytes.

The files are embedded in our Go binary (the server is a single program file, assets included) and never change, so we compress them once at startup and serve those bytes to any browser that accepts gzip, which browsers announce in an `Accept-Encoding` header. Simplified:

```pseudo
// Once, at startup
gzipped = {}
for each file in embedded_assets:
    gzipped[file] = gzip(file, best_compression)

// On every request for an asset
if request accepts "gzip":
    respond with gzipped[file] and "Content-Encoding: gzip"
else:
    respond with file
```

Behind a [CDN](https://developer.mozilla.org/en-US/docs/Glossary/CDN), a network of servers that keeps copies of your files near your visitors? It's supposed to do this for you. Check that it does: `curl -sI -H 'Accept-Encoding: gzip' https://your-site/app.js` fetches just the headers, saying it accepts gzip like a browser would. They should include `content-encoding: gzip`.

## 2. Start independent requests together

The page fetched the Site's details, rendered, and only then did the dashboard ask for stats. But the stats only needed the Site's ID, which had been in the URL all along. Now both start at once:

```pseudo
// Before: the stats wait for the Site
site = await fetch("/api/sites/" + site_id)
show dashboard(site)                        // which only now asks for its stats

// After: both start at once
site = fetch("/api/sites/" + site_id)
prefetch_dashboard_stats(site_id)           // the ID was in the URL all along
```

When the dashboard mounts (appears on the page), its requests are already on their way, and it waits for those instead of sending duplicates. A parent component fetches its data, draws a child component inside itself, then the child fetches its own: it's the most common waterfall in [single-page apps](https://developer.mozilla.org/en-US/docs/Glossary/SPA), where JavaScript swaps the content instead of loading new pages. If the child doesn't need the parent's answer, don't make it wait for one.

## 3. Don't ask again for what you just saw

Switching from "Last 30 days" to "Last 7 days" and back refetched everything. Now a view under a minute old shows instantly with no request, and an older one shows at once while a fresh copy loads in the background. That's [The 30-line cache that made our dashboard feel instant](https://foresite.dev/blog/stale-while-revalidate-without-a-library).

## 4. One query, not one per Goal

A Goal counts conversions, like sign-ups. The Goals panel queried Postgres, our database, once per Goal, one after another, and each query was another round trip. Now it's a single query: [8 database trips down to 1](https://foresite.dev/blog/n-plus-one-union-all).

## The result

At 140 ms of latency, the demo dashboard's cold load (a first visit, nothing cached) went from 1.07 s to 0.70 s, and going back to a view you've already seen became instant. No query got faster. The page just stopped standing around waiting.

[Open the demo](https://app.foresite.dev/demo), open DevTools (F12 in most browsers), and watch the waterfall.
