Context
In 2022 I co-designed how mock-test questions are delivered through a CDN, and implemented the read path on both client and server. A teammate did the asynchronous evaluation half.
The problem
A mock test starts for many learners at once. Every learner pulled the full question set from the origin, and submit was evaluated synchronously. A per-learner, authenticated response cannot be cached at a CDN as it is.
What I did
I created new learner quiz APIs on the backend and served questions through the CDN. The CDN does not forward the standard Authorization header, so I added a custom header on client and server.
I made the questions response learner-independent: I removed the per-attempt ID from the request and keyed the cache on the quiz's last-updated timestamp.
Randomized papers come from server-side sets, so they stay cacheable.
I added invalidation: a worker and question hooks bump the quiz timestamp whenever questions change.
Key decision
I cached the read path at the CDN with version-key invalidation. Evaluation became asynchronous, which was the team's half.
The trade-off was a custom auth header and a cache key that must be bumped on every question edit. If that bump is missed, learners see old questions, so I put it in hooks and a worker rather than rely on people.
Result
Question responses are now learner-free and cacheable at the CDN.
What I learned
I would do version-key invalidation again. I would add a metric on cache hit rate at launch, so the gain could be shown.
Stack
- Ruby on Rails
- CloudFront
- React Native
- Next.js
