Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Caching - A Primer

Ash Christopher
April 15, 2012
2.6k

Caching - A Primer

Ash Christopher

April 15, 2012
Tweet

Transcript

  1. Caching and Django
    A Primer
    @ashchristopher

    View Slide

  2. What is a Cache?

    View Slide

  3. Temporary storage
    of non-complex data

    View Slide

  4. Provides very
    fast lookups

    View Slide

  5. Django supports simple
    caching

    View Slide

  6. Django
    includes support for a number of
    cache
    backends out-of-the-box

    View Slide

  7. Memcache
    cluster of Memcache servers
    Database cache
    uses database table for cache
    Filesystem cache
    file on the local filesystem
    Local memory cache
    only good for local development

    View Slide

  8. Easy to make your own
    cache backend

    View Slide

  9. Extend
    core.cache.backends.base.BaseCache

    View Slide

  10. Simple caching follows the
    same pattern

    View Slide

  11. 1.
    Check the cache for data and if found,
    return it
    2. If data is not in cache -
    get data from the
    database,
    put data in cache then return the
    data
    3.
    Invalidate cache on data change.

    View Slide

  12. Your system
    works
    even if your cache is
    offline

    View Slide

  13. Some cool projects/apps to
    help you with it

    View Slide

  14. Django Middleware
    (per-site and per-view)

    View Slide

  15. Cache Machine

    View Slide

  16. Johnny-Cache

    View Slide

  17. View Slide

  18. Low-level Caching
    (doing it by hand)

    View Slide

  19. Caching is simple...

    View Slide

  20. ... except when it isn’t!

    View Slide

  21. Cache invalidation is hard

    View Slide

  22. Cache invalidation is especially
    hard at scale

    View Slide

  23. What happens when an
    expensive
    operation falls out of cache?

    View Slide

  24. Thundering Herd

    View Slide

  25. Fundamental
    flaw?

    View Slide

  26. I think so!

    View Slide

  27. Caching doesn’t mean what it
    used to mean

    View Slide

  28. What you want is
    probably not caching

    View Slide

  29. Denormalized Data

    View Slide

  30. Actively
    set new data instead of letting things fall
    out of cache

    View Slide

  31. Nothing falls out of cache

    View Slide

  32. Set your data via
    asynchronous processes

    View Slide

  33. View Slide

  34. No Silver Bullet

    View Slide

  35. Optimizing read
    speed by adding
    redundant data

    View Slide

  36. Persistent storage

    View Slide

  37. NoSQL

    View Slide

  38. There is a
    problem with denormalized data at
    scale

    View Slide

  39. Denormalized data becomes a
    requirement

    View Slide

  40. What problem?

    View Slide

  41. Embrace denormalized data

    View Slide

  42. @ashchristopher
    [email protected]

    View Slide