yelgabo

October 2026 · Next.js, Postgres, Railway

How I Made My Pages Load 5x Faster

Before announcing job-fraud publicly, I set a target of 1,000 requests a second and tested toward it. The cached pages held at the limit of my test machine. The uncached pages exposed a Postgres bottleneck, which two small changes cut from a 3.1-second 90th percentile to 587 ms.

470/s

cached pages, 0 errors; my home connection was the limit

2.5%

of one CPU core used by the app server during that run

3.1 s → 587 ms

p90 for uncached pages at 100/s

4 → 0.7

CPU cores Postgres used in the uncached test, before and after the first fix

The site and the goal

job-fraud reviews job postings from WorkBC, British Columbia's public job board, and rates each one Low, Medium or High on whether it looks like a genuine attempt to hire locally. It holds about 23,000 rated postings. The front end is Next.js 15 (App Router), the data lives in Postgres through Prisma, and everything runs on Railway, a hosting platform. Railway puts a CDN in front of each web service; I call it "the edge" below, and the Node server behind it "the origin".

The CPU figures in this post come from Railway's per-service metrics, which report CPU in vCPUs. One vCPU is one CPU core's worth of time, so 0.5 means half a core was busy on average.

A public launch sends a burst of people to the same few pages within minutes. I set the target at 1,000 requests a second on the public pages and treated it as a requirement. I didn't plan for the average traffic I expected afterwards.

The site had one constraint that shaped everything below. Railway's build step has no network route to the private database, so a page that queries Postgres cannot be rendered at build time. The list pages are therefore rendered on every request (force-dynamic) and keep their data in Next's data cache (unstable_cache, 10 minutes). That data cache protected Postgres, but the pages themselves were never cached at the edge.

1Let the edge cache the pages

Railway's edge caches whatever the origin marks cacheable. Next marks every dynamically rendered page private, so the edge passed every homepage request through to the Node server. Three changes fixed that.

Mark the list pages cacheable

A small middleware sets Cache-Control: s-maxage=600, stale-while-revalidate on / and /companies. The edge then serves those pages for 10 minutes and refreshes them in the background. Visitors never wait for that refresh.

Make URLs canonical

The edge's cache key includes the query string. A link shared on social media arrives as /?fbclid=…, a unique URL per click that would never hit the cache. The middleware rebuilds each URL in one fixed form, keeping only the parameters the page understands, in a fixed order and without defaults. It answers anything else with a 308 redirect to that canonical form. Every click from a share then lands on one of a few cached URLs.

// middleware.ts (trimmed)
const canonical = canonicalUrl(url.pathname, url.searchParams)
if (canonical !== url.pathname + url.search) return NextResponse.redirect(target, 308)
const res = NextResponse.next()
if (EDGE_CACHED_PAGES.has(url.pathname)) res.headers.set("Cache-Control", "s-maxage=600, stale-while-revalidate=31535400")

Stop prefetching the list

Next's <Link> prefetches every link in view. Each prefetch is a separate request for the page's React Server Components data (an ?_rsc= request), and I confirmed that Railway's edge never caches those. With dozens of filter tabs and pager links on the page, one homepage view cost about 30 renders on the server. The list pages now use plain <a> tags. A click loads a full page, which the edge serves from its cache.

Visitorany URL, including tracking parameters
Middleware308 to the canonical URL
Edgeserves the cached page, about 90 ms
Originrenders only on a miss, about every 10 min per URL

2Load test

I used oha, a command-line load generator, against the live site, ramping up from 100 requests a second. Each request downloaded a full page, about 29 KB.

oha -z 60s -q 1000 -c 300 -H "Accept-Encoding: gzip, br" \
  "https://job-fraud-production.up.railway.app/?band=high"
TestRateResponsesMedianp95
Cached page100/s, 30 s2,992 × 20094 ms329 ms
Cached page, aiming for 1,000/s470/s reached, 60 s27,889 × 200523 ms1.4 s
Tracking-parameter redirect50/s, 20 s996 × 30891 ms583 ms

The 1,000-a-second run reached 470 a second with no errors. The limit was my home connection: the test pulled 13.6 MB a second, the same speed as a plain download over that line. For that minute, Railway's metrics show the app server using 0.025 vCPU (2.5% of one core) and sending about 1 MB, because the edge served nearly all of the 777 MB from its cache.

The site's ceiling for cached pages is somewhere above 470 a second, and I haven't measured where. Reaching 1,000 a second takes a test machine with a faster link.

The redirects are cheap, but they always reach the server. The edge marks them uncacheable, and each click carries a different fbclid anyway, so a cache wouldn't help. They are the one kind of launch traffic the edge doesn't absorb.

3The cache-miss path

Cached pages only cover the URLs people have already visited. After a deploy, the edge cache is empty, and every combination of filter and page is its own URL. The list can be filtered by risk band, job category and posting date, and shows 50 postings a page. I generated 1,080 URLs that nobody had requested yet, each a real combination of those filters at page 41 or later, and sent 100 requests a second at them for 10 seconds.

Every request succeeded, but the median was 310 ms, the 90th percentile 3.1 seconds and the 99th 3.9 seconds. Postgres was using about 4 vCPU and the app servers about 0.7, so the database was the bottleneck.

Why

