APIs with Express · Chapter 23 of 29
Performance
By the end of this chapter you can make an Express API send less, compute less and never freeze. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production
- 5 tasks
- Express
- In the all labs plan
The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.
Why you need this
The partner cafés ask for Tapri's menu every ten seconds, though it changes twice a day. Ravi's dashboard keeps asking for a daily report that takes seconds to build. The full menu is 16 KB of JSON going to phones on weak monsoon signal. And whenever Kabir makes a partner a new API key, every other request stalls.
So far every request runs its route in full and sends the whole body, however little has changed, and one slow line holds up everybody.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
GET /menu: a strong ETag and 304 Not Modified
In createApp({ getMenu }), GET /menu answers getMenu() as JSON with a strong ETag: a fingerprint in double quotes, with no W/ in front (the etagFor helper makes one from the JSON text). The same menu always gets the same ETag, and a changed menu gets a new one.
When the If-None-Match request header matches, it answers 304 with an empty body and still sends the ETag. It matches when it is *, the tag itself, the tag with W/ in front, or a comma-separated list holding one of those ('"old-one", <the tag>'). Anything else ('"stale"', or the old tag after the menu changed) gets 200 and the menu.
- Task 2
Cache-Control: public for the menu, no-store for orders
GET /menu sends Cache-Control 'public, max-age=60', on 200 and on 304 answers alike.
GET /orders/:id sends Cache-Control 'no-store' on every answer: 200 with the order (/orders/7) and 404 with { error: 'No such order' } (/orders/99). An order answer has no ETag header at all, not even Express's own weak one.
- Task 3
responseCache: the daily report, kept for 30 seconds
Export responseCache({ ttlMs, now }), which returns a middleware (now defaults to Date.now). It keeps the JSON answers of GET requests by req.originalUrl, query string included (/report/mon?fresh=1 is not /report/mon).
A miss runs the route; when the route answers with res.json and status 200, the body is kept until now() + ttlMs, and the answer gets X-Cache: MISS. A hit, while now() is still before that time, answers the kept body with X-Cache: HIT and status 200 without running the route. With ttlMs 30000, the answer is a hit 29999 ms later and a miss 30000 ms later, which is then kept again.
A non-200 answer is never kept: the same 404 twice runs the route twice. Other methods pass straight on.
In createApp({ now }), GET /reports/daily goes through responseCache({ ttlMs: 30000, now }), so buildReport() runs again only after 30 seconds.
- Task 4
sendJson: gzip big answers for clients that accept it
Export async sendJson(req, res, data, minBytes = 1024), which answers data as JSON (Content-Type application/json) and always adds Accept-Encoding to the Vary header.
When the JSON text is at least minBytes bytes long and the client accepts gzip (Accept-Encoding: gzip, deflate), it sends the JSON gzipped with Content-Encoding: gzip, and the body is less than a quarter of the plain size. Otherwise it sends the plain JSON with no Content-Encoding: for Accept-Encoding identity or br, and for a small body such as { ok: true } even when gzip is accepted.
GET /menu/full sends fullMenu through sendJson.
- Task 5 · interview question
Interview: hashKey, the hash that stalls the API
Fix hashKey(key). It must return a promise that resolves with the PBKDF2 hash of key as hex (salt 'tapri-partners', 210000 rounds, 32 bytes, sha512): 64 hex characters, the same hash as today.
Today it blocks the event loop while it hashes, so a GET /menu sent while POST /partners/kulhad/key is hashing waits for the key. After the fix the menu answers first. POST /partners/:id/key still answers 201 with { partner, key } (key: 32 hex characters) and stores the hash in app.locals.partnerKeys under the partner's id.
What you will learn
- ETags and 304: "you already have this", answered with no body.
- Cache-Control: who may keep a copy, and for how long.
- A cache with a time to live: remember an expensive answer for a while.
- Gzip and the event loop: send fewer bytes, and keep slow work off the one thread that serves everybody.
More in Production
Build "Performance" yourself
Partner cafés want Tapri’s menu online by Friday: you build its API. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.