-80% Supabase egress thanks to smart caching
A rude awakening looking at my Supabase dashboard. Egress was climbing much faster than my number of users. Every app opening triggered dozens of requests.
The diagnosis: N+1 and unchanged data
First culprit: ViewStats. 20 tasks displayed = 20 individual requests. Second culprit: data fully reloaded on each navigation, even if nothing had changed.
ContentCacheService
Two-level cache: L1 (in-memory Map, instant access) and L2 (AsyncStorage, persists across restarts). The flow: L1 → L2 → Supabase → store in L1 and L2.
Supabase Realtime
Two channels (content-cache-tasks and content-cache-memos) invalidate the cache when data changes. React components re-render with new data, without additional requests.
Batch ViewStats
getTaskViewStatsBatch(taskIds) retrieves stats for 20 tasks in a single request instead of 20.
The impact
Egress dropped by 80 to 90%. The bonus: the app is noticeably faster. The feed displays instantly from the cache. Optimizing costs and optimizing UX is often the same job.