Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mobile App Data Caching

Mobile App Data Caching

Presented at Engineering Brown Bag Session in Onebit

Akhyar Amarullah

May 27, 2015
Tweet

More Decks by Akhyar Amarullah

Other Decks in Programming

Transcript

  1. Mobile App 
 Data Caching! Presented by Akhyar Amarullah
 at

    Engineering Brownbag Session in Onebit
 on Wednesday, May 27th 2015"
  2. “There are only two hard things in Computer Science: 


    cache invalidation and naming things.”! - Phil Karlton !
  3. Terminologies! •  Cache = a component that stores data so

    future requests for that data can be served faster"
  4. Why Cache?! Remove redundant expensive calls! ↓! Reduce latency bottleneck!

    ↓! Serve data to users faster! ↓! Happier users! J"
  5. Cache-First Logic! function get_article (id) throw exception { if (has_article_in_cache(id))

    { // cached data available return get_article_from_cache(id) // fast! } else { // cached data unavailable, // retrieve from network and put to cache article = get_article_from_rest_api(id) // slow! save_article_to_cache(id, article) return article } }
  6. Cache Key Naming! •  Used to uniquely identify cached data

    for lookup purpose! •  Components:! –  Namespace! –  Parameters: id, query params, etc! •  Example:! ü article:14" ü search_result:how%20to%20cache" •  Avoid unnecessary namespace/parameters! x  posts:page6 (pagination often breaks)! x  article:akhyar:14 (avoid unneeded metadata)!
  7. Cache Invalidation! •  The hardest part K! •  Know the

    lifespan of your cached data, 
 invalidate (i.e. throw it away) at the right time! •  Invalidation triggers:! –  Data’s timespan limit! –  Memory/Disk space limit! –  User actions (e.g. manual refresh)! –  Periodic invalidation! –  Push notification! •  Which data to invalidate when we reached the limit?! –  LRU (Least Recently Used)! –  LFU (Least Frequently Used)! –  MRU (Most Recently Used)! –  Bélády's Algorithm!
  8. In Mobile App…! •  Do:! – Cache contents: articles, ! – Cache

    remote bitmap! – Cache expensive calculation results that don’t change too often! •  Avoid:! – Cache sensitive data that might affect users’ perception if they see the cached one (e.g. bank balance)!
  9. Problems in caching mobile app data! •  Too early invalidation

    causes latency overhead! •  Too late invalidation causes stale data! •  Data lifespan is unpredictable!