Handling Multiple Caches in App
Article Summary
Swiggy's mobile team faced a common problem: juggling disk caches, LRU caches, and databases without creating a maintenance nightmare. Here's their elegant solution.
Ravi Gupta from Swiggy's engineering team shares their architecture for managing multiple caching mechanisms (disk, LRU, database) across different asset types like images, Lottie animations, and videos. The approach keeps each layer independent while making it easy to add new cache types.
Key Takeaways
- Three layer architecture: Utils, Assets Manager, and Caching Layer
- Images use both disk and LRU caching simultaneously
- Animations rely on LRU, videos use custom disk cache
- Protocol driven APIs keep layers mutually exclusive and swappable
- New caching mechanisms plug in without changing existing flow
A clean separation of concerns lets Swiggy mix disk caching, LRU caching, and custom solutions without tight coupling or code changes when adding new cache types.
About This Article
Swiggy's mobile app needed to cache different asset types like images, animations, and videos using different mechanisms at the same time. They wanted to avoid maintenance overhead and keep the layers loosely coupled.
Ravi Gupta's team built a three-layer architecture. The Utils layer handles asset-specific logic. The Assets Manager routes requests based on storage type. The Caching Layer doesn't care what data type it's working with. Protocol-driven APIs connect everything together.
Because the design uses protocols, new caching mechanisms can be added without changing existing code. Swiggy can now manage disk caching for images, LRU caching for animations, and custom disk cache for videos independently. The layers don't need contract changes between them.