Caching Strategies

9,683
-1

Published on

A common request sent from your web browser to a web server goes quite a long way and it can take a great deal of time until the data your browser can display are fetched back. I will talk about making this great deal of time significantly less great by caching things on different levels, starting with client-side caching for faster display and minimizing transferred data, storing results of already performed operations and computations and finishing with lowering the load of database servers by caching result sets. Cache expiration and invalidation is the hardest part so I will cover that too. Presentation will be focused mainly on PHP, but most of the principles are quite general work elsewhere too.

Published in: Technology
1 Comment
22 Likes
Statistics
Notes
No Downloads
Views
Total Views
9,683
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
246
Comments
1
Likes
22
Embeds 0
No embeds

No notes for slide
  • Q: Kdo víte, co je to cache? ME: cache je rychlé úložiště, kam s ukládají data z pomalého úložiště Q: kdo si myslí, že jsem to řekl špatně? Me: popovídáme si potom Q: kdo používáte nějakou cache? Me: všichni
  • Ukázkový model
  • Co znamená cache, jak se chová klient, v nejlepším případě neodejde žádný požadavel na server
  • Když odejde, server vrátí v odpovědi tyto hlavičky
  • Hlavičky pro podmíněné dotazy mají obrácené varianty
  • Má spoustu parametrů, tohle jsou nejzajímavější, zbytek by vydal na druhou přednášku
  • V ideálním případě odpovídá cache, ne půvvodní server
  • Víc se toho říct nedá, to je taky na samostatnou přednášku
  • ...Což není tak jednoduchý, protože
  • Největší teoretik v čechách napsal článek, návrh, jak to celý zautomatizovat, problém je ten, že existuje snad jen jeden člověk, který to dokáže prakticky napsat.
  • Caching Strategies

    1. 1. Caching Strategies Michal Špaček, Skype; WebExpo 2011 , Prague @spazef0rze, www.michalspacek.cz
    2. 2. What the... Cache ? HDD cache, CPU cache, DNS cache, web cache, geocache
    3. 3. What the... Cache ? HDD cache, CPU cache, DNS cache, web cache , geocache
    4. 4. CLIENT (BROWSER) WEB SERVER DATABASE SERVER
    5. 5. HTTP level caching
    6. 6. Client local disk/memory cache CLIENT (BROWSER) WEB SERVER
    7. 7. Response headers: Expires Last-Modified ETag Cache-Control Pragma CLIENT (BROWSER) WEB SERVER
    8. 8. Expires: HTTP-date Expires: Sat, 24 Sep 2011 12:00:00 GMT < Expires CLIENT (BROWSER)
    9. 9. Expires: HTTP-date Expires: Sat, 24 Sep 2011 12:00:00 GMT > Expires CLIENT (BROWSER) WEB SERVER
    10. 10. Last-Modified: HTTP-date Following request: If-Modified-Since: HTTP-date
    11. 11. Last-Modified: HTTP-date Following request: If-Modified-Since: HTTP-date Response: Entity modified: HTTP 200 OK Not modified: HTTP 304 Not Modified
    12. 12. Last-Modified: HTTP-date Saves bandwidth!
    13. 13. ETag: quoted-string Following request: If-None-Match: quoted-string
    14. 14. ETag: quoted-string Following request: If-None-Match: quoted-string Response: No match : HTTP 200 OK A match: HTTP 304 Not Modified
    15. 15. Saves bandwidth, too! ETag: quoted-string
    16. 16. If-Modified-Since If-Unmodified-Since If-None-Match If-Match
    17. 17. ETag × Last-Modified hash (GUID) × HTTP-date (seconds)
    18. 18. ETag × Last-Modified hash (GUID) × HTTP-date (seconds) weak+strong validation × strong
    19. 19. ETag × Last-Modified hash (GUID) × HTTP-date (seconds) weak+strong validation × strong needs content × needs date
    20. 20. Request+Response header Cache-Control: whatever
    21. 21. Request+Response header whatever = max-age , no-cache , public , must-revalidate (response only), ... Cache-Control: whatever
    22. 22. Pragma: no-cache Request+Response header
    23. 23. Pragma: no-cache Request+Response header Request: HTTP 1.0 variant of Cache-Control: no-cache
    24. 24. Pragma: no-cache Request+Response header Request: HTTP 1.0 variant of Cache-Control: no-cache Response: meaning undefined, wtf
    25. 25. Proxy cache CLIENT (BROWSER) WEB SERVER
    26. 26. Proxy cache CLIENT (BROWSER) WEB SERVER
    27. 27. Application level caching
    28. 28. Configuration Does not change often
    29. 29. Configuration Does not change often Parse (files)/read (from db) and store to APC
    30. 30. APC: The Alternative PHP Cache PHP extension Data store (also opcode cache)
    31. 31. APC: The Alternative PHP Cache Data persist between requests
    32. 32. APC: The Alternative PHP Cache Data persist between requests … until deleted or cache cleared or server restarted
    33. 33. Database connection cache Persistent connections (don't work) Connection pooler
    34. 34. Database query cache Some systems have query cache
    35. 35. Database query cache Some systems have query cache Some don't
    36. 36. Result set cache Some data don't change
    37. 37. Result set cache Some data don't change Fetch and store to memcached
    38. 38. Memcached Client-server architecture Key-value in memory store for strings and objects
    39. 39. Problems with caching
    40. 40. Data change sometimes but not often enough Employee list of a company
    41. 41. Data change sometimes but not often enough Employee list of a company Invalidate cache when adding or removing employees
    42. 42. Data change sometimes and delay is okay Translations from database
    43. 43. Data change sometimes and delay is okay Translations from database Introduce cache lifetime
    44. 44. Moar cache keys for the same data Sorted employee list (asc, desc)
    45. 45. Moar cache keys for the same data Sorted employee list (asc, desc) Cache groups
    46. 46. Cache groups Keys: list-sorted-asc, list-sorted-desc
    47. 47. Cache groups Keys: list-sorted-asc, list-sorted-desc Group „list“: list-sorted-asc, list-sorted-desc, list-unsorted
    48. 48. 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
    49. 49. Cache wherever and whatever
    50. 50. … but delete your whatever when needed
    51. 51. Caching is magic
    52. 52. Caching is magic but not automagic
    53. 53. Caching is magic but not automagic until now
    54. 54. www.phpguru.cz/kaskadova-cache <ul>2010 © Skype – As seen on TV </ul><ul>Slide </ul>
    55. 55. Practical implementation, anyone ? <ul>2010 © Skype – As seen on TV </ul><ul>Slide </ul>
    56. 56. <ul>Thanks for listening Michal Špaček, @spazef0rze BTW, love caching? skypecareers.com! </ul>

    ×