About Caching

21
About Caching Weng Wei, Dec 2014

Transcript of About Caching

About CachingWeng Wei, Dec 2014

Purpose of Caching

• Different accessing speed of different medium

• In-Process > Memory > Network > Disk

• Cache for Performance Optimization

Web Page Cache

• Browser Cache

• etag / lastmodified / max-age

• Server Cache

• memcached: Full page/pagelet cache

• Donut Cache: Layout cache

memcached

Cache within Request

Read-Through Cache

memcached issues• Deployment

• modifying nodes in cluster

• Duplicated call to backend when cache miss on multiple client

• Performance

• Network latency

• Memory slot

• CAS: check and save

• Mainly used for Read-through cache

GroupCache• https://github.com/golang/groupcache

• No separated deployment required

• singleflight builtin: avoid duplicated call to backend

• Better performance

• No network IO

• No CAS

• Go 1.4 Continuous Stack: fix memslot issue

Write Though

• Ideally

• ZERO read to backend

• Only write

• Data warm up + complete write-through cache

Complete Write-Through

• Available API for application?

• Can support SQL?

• select * from users where sex = ‘f’ order by createAt limit 10, 10

Implementation Examples

• https://github.com/nkallen/cache-money

• https://github.com/Wuvist/pysonic

GroupCache + Write-Through?https://github.com/Wuvist/groupcache/commits/group_delete

–Phil Karlton

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

• Easy to implement『eventual consistency』

• Atomic:Distributed transaction is difficult

• Paxos

• System provision is more difficult

• http://en.wikipedia.org/wiki/Queueing_theory

Which is faster?

proper stress test to confirm!

Connection Pool

• Pool is also a kind of “cache”

• Database connection pool

• Application connection pool

• php FPM / rails unicorn

How to release connection? MRU or LRU?

Multiple Facebook teams spent two years

https://code.facebook.com/posts/1499322996995183/solving-the-mystery-of-link-imbalance-a-metastable-failure-state-at-scale/

Q & A