Skip to content
Keshav Raj
A shared cache vault feeding a browser and a phone instantly while refreshing in the background.
ARCHITECT HILLS2 PLATFORMS
SHIPPED Stale-while-revalidate with write-invalidation, one layer for web and mobile.I OWNED DesignerOUTCOME 2 PLATFORMS

Shared API cache

Keshav RajDesigner · 2021–22 · ShippedContact meResume PDF

Context

Between 2021 and 2022 I designed a shared API cache for the learner apps on web and mobile. I implemented it and created both cache files.

The problem

Each Redux bundle hand-rolled its own persistence. Repeat screens refetched the same data, and web and mobile cached differently. A cache inside the shared HTTP layer had to stay correct after writes and not leak stale data between screens.

What I did

  1. In phase one I put a persisted response cache into the shared request function. GET routes match a pattern table with a per-route TTL and a global version key.

  2. On a hit it returns the cached data at once and still refreshes from the network. That is stale-while-revalidate.

  3. In phase two I moved the cache to platform-neutral code so web uses it too, and added a write-invalidation map. Each PATCH, PUT or DELETE route lists the cached lists it must purge.

  4. In phase three I moved storage from AsyncStorage to MMKV, with one store per school, behind the same API.

Key decision

I chose stale-while-revalidate with explicit write-invalidation instead of caching per screen. Screens get instant data, and writes clean up the lists they affect.

The cost is that a learner may briefly see cached data until the refresh lands. I accepted that for speed on repeat screens.

Result

One cache now serves 2 platforms, web and mobile. I deleted about 600 lines of per-bundle persistence across 9 bundles.

What I learned

I would keep stale-while-revalidate with explicit write-invalidation. I would add a request-count measurement, so the gain could be shown.

Stack

  • JavaScript
  • React Native
  • Next.js
  • Redux
  • MMKV
Play the world →