caching up and down the stack

54
Caching Up and Down the Stack 1 James Meickle Developer evangelist, AppNeta @jmeickle Django Boston Meetup Group March 26, 2014

Upload: james-meickle

Post on 06-May-2015

393 views

Category:

Software


2 download

DESCRIPTION

Presented by James Meickle, developer evangelist at AppNeta, to the Boston Django Meetup on March 28, 2014.

TRANSCRIPT

Page 1: Caching Up and Down the Stack

Caching Up and

Down the Stack

1

James MeickleDeveloper evangelist, AppNeta

@jmeickle

Django Boston Meetup GroupMarch 26, 2014

Page 2: Caching Up and Down the Stack

2

4WHAT IS CACHING?

Uncached

Client

Data Source

Page 3: Caching Up and Down the Stack

3

4WHAT IS CACHING?

Uncached Cached

Client

Data Source

Data Source

Client

Cache Intermediary

Page 4: Caching Up and Down the Stack

4

4WHAT IS CACHING?

Uncached Cached

Client

Data Source

Data Source

Client

Cache Intermediary

Fast!

Slow...

Page 5: Caching Up and Down the Stack

5

• Images

• CSS

• JavaScript

• HTML documents

• DNS

4WHAT GETS CACHED BY CLIENTS?

Page 6: Caching Up and Down the Stack

6

• HTML documents

• HTML fragments

• Operations on data

• Database queries

• Expensive objects

4WHAT GETS CACHED BY APPS?

Page 7: Caching Up and Down the Stack

7

• Compiled source

• Packages

• Disk access

• Memory access

• CPU instructions

4WHAT GETS CACHED BY SERVERS?

Page 8: Caching Up and Down the Stack

8

• Client-side assets

• Full pages

4CACHING IN DJANGO: FRONTEND

Page 9: Caching Up and Down the Stack

9

4OUR SITESappneta.com

(static site via Jekyll)

Page 10: Caching Up and Down the Stack

10

4OUR SITESinternal.tracelytics.com

(dynamic single page Pylons app)

Page 11: Caching Up and Down the Stack

11

4CLIENT-SIDE ASSETS

Page 12: Caching Up and Down the Stack

12

• Use HTTP caches!

• CDN

• Intermediate proxies

• Browser

• Set policy with cache headers

• Cache-Control

• Expires

• ETag

4CLIENT-SIDE ASSETS

Page 13: Caching Up and Down the Stack

13

4CLIENT-SIDE ASSETS

/tl-layouts_base-compiled-757f5eec3603f60850acfdb86e6701cf104f80ae.cssRequest Method: GETStatus Code: 304 Not Modified

Cache-Control: max-age=315360000Connection: keep-aliveDate: Mon, 18 Feb 2013 22:46:12 GMTExpires: Thu, 31 Dec 2037 23:55:55 GMTLast-Modified: Tue, 12 Feb 2013 21:10:20 GMTServer: nginx/0.8.54

Page 14: Caching Up and Down the Stack

14

4CLIENT-SIDE ASSETSappneta.com

internal.tracelytics.com

Page 15: Caching Up and Down the Stack

15

4CLIENT-SIDE ASSETS (IN DJANGO)

VS

Page 16: Caching Up and Down the Stack

16

• Full pages

• Partial pages

• Objects

• Queries

4CACHING IN DJANGO: BACKEND

Page 17: Caching Up and Down the Stack

17

4WE’RE STILL TALKING ABOUT PAGES?

Client-sideassets

Pages

Page 18: Caching Up and Down the Stack

18

4FULL-PAGE HTTP CACHING

Client Varnish

Do HTTP caching, but with your

rules.

No internet standards necessary! Webserver

Page 19: Caching Up and Down the Stack

19

• Why do it server-side?

• Invalidation

• Amount cached

• Changing cache policies

4FULL-PAGE HTTP CACHING

Page 20: Caching Up and Down the Stack

20

4FULL-PAGE HTTP CACHING (IN DJANGO)

Page 21: Caching Up and Down the Stack

21

• Full pages

• Partial pages

• Objects

• Queries

4CACHING IN DJANGO: BACKEND

Page 22: Caching Up and Down the Stack

22

4FULL-PAGE HTTP CACHING?

appneta.com

Page 23: Caching Up and Down the Stack
Page 24: Caching Up and Down the Stack

24

4FRAGMENT CACHING

appneta.com

Ruins everything

Page 25: Caching Up and Down the Stack

25

4FRAGMENT CACHING

Page 26: Caching Up and Down the Stack

26

4FRAGMENT CACHING

Page 27: Caching Up and Down the Stack

27

4FRAGMENT CACHING

Page 28: Caching Up and Down the Stack

28

4FRAGMENT CACHING (IN DJANGO)

Page 29: Caching Up and Down the Stack

29

• Full pages

• Partial pages

• Objects

• Queries

4CACHING IN DJANGO: BACKEND

Page 30: Caching Up and Down the Stack

30

4OBJECT CACHING

