Backend Developer · Projects

Project 2: A fast, fresh menu

Marking guide version 1 · Lessons: BE3 lessons 1–4, BE4 lessons 1–5, BE8 lesson 3, BE9 lesson 5, BE10 lesson 3

The problem and who it's for

The kitchen menu is TiffinBox's busiest route: every customer opens it several times a day. In the starter it runs 62 database queries per request, and with just 10 customers at once each answer takes over a second. Kitchens also change prices during the day, and a customer who sees ₹90 and is charged ₹110 loses trust.

Scope

Not included

Before you start

Starter files

Download everything at once: starter/backend-starter.zip.

Milestones

  1. Run npm run test:cache and record the starter's load test (npm run load, with the API started with RATE_LIMIT=1000000).
  2. Rewrite the menu query and check its plan with EXPLAIN ANALYZE (tests 1 to 3).
  3. Add an index migration and run npm run db:migrate.
  4. Add the Redis cache with a time-to-live and single-flight loading (tests 4 to 6).
  5. Clear the key on change and add the Redis-down fallback (tests 7 to 9).
  6. Record the load test again and run all 9 tests three times in a row.

Expected outputs

Acceptance tests

# Input Expected result
1 npm run test:cache 9 of 9 pass, three runs in a row
2 A cold GET /v1/kitchens/7/menu, then GET /_stats At most 3 database queries, answering within 500 ms (test 3)
3 50 requests at once on an empty cache At most 2 menu loads (test 5)
4 Change a dish price with a menu:write token, then request the menu The new price on the very next request (test 7)
5 Stop Redis, then request the menu 200 with the correct menu within 1.5 s; caching resumes when Redis is back (test 9)

Failure and recovery exercises

  1. Remove the line that deletes the cached key after a change and run test 7. Explain what customers would see, then restore it.
  2. Drop your index and run EXPLAIN ANALYZE on the menu query. Compare the plans and timings, then recreate it.
  3. Turn off single-flight loading and run test 5. Explain the stampede in two sentences, then put it back.

Evidence to submit

Marking guide and self-check

Pass rule: every Safety-critical (S) and Mandatory (M) item must be met. A single unmet safety item means the project is not complete, whatever else is done. Quality items are for your own improvement.

Your answers are saved in this browser on this device. Self-checked means you checked your own work against this guide; it is not a reviewer's approval. Never paste passwords, keys, tokens or real customer data here.

Checklist (marking guide version 1)
Evidence

← PreviousA secure orders API
ProLearners Assist