Time-Aware LRU: How Grab Boosts Android Image Caching Efficiency

Time-Aware LRU: How Grab Boosts Android Image Caching Efficiency

Time-Aware LRU: How Grab Boosts Android Image Caching Efficiency

Image caching is a critical component of mobile app performance, especially for apps like Grab that handle high volumes of image requests. Recently, Grab engineers shared how they optimized their Android app’s image caching strategy by transitioning from a traditional Least Recently Used (LRU) cache to a Time-Aware LRU (TLRU) system. This shift allowed them to reduce storage usage by up to 50MB per device while maintaining performance and server efficiency.

Why Traditional LRU Falls Short

Grab’s Android app previously relied on Glide’s LRU cache to store images locally. While LRU effectively evicts rarely used images, it has two major drawbacks:

  • Overfilling: A 100MB LRU cache often filled too quickly, forcing frequent evictions and degrading performance.
  • Stale Data: Images could remain cached for months if the cache never reached capacity, wasting storage space.

These issues created a trade-off between storage efficiency and user experience.

Introducing Time-Aware LRU (TLRU)

To address these challenges, Grab implemented TLRU, which adds time-based expiration to the LRU algorithm. TLRU introduces three key parameters:

  1. Time to Live (TTL): Sets a maximum age for cached images before they expire.
  2. Minimum Cache Size: Ensures essential images stay cached even after expiration if storage is low.
  3. Maximum Cache Size: Prevents the cache from exceeding a defined storage limit.

Implementation Strategy

Instead of building a custom solution, Grab forked Glide and extended its DiskLruCache implementation. This approach leveraged Glide’s mature codebase while adding:

  • Timestamp Tracking: To sort and evict entries based on last access time.
  • Time-Based Eviction Logic: Automatically removes expired entries during cache access.
  • Cache Migration: Assigned a consistent migration timestamp to existing LRU entries, ensuring compatibility with the new system.

Results and Optimization

Grab’s experiments focused on balancing storage savings with performance. Their success criteria allowed a maximum 3% drop in cache hit ratio to avoid excessive server requests. The results were impressive:

  • 95% of users: Achieved a 50MB reduction in cache size.
  • Top 5% of users: Saw even greater storage savings.
  • Overall: Reclaimed terabytes of device storage across the user base without increasing server costs.

Key Technical Considerations

Grab’s implementation prioritized robustness and flexibility:

  • Bidirectional Compatibility: The original LRU system can read TLRU cache files, enabling safe rollbacks.
  • Controlled Experiments: Used A/B testing to validate performance thresholds before full rollout.
  • Crash Recovery: Leveraged Glide’s existing thread-safety and crash-handling mechanisms.

Why This Matters for Mobile Developers

Grab’s TLRU strategy demonstrates how combining time-based logic with traditional caching algorithms can solve real-world storage challenges. By extending a mature framework like Glide, they minimized development risk while achieving significant efficiency gains. This approach is particularly valuable for apps with high image traffic, where storage optimization directly impacts user experience and infrastructure costs.

For deeper technical insights, read the original Grab engineering blog post to explore implementation details and performance metrics.

Takeaways

  • Time-Aware LRU balances storage efficiency and performance through smart expiration rules.
  • Extending existing libraries like Glide can accelerate development while ensuring reliability.
  • Controlled experiments are essential to validate caching strategies before deployment.

Whether you’re optimizing an image-heavy app or managing large-scale caching systems, Grab’s approach offers a proven blueprint for balancing storage, performance, and user experience.