Each list page shows a count next to every filter tab: High 203, Medium 1,310 and so on, for each band, category and date window. Filling them in takes about a dozen count and group-by queries. The data cache was keyed on the filters and the page number, so page 41 of "High" recomputed exactly the same counts as page 1. A burst of deep-page misses ran the full set of aggregate queries every time.

The fix

I split the loader into two caches: the tab counts, keyed on band, category and date window only, and the rows, keyed on the page as well. A miss on a new page of an existing filter now runs one row query.

const loadCounts = unstable_cache(loadCountsUncached, ["postings-counts"], { revalidate: 600 })  // (band, cat, posted)
const loadRows   = unstable_cache(loadRowsUncached,   ["postings-rows"],   { revalidate: 600 })  // (band, cat, posted, page)

I re-ran the same test against the same 1,080 URLs after a deploy had emptied both caches. It gave a 208 ms median, an 829 ms 90th percentile and a 1.4-second 99th, and Postgres dropped from about 4 vCPU to about 0.7.

4Indexes

Next I ran EXPLAIN ANALYZE on the queries a page runs. The table is small, but the rows are wide because each one stores the full posting text, so any query that reads the whole table pays for it.

QueryBeforePlan before
Unfiltered list, page 4134 msread all 18,778 rated rows, then sort
Whole-corpus counts27–33 msread the whole table
One date window's counts5 mspostedDate index
"Medium" list, page 414 ms[riskBand, fraudScore] index

The unfiltered list is the homepage's default view, and its sort order (score, then title, then ID) had no matching index. I added one with the same three columns in the same order. Page 41 at 50 rows a page needs rows 2,001 to 2,050, so Postgres now reads the first 2,050 index entries in order and stops. Page 41 dropped from 34 ms to 11 ms. The same index now serves "last 7 days, sorted by score": Postgres walks the score order and skips postings outside the window, in 16 ms.

The second index covers scoredAt (when a posting was rated, empty until then) and riskBand (its rating). It was meant to let the whole-corpus counts use an index-only scan. Postgres still chooses a full table scan for them, most likely because the table hasn't been vacuumed since I added the index. An index-only scan can skip the table only for pages the visibility map marks all-visible, and only a vacuum sets those bits. With the counts cached per filter, those queries rarely run now.

Uncached pages at 100 requests a second

Same 1,080 cold URLs, 10 seconds each run, all requests successful. Lower is better.

90th percentile

Before3.1 s
Counts cached829 ms
+ indexes587 ms

99th percentile

Before3.9 s
Counts cached1.4 s
+ indexes1.1 s

Median

Before310 ms
Counts cached208 ms
+ indexes219 ms

The two fixes add up. Caching the counts cut the database work on a miss, and the indexes made the remaining work cheaper. The median barely moved between the last two runs. The tail is where requests had been queueing behind a saturated database, and the tail is what improved.

What surprised me

A cache that says HIT can still be wrong

After a batch job added 4,243 postings, every page returned x-cache: HIT, but one filter page showed the old counts for 50 minutes. Two caches stacked up to cause it. The site ran two app replicas, and each keeps its own Next data cache. Once an entry is older than 10 minutes, Next serves it stale to the next request and refreshes it in the background. The edge stored that stale page. When the edge later refreshed its copy, the request could land on the other replica, whose entry hadn't been refreshed yet, so the edge stored stale counts again. The fix was procedural. I now send a few requests the edge never caches (React Server Components requests) so both replicas refresh, then purge the edge and request each page once to refill it. I now check the numbers on each page against the database after a data change, not only the cache header.

Docs commits were emptying the cache

Every deploy purges the edge's HTML, and every push deployed, including commits that only changed documentation. Railway lets a service list "watch paths", file patterns a push must touch to trigger a deploy, so I set them to skip pushes that only change docs. My first pattern started with /**, which matched nothing, so it skipped the next code deploy too. Railway's examples start with **. Checking that a docs-only push is skipped proved only half of it; the deploy status of the next code push showed the other half.

Load tests are cheap

Railway bills CPU and memory by the minute and outbound traffic at $0.05 per GB, with no per-request charge. The whole session of tests used well under a dollar. Running a second replica for the launch costs more than any of the tests.

Why not Redis

A shared Redis cache would let both replicas use one data cache and keep it across deploys. Today each replica computes a filter's counts once per 10 minutes, so sharing saves at most half of that, and the edge absorbs most traffic before it reaches either replica. It would add a service to pay for and a custom cache handler for Next. It becomes worth it with more replicas, or if deploys during busy periods keep emptying the cache. Neither applies yet.

Method and caveats

  • All tests ran from one machine on a home connection, through one edge location. Real visitors spread across several, which is easier on the system.
  • The uncached test used pages 41 and later, because the earlier pages were already cached. Deep pages cost more in Postgres than the pages most visitors open, so this test is harsher than real traffic.
  • Railway samples CPU every 30 seconds and these tests lasted 10, so the CPU numbers show the scale of the change, not a precise figure. The latency numbers come from the load generator and are exact for each run.
  • "Aborted due to deadline" in oha's output means requests still in flight when the timer ended. They are not errors.
  • The site ran on two replicas during every test.

The code is public at github.com/yelgabo/job-fraud. The launch runbook with every measurement is docs/launch-runbook.md.