Slide 1

Slide 1 text

Mobile App 
 Data Caching! Presented by Akhyar Amarullah
 at Engineering Brownbag Session in Onebit
 on Wednesday, May 27th 2015"

Slide 2

Slide 2 text

“There are only two hard things in Computer Science: 
 cache invalidation and naming things.”! - Phil Karlton !

Slide 3

Slide 3 text

Terminologies! •  Cache = a component that stores data so future requests for that data can be served faster"

Slide 4

Slide 4 text

Cache Characteristics! Fast" Small enough! Have lifetime!

Slide 5

Slide 5 text

Why Cache?! Remove redundant expensive calls! ↓! Reduce latency bottleneck! ↓! Serve data to users faster! ↓! Happier users! J"

Slide 6

Slide 6 text

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 } }

Slide 7

Slide 7 text

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)!

Slide 8

Slide 8 text

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!

Slide 9

Slide 9 text

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)!

Slide 10

Slide 10 text

Problems in caching mobile app data! •  Too early invalidation causes latency overhead! •  Too late invalidation causes stale data! •  Data lifespan is unpredictable!