Caching Up and
Down the Stack
1
James Meickle
Developer evangelist, AppNeta
@jmeickle
Django Boston Meetup Group
March 26,...
2
4
WHAT IS CACHING?
Uncached
Client
Data
Source
3
4
WHAT IS CACHING?
Uncached Cached
Client
Data
Source
Data
Source
Client
Cache Intermediary
4
4
WHAT IS CACHING?
Uncached Cached
Client
Data
Source
Data
Source
Client
Cache Intermediary
Fast
!
Slow...
5
• Images
• CSS
• JavaScript
• HTML documents
• DNS
4
WHAT GETS CACHED BY
CLIENTS?
6
• HTML documents
• HTML fragments
• Operations on data
• Database queries
• Expensive objects
4
WHAT GETS CACHED BY APPS?
7
• Compiled source
• Packages
• Disk access
• Memory access
• CPU instructions
4
WHAT GETS CACHED BY
SERVERS?
8
• Client-side assets
• Full pages
4
CACHING IN DJANGO:
FRONTEND
9
4
OUR SITES
appneta.com
(static site via Jekyll)
10
4
OUR SITES
internal.tracelytics.com
(dynamic single page Pylons app)
11
4
CLIENT-SIDE ASSETS
12
• Use HTTP caches!
• CDN
• Intermediate proxies
• Browser
• Set policy with cache headers
• Cache-Control
• Expires
4
C...
13
4
CLIENT-SIDE ASSETS
/tl-layouts_base-compiled-757f5eec3603f60850acfdb86e6701cf104f80ae.css
Request Method: GET
Status ...
14
4
CLIENT-SIDE ASSETS
appneta.com
internal.tracelytics.com
15
4
CLIENT-SIDE ASSETS (IN
DJANGO)
VS
16
• Full pages
• Partial pages
• Objects
• Queries
4
CACHING IN DJANGO: BACKEND
17
4
WE’RE STILL TALKING ABOUT
PAGES?
Client-side
assets
Pages
18
4
FULL-PAGE HTTP CACHING
Client Varnish
Do HTTP caching, but with your rules.
No internet standards necessary!
Webserver
19
• Why do it server-side?
• Invalidation
• Amount cached
• Changing cache policies
4
FULL-PAGE HTTP CACHING
20
4
FULL-PAGE HTTP CACHING (IN
DJANGO)
21
• Full pages
• Partial pages
• Objects
• Queries
4
CACHING IN DJANGO: BACKEND
22
4
FULL-PAGE HTTP CACHING?
appneta.com
24
4
FRAGMENT CACHING
appneta.com
Ruins
everything
25
4
FRAGMENT CACHING
26
4
FRAGMENT CACHING
27
4
FRAGMENT CACHING
28
4
FRAGMENT CACHING (IN
DJANGO)
29
• Full pages
• Partial pages
• Objects
• Queries
4
CACHING IN DJANGO: BACKEND
30
4
OBJECT CACHING
def get_item_by_id(key):
# Look up the item in our database
return session.query(User)
.filter_by(id=k...
31
4
OBJECT CACHING
def get_item_by_id(key):
# Check in cache
val = mc.get(key)
# If exists, return it
if val:
return val
...
32
4
OBJECT CACHING
@decorator
def cache(expensive_func, key):
# Check in cache
val = mc.get(key)
# If exists, return it
i...
33
4
OBJECT CACHING
@cache
def get_item_by_id(key):
# Look up the item in our database
return session.query(User)
.filter_...
34
4
OBJECT CACHING (IN DJANGO)
35
• Full pages
• Partial pages
• Objects
• Queries
4
CACHING IN DJANGO: BACKEND
36
4
QUERY CACHING
Cached
Table
Data
DB client
Query
Cache
SQL server
Retrieve results from memory…
…or from memcached…
…o...
37
4
QUERY CACHING
mysql> select SQL_CACHE count(*) from traces;
+----------+
| count(*) |
+----------+
| 3135623 |
+-----...
38
4
QUERY CACHING (IN DJANGO)
39
4
QUERY CACHING (IN
TRACEVIEW)
Cassandra in TraceView:
Retrieving data from data warehouse
40
4
QUERY CACHING (IN
TRACEVIEW)
Memcache in TraceView:
97%ile is 50ms!
41
• Invalidation
• Fragmentation
• Stampedes
• Complexity
4
WHAT CAN GO WRONG?
42
4
INVALIDATION
Uncached Cached
Client
Data
Source
Data
Source
Client
Cache Intermediary
Invalidation
43
4
INVALIDATION
Drupal:
Automatically cache everything!
44
4
INVALIDATION
Memcache GET performance
45
4
INVALIDATION
Do you know what you’re storing in
this call?
46
4
INVALIDATION
Memcache SET performance
47
4
FRAGMENTATION
“In the beginning there was NCSA Mosaic, and Mosaic
called itself NCSA_Mosaic/2.0 (Windows 3.1), and
Mo...
48
• On a cache miss extra work is done
• The result is stored in the cache
• What if multiple simultaneous
misses?
• Ever...
49
• What caching scheme?
• How many extra servers?
• What happens if they fail?
• What will you do to debug it?
4
COMPLEX...
50
• The ‘how’ of caching:
• What are you caching?
• Where are you caching it?
• How bad is a cache miss?
• How and when a...
51
• The ‘why’ of caching:
• Did it actually get faster?
• Is speed worth extra complexity?
• Don’t guess – measure!
• Alw...
52
• ?
4
QUESTIONS
53
• Django documentation on caching:
https://docs.djangoproject.com/en/dev/topics/cache/
• Varnish caching, via Disqus:
h...
54
TraceView
Stop by later tonight to hear more
about AppNeta or TraceView 
(make sure to get a sticker!)
April 1: Come d...
Caching Up and Down the Stack
Upcoming SlideShare
Loading in …5
×

Caching Up and Down the Stack

695 views
535 views

Published on

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

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
695
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Caches that exist at the client or CDN level
  • Caches that exist within a running application’s code
  • Caches that exist at the programming language or infrastructure level
  • Caching Up and Down the Stack

    1. 1. Caching Up and Down the Stack 1 James Meickle Developer evangelist, AppNeta @jmeickle Django Boston Meetup Group March 26, 2014
    2. 2. 2 4 WHAT IS CACHING? Uncached Client Data Source
    3. 3. 3 4 WHAT IS CACHING? Uncached Cached Client Data Source Data Source Client Cache Intermediary
    4. 4. 4 4 WHAT IS CACHING? Uncached Cached Client Data Source Data Source Client Cache Intermediary Fast ! Slow...
    5. 5. 5 • Images • CSS • JavaScript • HTML documents • DNS 4 WHAT GETS CACHED BY CLIENTS?
    6. 6. 6 • HTML documents • HTML fragments • Operations on data • Database queries • Expensive objects 4 WHAT GETS CACHED BY APPS?
    7. 7. 7 • Compiled source • Packages • Disk access • Memory access • CPU instructions 4 WHAT GETS CACHED BY SERVERS?
    8. 8. 8 • Client-side assets • Full pages 4 CACHING IN DJANGO: FRONTEND
    9. 9. 9 4 OUR SITES appneta.com (static site via Jekyll)
    10. 10. 10 4 OUR SITES internal.tracelytics.com (dynamic single page Pylons app)
    11. 11. 11 4 CLIENT-SIDE ASSETS
    12. 12. 12 • Use HTTP caches! • CDN • Intermediate proxies • Browser • Set policy with cache headers • Cache-Control • Expires 4 CLIENT-SIDE ASSETS
    13. 13. 13 4 CLIENT-SIDE ASSETS /tl-layouts_base-compiled-757f5eec3603f60850acfdb86e6701cf104f80ae.css Request Method: GET Status Code: 304 Not Modified Cache-Control: max-age=315360000 Connection: keep-alive Date: Mon, 18 Feb 2013 22:46:12 GMT Expires: Thu, 31 Dec 2037 23:55:55 GMT Last-Modified: Tue, 12 Feb 2013 21:10:20 GMT Server: nginx/0.8.54
    14. 14. 14 4 CLIENT-SIDE ASSETS appneta.com internal.tracelytics.com
    15. 15. 15 4 CLIENT-SIDE ASSETS (IN DJANGO) VS
    16. 16. 16 • Full pages • Partial pages • Objects • Queries 4 CACHING IN DJANGO: BACKEND
    17. 17. 17 4 WE’RE STILL TALKING ABOUT PAGES? Client-side assets Pages
    18. 18. 18 4 FULL-PAGE HTTP CACHING Client Varnish Do HTTP caching, but with your rules. No internet standards necessary! Webserver
    19. 19. 19 • Why do it server-side? • Invalidation • Amount cached • Changing cache policies 4 FULL-PAGE HTTP CACHING
    20. 20. 20 4 FULL-PAGE HTTP CACHING (IN DJANGO)
    21. 21. 21 • Full pages • Partial pages • Objects • Queries 4 CACHING IN DJANGO: BACKEND
    22. 22. 22 4 FULL-PAGE HTTP CACHING? appneta.com
    23. 23. 24 4 FRAGMENT CACHING appneta.com Ruins everything
    24. 24. 25 4 FRAGMENT CACHING
    25. 25. 26 4 FRAGMENT CACHING
    26. 26. 27 4 FRAGMENT CACHING
    27. 27. 28 4 FRAGMENT CACHING (IN DJANGO)
    28. 28. 29 • Full pages • Partial pages • Objects • Queries 4 CACHING IN DJANGO: BACKEND
    29. 29. 30 4 OBJECT CACHING def get_item_by_id(key): # Look up the item in our database return session.query(User) .filter_by(id=key) .first()
    30. 30. 31 4 OBJECT 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
    31. 31. 32 4 OBJECT CACHING @decorator def 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
    32. 32. 33 4 OBJECT CACHING @cache def get_item_by_id(key): # Look up the item in our database return session.query(User) .filter_by(id=key) .first()
    33. 33. 34 4 OBJECT CACHING (IN DJANGO)
    34. 34. 35 • Full pages • Partial pages • Objects • Queries 4 CACHING IN DJANGO: BACKEND
    35. 35. 36 4 QUERY CACHING Cached Table Data DB client Query Cache SQL server Retrieve results from memory… …or from memcached… …or cache in the DB itself!
    36. 36. 37 4 QUERY 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)
    37. 37. 38 4 QUERY CACHING (IN DJANGO)
    38. 38. 39 4 QUERY CACHING (IN TRACEVIEW) Cassandra in TraceView: Retrieving data from data warehouse
    39. 39. 40 4 QUERY CACHING (IN TRACEVIEW) Memcache in TraceView: 97%ile is 50ms!
    40. 40. 41 • Invalidation • Fragmentation • Stampedes • Complexity 4 WHAT CAN GO WRONG?
    41. 41. 42 4 INVALIDATION Uncached Cached Client Data Source Data Source Client Cache Intermediary Invalidation
    42. 42. 43 4 INVALIDATION Drupal: Automatically cache everything!
    43. 43. 44 4 INVALIDATION Memcache GET performance
    44. 44. 45 4 INVALIDATION Do you know what you’re storing in this call?
    45. 45. 46 4 INVALIDATION Memcache SET performance
    46. 46. 47 4 FRAGMENTATION “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
    47. 47. 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 4 STAMPEDES
    48. 48. 49 • What caching scheme? • How many extra servers? • What happens if they fail? • What will you do to debug it? 4 COMPLEXITY
    49. 49. 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? 4 TAKEAWAYS
    50. 50. 51 • The ‘why’ of caching: • Did it actually get faster? • Is speed worth extra complexity? • Don’t guess – measure! • Always use real-world conditions. 4 TAKEAWAYS
    51. 51. 52 • ? 4 QUESTIONS
    52. 52. 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/ 4 RESOURCES
    53. 53. 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/

    ×