Why 39% of our API calls came back 404
39% of the assessment calls behind the uni app came back 404, on every refresh. A six-week rule and a 24-hour memory taught the backend to stop asking, and both are now on in production.
By Ojas KekreCyber security, Monash University
Open the uni app and check what's due. Behind that screen, the backend asks another system what assessments you have, once for every unit you're enrolled in. 39% of those calls came back 404, on every refresh: same student, same unit, same answer.
- Assessment calls that came back 404
- 39%
- How far ahead the fix looks
- 6 weeks
- How long the fix remembers a no
- 24 h
How assessments get to your phone
- 01Reads the stored assessments
- 02Background refresh starts
- 03Look up the units you're enrolled in
- 04One call per unit
- 05Asks the LMS
- 06404
- 07Treats it as "no assessments" and moves on
- 01AppBackendReads the stored assessments
- 02QueueBackendBackground refresh starts
- 03BackendLook up the units you're enrolled in
- 04BackendAssessments serviceOne call per unit
- 05Assessments serviceLMSAsks the LMS
- 06Assessments serviceBackend404
- 07BackendTreats it as "no assessments" and moves on
My first mental model was wrong. I assumed the backend called the assessments service every time someone opened the app. It doesn't. The refresh runs in the background, through a queue, and is throttled so no single student can trigger it too often.
That is fine for one call. It is much less fine when the same doomed call goes out for thousands of students, all day.
Two kinds of 404
I pulled the raw 404 bodies out of the logs, and they came in two kinds. They don't even agree on the type of their error code: one sends a number, the other a string, from the same service with the same status code. So the code ignores that field and reads the human-readable message, which is at least consistent.
| Course not set up yet | Student not in the course yet | |
|---|---|---|
| What it means | The unit offering has no assessments in the LMS yet, usually because it hasn't started | The student's enrolment hasn't synced across to the LMS course yet |
| Who it affects | Every student in that offering | Just that one student |
| Fixes itself soon? | Not until the course is set up | Usually, once the sync catches up |
- 75%Course not set upAbout three quarters
- 25%Student not in the courseThe rest
Two cheap rules
The fancier options, like caching inside the assessments service, meant a lot of work in a system that isn't ours. So the owning team and I settled on two rules that live entirely in our backend and cost almost nothing to run.
Rule 1: don't ask about units that are ages away
- 1Exactly 42 days42 daysStill asked
- 243 days43 daysSkipped, and everything further out
- Asked0 days to 42 days
- Skipped42 days to 84 days
| Case | What the backend does |
|---|---|
| A unit is skipped | It returns the same empty list a 404 would have |
| The start date is missing or doesn't parse | It asks anyway, because skipping is only an optimisation and should never be why an assessment goes missing |
| Which date counts as today | Today is worked out as a Melbourne calendar date first, then compared date with date. No off-by-one |
The trap is that the server runs on UTC and the students are in Melbourne. For 10 to 11 hours a day the UTC date is still yesterday there.
Rule 2: remember the no
| The 404 says | Saved against | Why |
|---|---|---|
| Course not set up | The offering | One student's refresh finds out, and the whole class gets the answer for a day |
| Student not in the course | That offering and that student | Your sync lag shouldn't hide a classmate's assessments |
- 01
Refresh starts
From the queue
- 02
Enrolled units
From the student record
- 03
Skip far-off units
Starts more than 42 days out
- 04
Check the 404 memory
One batched read
- 05
Call only the rest
The assessments service
- 06
Remember new 404s
For 24 hours
- 01Student 1: any no on file for unit C?
- 02Nothing
- 03Assessments for unit C?
- 04404: course not set up
- 05Remember for 24 h, whole offering
- 06Student 2: any no on file for unit C?
- 07Yes, still fresh
- 08Skip the call, return an empty list
- 01Backend404 memoryStudent 1: any no on file for unit C?
- 02404 memoryBackendNothing
- 03BackendAssessments serviceAssessments for unit C?
- 04Assessments serviceBackend404: course not set up
- 05Backend404 memoryRemember for 24 h, whole offering
- 06Backend404 memoryStudent 2: any no on file for unit C?
- 07404 memoryBackendYes, still fresh
- 08BackendSkip the call, return an empty list
The first student's refresh pays for the 404. Every other refresh for that offering gets the answer from memory for the next 24 hours.
Far-off units are skipped, remembered nos are skipped, and any new 404 is remembered for a day.
typescriptconst units = enrolled.filter(u => !startsMoreThan(u.startDate, 42))
const remembered = await readFreshNos(units) // one batched read
for (const unit of units.filter(u => !remembered.has(u))) {
const res = await askAssessmentsService(unit)
if (res.status === 404) {
const scope = res.reason === 'course not set up' ? 'offering' : 'student'
await rememberNo(unit, scope, { hours: 24 })
}
}Rule 0: be boring when things break
| Situation | What happens |
|---|---|
| Both rules, by default | Off. Each switches on with a single number in the backend's config, so the change can ship dark and be flipped later |
| The number is missing, zero, negative or not a number | The rule is off |
| Firestore hiccups on the read | The backend logs a warning and calls the service like it always did |
A 400 | It still throws, so the queue retries it |
| A successful response | Never cached |
| What the app gets | The exact same empty list it always got |
| Every refresh | One new summary line: counts only, no student identifiers. Units, skipped, answered from memory, calls made, and 404s of each kind. It logs even with both rules off, so there's a baseline before anything gets flipped |
Testing it
- Tue
Port 8080 was taken by... VMware?
The tests expect the Firestore emulator on localhost:8080. A VMware network service on my machine held it over IPv4.
- Tue
Bound the emulator to IPv6
Ugly, but it works, since
localhostresolves to IPv6 first. - Wed
Faking a Firestore outage broke config
A plain single-document read uses the same batched read underneath, so my mocked outage took the config read down too.
- Wed
Narrowed the mock
It now only fails reads from the 404 memory.
- Wed
Breaking my own code on purpose
For every important rule I planted a bug (> to >=, offering-wide to per-student, deleting a try/catch) and made sure a test went red. A test that can't catch the bug it's named after is decoration.
- Thu
The _undefined bug
Caught before merge. The walkthrough is below.
- Fri
The TEST environment fought back three times
The table below has all three.
- Fri
Green on TEST
Two refreshes, both matching the prediction to the number. Before the fix deployed, I watched the old build reproduce the _undefined bug live.
- 01The admin console looks students up without the account identity the app sends. Internal admin console: no account identity.
- 02My per-student memory key was built with a template string, so the missing identity became the literal text
undefined. Per-student memory key: identity: undefined. - 03con to key: A's no. Every admin lookup now shares that one key. Student A's "not in the course" is stored under it. Student A: not in the course.
- 04key to b: A's no. Student B's lookup reads A's no, and B's assessments are hidden. Student B: assessments hidden.
- 05The fix is no identity, no per-student memory. Student B: cleared; Per-student memory key: nothing written.
| # | What went wrong | What I did |
|---|---|---|
| 1 | The admin console had saved a stub setting that made every unit return the same answer | Deleted it by hand |
| 2 | The stub wasn't even being called, because TEST's default data source pointed at the QA service | Changed one config field, with the backend lead's OK |
| 3 | My own validation blocked me. The Firestore console's "add field" defaults to string, so 42 was saved as "42", and my code only accepts numbers | Not a bug. Off-by-default did what I told it to |
For the evidence I made a fake student on TEST, invented from scratch rather than copied from anyone. It has four made-up units, each built to hit a different branch. I refreshed twice, two seconds apart.
| Unit | Setup | What should happen |
|---|---|---|
| A | Starts in 43 days | Rule 1 skips it |
| B | Starts in 42 days | It gets asked and returns one assessment |
| C | Running; the stub says the course isn't set up | Asked, 404, remembered |
| D | Running; the stub says the student isn't in the course | Asked, 404 |
# Refresh 1: A skipped, B, C and D asked, C and D both 404, C's no remembered assessments summary { units: 4, skippedFuture: 1, cachedNotFound: 0, called: 3, notFoundOffering: 1, notFoundStudent: 1 } # Refresh 2: A skipped, C answered from memory, only B and D asked assessments summary { units: 4, skippedFuture: 1, cachedNotFound: 1, called: 2, notFoundOffering: 0, notFoundStudent: 1 } # D is asked again on purpose: the admin console sends no account identity, so after the _undefined fix only offering-wide nos are remembered on this path. In the app, D's no would be remembered too.
- Units enrolled
- Calls made
| Label | Units enrolled | Calls made |
|---|---|---|
| Refresh 1 | 4 | 3 |
| Refresh 2 | 4 | 2 |
The result
Both rules are on in production now, and the doomed calls have stopped repeating. A unit that starts more than six weeks out isn't asked about at all, and a unit that isn't set up yet gets one call and a 24-hour memory instead of a 404 on every refresh.
| Before | After | |
|---|---|---|
| Calls for that unit | One doomed call per student per refresh, and hundreds of identical 404s in the logs | One call, then nothing for 24 hours |
| If the unit starts in two months | Asked like any other | Nobody asks at all |
| Who | What changes |
|---|---|
| The owning team and the LMS behind it | They no longer answer questions they've already answered |
| Our background refreshes | They do less pointless work |
| Whoever's on call | No more wading through thousands of identical warnings, so new problems stand out |
| Future me | The summary line turns a number like 39% from needing a custom Python tool into one log query |
| The student | Sees what they saw before |
- 01
Reviewed and approved
- 02
Merged with both rules off
- 03
Both rules switched on
In production