nvx/apertureAll write-ups
No. 08ENGINEERING · 7 min

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 Cyber 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

SequenceOne refresh, before the fix
App
BackendHolds a stored copy
QueueThrottled per student
Assessments service
LMS
  1. 01Reads the stored assessments
  2. 02Background refresh starts
  3. 03Look up the units you're enrolled in
  4. 04One call per unit
  5. 05Asks the LMS
  6. 06404
  7. 07Treats it as "no assessments" and moves on
  1. 01AppBackendReads the stored assessments
  2. 02QueueBackendBackground refresh starts
  3. 03BackendLook up the units you're enrolled in
  4. 04BackendAssessments serviceOne call per unit
  5. 05Assessments serviceLMSAsks the LMS
  6. 06Assessments serviceBackend404
  7. 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.

The two ways to get a 404
Course not set up yetStudent not in the course yet
What it meansThe unit offering has no assessments in the LMS yet, usually because it hasn't startedThe student's enrolment hasn't synced across to the LMS course yet
Who it affectsEvery student in that offeringJust that one student
Fixes itself soon?Not until the course is set upUsually, once the sync catches up
Share of the 404s
  • 75%Course not set upAbout three quarters
  • 25%Student not in the courseThe rest
Rounded: the split is about three quarters to a quarter.

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

Days from today until the unit starts
  1. 1Exactly 42 days42 daysStill asked
  2. 243 days43 daysSkipped, and everything further out
  3. Asked0 days to 42 days
  4. Skipped42 days to 84 days
Past six weeks out, there's almost never anything set up for the unit in the LMS yet.
Rule 1 in the odd cases
CaseWhat the backend does
A unit is skippedIt returns the same empty list a 404 would have
The start date is missing or doesn't parseIt asks anyway, because skipping is only an optimisation and should never be why an assessment goes missing
Which date counts as todayToday 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

What gets remembered for 24 hours in Firestore
The 404 saysSaved againstWhy
Course not set upThe offeringOne student's refresh finds out, and the whole class gets the answer for a day
Student not in the courseThat offering and that studentYour sync lag shouldn't hide a classmate's assessments
This applies to units still worth asking about. All the memories a refresh needs are checked in one batched read.
One refresh, after the fix6 stages
  1. 01

    Refresh starts

    From the queue

  2. 02

    Enrolled units

    From the student record

  3. 03

    Skip far-off units

    Starts more than 42 days out

  4. 04

    Check the 404 memory

    One batched read

  5. 05

    Call only the rest

    The assessments service

  6. 06

    Remember new 404s

    For 24 hours

The two new stages sit in front of the fan-out, so a skipped or remembered unit never turns into a call.
SequenceOne unit, two students, one call
BackendRefreshing
404 memoryFirestore
Assessments service
  1. 01Student 1: any no on file for unit C?
  2. 02Nothing
  3. 03Assessments for unit C?
  4. 04404: course not set up
  5. 05Remember for 24 h, whole offering
  6. 06Student 2: any no on file for unit C?
  7. 07Yes, still fresh
  8. 08Skip the call, return an empty list
  1. 01Backend404 memoryStudent 1: any no on file for unit C?
  2. 02404 memoryBackendNothing
  3. 03BackendAssessments serviceAssessments for unit C?
  4. 04Assessments serviceBackend404: course not set up
  5. 05Backend404 memoryRemember for 24 h, whole offering
  6. 06Backend404 memoryStudent 2: any no on file for unit C?
  7. 07404 memoryBackendYes, still fresh
  8. 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.

The refresh loop, heavily simplified (not the real code)

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

What the backend does when something goes wrong
SituationWhat happens
Both rules, by defaultOff. 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 numberThe rule is off
Firestore hiccups on the readThe backend logs a warning and calls the service like it always did
A 400It still throws, so the queue retries it
A successful responseNever cached
What the app getsThe exact same empty list it always got
Every refreshOne 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

The week of testing: things that fought back8 events
  1. 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.

  2. Tue

    Bound the emulator to IPv6

    Ugly, but it works, since localhost resolves to IPv6 first.

  3. 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.

  4. Wed

    Narrowed the mock

    It now only fails reads from the 404 memory.

  5. 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.

  6. Thu

    The _undefined bug

    Caught before merge. The walkthrough is below.

  7. Fri

    The TEST environment fought back three times

    The table below has all three.

  8. 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.

The _undefined bug5 steps
  1. 01The admin console looks students up without the account identity the app sends. Internal admin console: no account identity.
  2. 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.
  3. 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.
  4. 04key to b: A's no. Student B's lookup reads A's no, and B's assessments are hidden. Student B: assessments hidden.
  5. 05The fix is no identity, no per-student memory. Student B: cleared; Per-student memory key: nothing written.
The TEST environment fought back three times
#What went wrongWhat I did
1The admin console had saved a stub setting that made every unit return the same answerDeleted it by hand
2The stub wasn't even being called, because TEST's default data source pointed at the QA serviceChanged one config field, with the backend lead's OK
3My own validation blocked me. The Firestore console's "add field" defaults to string, so 42 was saved as "42", and my code only accepts numbersNot 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.

The fake student on TEST
UnitSetupWhat should happen
AStarts in 43 daysRule 1 skips it
BStarts in 42 daysIt gets asked and returns one assessment
CRunning; the stub says the course isn't set upAsked, 404, remembered
DRunning; the stub says the student isn't in the courseAsked, 404
Two refreshes of the fake student5 lines
# 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.
Calls per refresh, fake student on TEST
  • Units enrolled
  • Calls made
01234Refresh 1Refresh 24432A skippedC from memory
Measured on TEST with four fake units, before the rules went live.
Calls per refresh, fake student on TEST
LabelUnits enrolledCalls made
Refresh 143
Refresh 242

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.

For example, one unit that isn't set up in the LMS yet, with a few hundred students refreshing a few times a day
BeforeAfter
Calls for that unitOne doomed call per student per refresh, and hundreds of identical 404s in the logsOne call, then nothing for 24 hours
If the unit starts in two monthsAsked like any otherNobody asks at all
Who gains
WhoWhat changes
The owning team and the LMS behind itThey no longer answer questions they've already answered
Our background refreshesThey do less pointless work
Whoever's on callNo more wading through thousands of identical warnings, so new problems stand out
Future meThe summary line turns a number like 39% from needing a custom Python tool into one log query
The studentSees what they saw before
How it shipped3 stages
  1. 01

    Reviewed and approved

  2. 02

    Merged with both rules off

  3. 03

    Both rules switched on

    In production

No comments yet

Comments are read before they appear.