39
Cache Is King Waseem Asif @folio_3 www.folio3.com Copyright 2015

Cache is king

Embed Size (px)

Citation preview

Page 1: Cache is king

Cache Is KingWaseem Asif

@folio_3 www.folio3.com Copyright 2015

Page 2: Cache is king

Introduction Typical Implementation Tools

◦ Memcache, Redis◦ Varnish◦ HTML 5

Challenges

@folio_3 www.folio3.com Copyright 2015

Page 3: Cache is king

A cache is a component that transparently stores data so that future requests for that data can be served faster

Speeds up the data lookups Wikipedia serves 75% content from Cache

Cache

@folio_3 www.folio3.com Copyright 2015

Page 4: Cache is king

Hardware◦ CPU (L1, L2)◦ HDD (Read/Write Buffers)

Applications Database Proxies Web Servers Browsers

It is everywhere

@folio_3 www.folio3.com Copyright 2015

Page 5: Cache is king

Cache Levels

@folio_3 www.folio3.com Copyright 2015

Page 6: Cache is king

Populating the cache◦ Upfront Population◦ Lazy Population

Keeping the cache and source in sync◦ Time based Expiry◦ Active Expiry◦ Write through Cache

Managing cache size◦ Removing data

Consideration

@folio_3 www.folio3.com Copyright 2015

Page 7: Cache is king

Populating the Cache

Upfront Population Lazy Population

No one-off cache read delay

The initial build of the cache takes time

Data cached may not be read at all

First request is not found in cache

Only caches data that is actually read

Has no cache build delay

@folio_3 www.folio3.com Copyright 2015

Page 8: Cache is king

Time Based Expiry◦ Data is removed from cache after some time◦ Most commonly used◦ Short expiry time can be a problem

Active Expiry◦ Source tells cache to remove data when updated

or removed Write-through cache

◦ Allow read and write to cache◦ Data is added/written to source when written to

cache

Keeping in Sync

@folio_3 www.folio3.com Copyright 2015

Page 9: Cache is king

Cache storage is smaller then original data Have to manage space smartly To make space for new data:

◦ Time based eviction◦ First in, first out (FIFO)◦ Least accessed◦ Least time between access◦ Many others (http://en.wikipedia.org/wiki/Cache_algorithms#Examples)

Managing Size

@folio_3 www.folio3.com Copyright 2015

Page 10: Cache is king

Without Cache

@folio_3 www.folio3.com Copyright 2015

Page 11: Cache is king

With Cache

@folio_3 www.folio3.com Copyright 2015

Page 12: Cache is king

Update Cache

@folio_3 www.folio3.com Copyright 2015

Page 13: Cache is king

Application◦ Objects◦ Arrays / Query results◦ String/HTML/ Full Page

Database◦ Query cache

Web Server◦ Static resources◦ Full page cache

Browser◦ Headers◦ HTML5

Cache Levels

@folio_3 www.folio3.com Copyright 2015

Page 14: Cache is king

@folio_3 www.folio3.com Copyright 2015

Page 15: Cache is king

It is Free & Open Source Distributed memory based cache Key value store Easy to deploy Client libraries available in most of the

languages

Memcached

@folio_3 www.folio3.com Copyright 2015

Page 16: Cache is king

Since 2003 Written in C Works on Linux, Windows Fast by default Security can be an issue

◦ Expose only to trusted network◦ Optional authentication

Memcached

@folio_3 www.folio3.com Copyright 2015

Page 17: Cache is king

Memcached in Application

@folio_3 www.folio3.com Copyright 2015

Page 18: Cache is king

Memcached Internals

@folio_3 www.folio3.com Copyright 2015

Page 19: Cache is king

Issues with large implementations.◦ Large works are fine by default

No communication between Servers◦ No Data replication

Capacity issue of single Server No warm-up on startup Solution

◦ Mick-Router

Issues with Memcached

@folio_3 www.folio3.com Copyright 2015

Page 20: Cache is king

Tool by Facebook Provides failover, Load balance, warm-up

server, replication of server How it works:

◦ https://code.facebook.com/posts/296442737213493/introducing-mcrouter-a-memcached-protocol-router-for-scaling-memcached-deployments/

Memcached Router

@folio_3 www.folio3.com Copyright 2015

Page 21: Cache is king

Admin Interface

@folio_3 www.folio3.com Copyright 2015

Page 22: Cache is king

@folio_3 www.folio3.com Copyright 2015

Page 23: Cache is king

@folio_3 www.folio3.com Copyright 2015

Page 24: Cache is king

@folio_3 www.folio3.com Copyright 2015

Page 25: Cache is king

Key-value cache Optionally a permanent Store

◦ Snapshotting◦ Log Based

Support a lot of Data Structures.◦ String, Lists (arrays), Sets (Unique), Hashes (Objects),

Sorted Sets, BitMaps, HyperLogLogs etc◦ http://redis.io/topics/data-types-intro

Lot more than just SET, GET and DEL◦ Increment, Append, LPUSH, RPOP, LRANGE◦ Pipelining (Send multiple commands in one go)◦ http://redis.io/commands

Redis

@folio_3 www.folio3.com Copyright 2015

Page 26: Cache is king

Replication Client libraries available in most of the languages

◦ C, C#, PHP, Objective-C, Node.Js, Python, Ruby and more◦ http://redis.io/clients

Works on Windows◦ Windows port developed/maintained by Microsoft Open

Tech group Available on cloud

◦ AWS, Morpheus, RedisToGo, RackSpace, Microsoft Azure◦ Comes with a lot of preset like backup, monitoring, high

availability, ACL etc ◦ http://en.wikipedia.org/wiki/Redis#Cloud_deployment

Redis

@folio_3 www.folio3.com Copyright 2015

Page 27: Cache is king

Performance◦ Has built-in benchmark, easy to run◦ It is easy to run test and find out real picture

◦ http://redis.io/topics/benchmarks Since 2009 Written in C

Redis

@folio_3 www.folio3.com Copyright 2015

Page 28: Cache is king

Redis Memcached

Stores data in a variety of formats Complex to configure (Default

configuration can work) Pipelining! Multiple commands at

once Locking reads - will wait until

another process writes data to the cache

Mass insertion of data to prime a cache

Partitions data across multiple redis instances

Can back data to disk Pub/Sub feature

Doesn't do anything besides being an in-memory key/value store

Low complexity Simple to configure Few command macros

making it simple to master Runs like a rock Many years in production Every programming

language has a memcached library.

@folio_3 www.folio3.com Copyright 2015

Page 29: Cache is king

A lot of bench marks available◦ http://oldblog.antirez.com/post/redis-memcached-

benchmark.html◦ http://dormando.livejournal.com/525147.html◦ http://oldblog.antirez.com/post/update-on-memca

ched-redis-benchmark.html

@folio_3 www.folio3.com Copyright 2015

Page 30: Cache is king

NCache: Distributed Cache for .NET CouchDB: Can be used as replacement of

Memcache MemSQL: The Database for Speed, Scale &

Simplicity And a lot more

Other Tools

@folio_3 Copyright 2015www.folio3.com

Page 31: Cache is king

@folio_3 Copyright 2015www.folio3.com

Page 32: Cache is king

Sits in front of web server(s)

Varnish

@folio_3 Copyright 2015www.folio3.com

Page 33: Cache is king

Page cache Static File Cache Load Balancing Rewriting and redirecting URLs Varnish Configuration Language

◦ Powerful, Can manage rules, Modify headers Community/Enterprise Editions Becoming the default for specialized cloud

hosting◦ WP, Magento, Joom, Drupal etc

Varnish

@folio_3 Copyright 2015www.folio3.com

Page 34: Cache is king

ESI◦ Useful for user customized dynamic content◦ Help increase TTL for pages◦ <esi:include src="/news/hot_news.html"/>

Works for Windows via CYGWIN◦ Can have IIS on backend

Stores cache in memory or file Paid edition has admin interface

◦ Real-time Statistics◦ Cache group management◦ Invalidations of cache etc

Varnish

@folio_3 Copyright 2015www.folio3.com

Page 35: Cache is king

LiteSpeed: Provides cache, replacement for apache httpd, Load balancer etc

Apache Httpd: Mod Cache, Mod Mem Cache Nginx Cache

Others

@folio_3 Copyright 2015www.folio3.com

Page 36: Cache is king

If cached properly:◦ Resources are not downloaded on next requests

Set “Expires” Header for Static Resources “304 Not Modified” Requests

◦ Involves http round trip but file is not downloaded

Browser

@folio_3 Copyright 2015www.folio3.com

Page 37: Cache is king

Easy to implement Very effective Headers can be set on

◦ Application◦ Web Server

Browser

@folio_3 Copyright 2015www.folio3.com

Page 38: Cache is king

Application Cache◦ manifest="demo.appcache“◦ Define files which are to be cached

CACHE MANIFEST NETWORK: Not to be cached FALLBACK: If page not available then fallback to

these◦ http://diveintohtml5.info/offline.html

HTML 5

@folio_3 Copyright 2015www.folio3.com

Page 39: Cache is king

Which Data to Cache◦ Can’t cache every thing◦ Space is limited

Expiry Time◦ Have to be selective, too short or too long can be a problem

Multiple Cache backend◦ Use frame works so it is easy to Switch

Full Page / Parts◦ Based on needs and usage

Logged/Logged Out Users◦ ESI, Logged in user still has part which can be cached

Removing Data form cache◦ There must be a way to remove individual item

Key Scheme Monitor Hit/Miss

Challenges

@folio_3 Copyright 2015www.folio3.com