def get_item_by_id(key): # Look up the item in our database return session.query(User)\ .filter_by(id=key)\ .first()

Page 31: Caching Up and Down the Stack

31

4OBJECT CACHING

def get_item_by_id(key): # Check in cache val = mc.get(key)

# If exists, return it if val: return val

# If not, get the val, store it in the cache val = return session.query(User)\ .filter_by(id=key)\ .first() mc.set(key, val) return val

Page 32: Caching Up and Down the Stack

32

4OBJECT CACHING

@decoratordef cache(expensive_func, key): # Check in cache val = mc.get(key)

# If exists, return it if val: return val

# If not, get the val, store it in the cache val = expensive_func(key) mc.set(key, val) return val

Page 33: Caching Up and Down the Stack

33

4OBJECT CACHING

@cachedef get_item_by_id(key): # Look up the item in our database return session.query(User)\ .filter_by(id=key)\ .first()

Page 34: Caching Up and Down the Stack

34

4OBJECT CACHING (IN DJANGO)

Page 35: Caching Up and Down the Stack

35

• Full pages

• Partial pages

• Objects

• Queries

4CACHING IN DJANGO: BACKEND

Page 36: Caching Up and Down the Stack

36

4QUERY CACHING

Cached

TableData

DB client

Query Cache

SQL server

Retrieve results from

memory…

…or from memcached…

…or cache in the DB itself!

Page 37: Caching Up and Down the Stack

37

4QUERY CACHING

mysql> select SQL_CACHE count(*) from traces; +----------+| count(*) |+----------+| 3135623 |+----------+1 row in set (0.56 sec)

mysql> select SQL_CACHE count(*) from traces;+----------+| count(*) |+----------+| 3135623 |+----------+1 row in set (0.00 sec)

Page 38: Caching Up and Down the Stack

38

4QUERY CACHING (IN DJANGO)

Page 39: Caching Up and Down the Stack

39

4QUERY CACHING (IN TRACEVIEW)

Cassandra in TraceView:Retrieving data from data warehouse

Page 40: Caching Up and Down the Stack

40

4QUERY CACHING (IN TRACEVIEW)

Memcache in TraceView:97%ile is 50ms!

Page 41: Caching Up and Down the Stack

41

• Invalidation

• Fragmentation

• Stampedes

• Complexity

4WHAT CAN GO WRONG?

Page 42: Caching Up and Down the Stack

42

4INVALIDATION

Uncached Cached

Client

Data Source

Data Source

Client

Cache Intermediary

Invalidation

Page 43: Caching Up and Down the Stack

43

4INVALIDATION

Drupal: Automatically cache everything!

Page 44: Caching Up and Down the Stack

44

4INVALIDATION

Memcache GET performance

Page 45: Caching Up and Down the Stack

45

4INVALIDATION

Do you know what you’re storing in this call?

Page 46: Caching Up and Down the Stack

46

4INVALIDATION

Memcache SET performance

Page 47: Caching Up and Down the Stack

47

4FRAGMENTATION

“In the beginning there was NCSA Mosaic, and Mosaic called itself NCSA_Mosaic/2.0 (Windows 3.1), and

Mosaic displayed pictures along with text, and there was much rejoicing.”

History of the browser user-agent string

Page 48: Caching Up and Down the Stack

48

• On a cache miss extra work is

done

• The result is stored in the cache

• What if multiple simultaneous

misses?

• Every node tries to do the same

work at the same time and your

cache dies

4STAMPEDES

Page 49: Caching Up and Down the Stack

49

• What caching scheme?

• How many extra servers?

• What happens if they fail?

• What will you do to debug it?

4COMPLEXITY

Page 50: Caching Up and Down the Stack

50

• The ‘how’ of caching:

• What are you caching?

• Where are you caching it?

• How bad is a cache miss?

• How and when are you

invalidating?

4TAKEAWAYS

Page 51: Caching Up and Down the Stack

51

• The ‘why’ of caching:

• Did it actually get faster?

• Is speed worth extra

complexity?

• Don’t guess – measure!

• Always use real-world

conditions.

4TAKEAWAYS

Page 52: Caching Up and Down the Stack

52

• ?

4QUESTIONS

Page 53: Caching Up and Down the Stack

53

• Django documentation on caching: https://docs.djangoproject.com/en/dev/topics/cache/

• Varnish caching, via Disqus: http://blog.disqus.com/post/62187806135/scaling-django-to-8-billion-page-views

• Django cache option comparisons: http://codysoyland.com/2010/jan/17/evaluating-django-caching-options/

• More Django-specific tips: http://www.slideshare.net/csky/where-django-caching-bust-at-the-seams

• Guide to cache-related HTTP headers: http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

4RESOURCES

Page 54: Caching Up and Down the Stack

54

TraceView

Stop by later tonight to hear more about AppNeta or TraceView (make sure to get a sticker!)

April 1: Come drink on our tab after the Boston Python Meetup

April 11: PyCon! Swing by our booth (214) for an awesome shirt

Or, just try us out:

THANK YOU!

http://www.appneta.com/products/traceview/