Building an Open Source Carefree Android Disk Cache
Article Summary
Instagram's disk cache was causing more crashes than any other component in their Android app. The culprit? A well-intentioned open source library that made error handling a developer nightmare.
Instagram's engineering team rebuilt their Android disk caching system from scratch after DiskLruCache's complex exception handling led to constant NPEs and IOExceptions. Engineer Jimmy Zhang shares how they designed IgDiskCache to be truly "carefree" for developers.
Key Takeaways
- DiskLruCache required NULL checks and manual IOException handling at every cache operation
- Cache related crashes topped Instagram's crash list for over a year
- IgDiskCache uses stub instances so developers never check for NULL
- The new cache handles IOExceptions internally and prevents UI thread blocking
- Dramatically reduced crashes after launch while simplifying code
By rethinking cache design around the principle that caches can always say "I don't have this," Instagram eliminated their top crash category and made disk caching simple.
About This Article
DiskLruCache forced developers to manually handle Editor and OutputStream objects through nested try-catch blocks. When cleanup went wrong, incomplete cached files would corrupt the entire cache.
Instagram built IgDiskCache with a simpler architecture. They removed the Editor and Snapshot layers and added OptionalStream to handle IOExceptions automatically. This meant developers no longer had to manage resources manually.
After IgDiskCache launched in production, Instagram saw cache-related crashes drop significantly. These crashes had been at the top of their crash list for over a year. The built-in thread checking also stopped inefficient disk IO from running on the main thread.