Caching Strategies

Embed Size (px)

Citation preview

Slide 1


Caching Strategies

Michal paek, Skype; WebExpo 2011, Prague
@spazef0rze, www.michalspacek.cz

What the... Cache?

HDD cache, CPU cache, DNS cache, web cache, geocache

Q: Kdo vte, co je to cache?ME: cache je rychl loit, kam s ukldaj data z pomalho loitQ: kdo si mysl, e jsem to ekl patn?Me: popovdme si potomQ: kdo pouvte njakou cache?Me: vichni

What the... Cache?

HDD cache, CPU cache, DNS cache, web cache, geocache

CLIENT
(BROWSER)WEB
SERVERDATABASE
SERVER1

23 4

Ukzkov model

HTTP level
caching

Client local disk/memory cache

CLIENT
(BROWSER)WEB
SERVER

Co znamen cache, jak se chov klient, v nejlepm ppad neodejde dn poadavel na server

CLIENT
(BROWSER)WEB
SERVER21

Response headers:
Expires
Last-Modified
ETag
Cache-Control
Pragma

Kdy odejde, server vrt v odpovdi tyto hlaviky

Expires: HTTP-date

Expires: Sat, 24 Sep 2011 12:00:00 GMT

CLIENT
(BROWSER)

< Expires

Expires: HTTP-date

Expires: Sat, 24 Sep 2011 12:00:00 GMT

> Expires

CLIENT
(BROWSER)WEB
SERVER21

Last-Modified: HTTP-date

Following request:
If-Modified-Since: HTTP-date



Last-Modified: HTTP-date

Following request:
If-Modified-Since: HTTP-date

Response:
Entity modified: HTTP 200 OK
Not modified: HTTP 304 Not Modified

Last-Modified: HTTP-date

Saves bandwidth!

ETag: quoted-string

Following request:
If-None-Match: quoted-string



ETag: quoted-string

Following request:
If-None-Match: quoted-string

Response:
No match: HTTP 200 OK
A match: HTTP 304 Not Modified

Saves bandwidth, too!

ETag: quoted-string

If-Modified-Since
If-Unmodified-Since

If-None-Match
If-Match

Hlaviky pro podmnn dotazy maj obrcen varianty

ETag Last-Modified

hash (GUID) HTTP-date (seconds)



ETag Last-Modified

hash (GUID) HTTP-date (seconds)

weak+strong validation strong

ETag Last-Modified

hash (GUID) HTTP-date (seconds)

weak+strong validation strong

needs content needs date

Request+Response header


Cache-Control: whatever

Request+Response header

whatever = max-age, no-cache, public, must-revalidate (response only), ...

Cache-Control: whatever

M spoustu parametr, tohle jsou nejzajmavj, zbytek by vydal na druhou pednku

Pragma: no-cache

Request+Response header




Pragma: no-cache

Request+Response header

Request: HTTP 1.0 variant of
Cache-Control: no-cache

Pragma: no-cache

Request+Response header

Request: HTTP 1.0 variant of
Cache-Control: no-cache

Response: meaning undefined, wtf

Proxy cache

CLIENT
(BROWSER)WEB
SERVER4 31 2

Proxy cache

CLIENT
(BROWSER)

21

WEB
SERVERV idelnm ppad odpovd cache, ne pvvodn server

Application level caching

Configuration

Does not change often



Configuration

Does not change often

Parse (files)/read (from db)
and store to APC

APC: The Alternative PHP Cache

PHP extension

Data store (also opcode cache)

APC: The Alternative PHP Cache

Data persist between requests



APC: The Alternative PHP Cache

Data persist between requests

until deleted
or cache cleared
or server restarted

Database connection cache

Persistent connections (don't work)

Connection pooler

Database query cache

Some systems have query cache

Database query cache

Some systems have query cache

Some don't

Vc se toho ct ned, to je taky na samostatnou pednku

Result set cache

Some data don't change

Result set cache

Some data don't change

Fetch and store to memcached

Memcached

Client-server architecture

Key-value in memory store
for strings and objects

Problems with caching

Data change sometimes
but not often enough


Employee list of a company


Data change sometimes
but not often enough


Employee list of a company

Invalidate cache when
adding or removing employees

Data change sometimes
and delay is okay


Translations from database

Data change sometimes
and delay is okay


Translations from database

Introduce cache lifetime

Moar cache keys for
the same data


Sorted employee list (asc, desc)

Moar cache keys for
the same data


Sorted employee list (asc, desc)

Cache groups

Cache groups


Keys: list-sorted-asc, list-sorted-desc




Cache groups


Keys: list-sorted-asc, list-sorted-desc

Group list: list-sorted-asc,
list-sorted-desc, list-unsorted

Cache groups


Keys: list-sorted-asc, list-sorted-desc

Group list: list-sorted-asc,
list-sorted-desc, list-unsorted

Delete group list when adding/removing

Cache wherever
and whatever

but delete your whatever when needed

...Co nen tak jednoduch, protoe

Caching is magic

Caching is magic
but not automagic

Caching is magic
but not automagic
until now

2010 Skype As seen on TV

Slide

www.phpguru.cz/kaskadova-cache

Nejvt teoretik v echch napsal lnek, nvrh, jak to cel zautomatizovat, problm je ten, e existuje snad jen jeden lovk, kter to doke prakticky napsat.

2010 Skype As seen on TV

Slide

Practical
implementation,anyone?

Thanks for listening

Michal paek, @spazef0rze



BTW, love caching? skypecareers.com!

Slide

2011 Skype Not for resale

09/24/11

2011 Skype Parental Advisory

Slide

09/24/11

2011 Skype . Commercially confidential

Slide

This slide is blank, intended for full screen images

09/24/11

2011 Skype kthxbai

09/24/11

Slide

09/24/11

2009 Skype . Commercially confidential

Slide

09/24/11

2009 Skype . Commercially confidential

Slide

09/24/11