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

Cache me if you can: memcached, caching patterns and best practices by Guillaume Ardaud

Cache me if you can: memcached, caching patterns and best practices by Guillaume Ardaud

PyCon 2014

April 11, 2014
Tweet

More Decks by PyCon 2014

Other Decks in Programming

Transcript

  1. CACHE ME
    IF YOU CAN
    Guillaume Ardaud

    @gardaud | gardaud.fr

    [email protected]
    Friday April 11th

    PyCon2014

    Montreal

    View full-size slide

  2. MEMCACHED
    -What is it?

    -How does it work?

    -Patterns & Best Practices

    View full-size slide

  3. Key/Object Store
    • Give it an object, identified by a key

    • Ask for a key

    • If it isn’t expired, you get the object

    View full-size slide

  4. O(1) EVERYTHING

    View full-size slide

  5. primary data:

    relational database
    data you can lose, regenerate fast: 

    RAM store
    data you can lose, regenerate slowly: 

    persistent store
    Where does memcached fit?

    View full-size slide

  6. Typical memcached API

    View full-size slide

  7. Key names
    - ASCII based

    - Not crazy long…

    - … but explicit enough

    View full-size slide

  8. Good naming
    e.g.: 

    json.users.

    View full-size slide

  9. Bad naming
    e.g.: 

    md5(sql_query)

    View full-size slide

  10. HOW DO I LIST ALL KEYS IN
    MEMCACHED?
    YOU DON’T
    (or all keys matching a regex, or all...)

    View full-size slide

  11. it’s a cache

    not a database

    View full-size slide

  12. memcached docs:

    View full-size slide

  13. memcached is distributed

    View full-size slide

  14. your amazing
    application
    memcached
    box A
    memcached
    box B
    memcached
    box C
    memcached client

    View full-size slide

  15. your amazing
    application
    memcached
    box A
    memcached
    box B
    memcached
    box C
    memcached client
    set $KEY
    send $KEY

    to box C
    Received!

    View full-size slide

  16. your amazing
    application
    memcached
    box A
    memcached
    box B
    memcached client
    set $KEY
    send $KEY

    to box A

    View full-size slide

  17. ~ some memcached subtleties ~

    View full-size slide

  18. EXPIRATION
    It’s 8am, a request stores an
    object under the key ‘foo’, with
    a 2 hour expiration time.
    What happens at 10am?

    View full-size slide

  19. But what happens when a client tries to access 'foo'

    at 1pm?
    …'foo' gets removed.
    NOTHING

    View full-size slide

  20. Q: Sometimes
    fetching a key returns
    None even though it’s
    not supposed to have
    expired yet?

    View full-size slide

  21. Memcached has a fixed amount
    of memory."
    If you try to store data when
    memory is full, data will get
    evicted.

    View full-size slide

  22. How does memcached determine what to
    evict?
    Object 1 Object 2
    timestamp:

    1378399504
    timestamp:

    137839983
    Object 3
    timestamp:

    1378399939
    Object 4
    timestamp:

    13783992932
    Object 5
    timestamp:

    13783993829

    View full-size slide

  23. Q: Sometimes fetching a
    key returns None
    even though it’s not
    supposed to have expired
    yet…
    … AND memcached still
    has free memory?

    View full-size slide

  24. page
    1
    page
    2
    page
    3
    page
    4
    page
    5
    page
    6
    page
    7
    ...
    1 page = 1MB

    View full-size slide

  25. page
    chunk
    chunk
    chunk chunk chunk chunk chunk chunk chunk
    chunk chunk chunk chunk chunk chunk chunk
    1st_slab_chunk_size = 80 bytes!
    2nd_slab_chunk_size = 80 * 1.25 = 100 bytes!
    3rd_slab_chunk_size = 100 * 1.25 = 125 bytes!
    . . .!
    last_slab_chunk_size = 1048576 bytes
    Last slab has 1 chunk of 1 MB.

    View full-size slide

  26. page
    1
    page
    2
    page
    3
    page
    4
    page
    5
    page
    6
    page
    7
    ...
    class 1 class 2 class 3 class 4 class 5 free page free page

    View full-size slide

  27. If all pages of the needed class are full:

    get a free page and give it the needed class.
    If there are no more free pages: 

    the LRU kicks in and evicts data.
    Each slab class has its own LRU!
    "
    If your data needs class 3 and:

    - there are no free pages

    - all class 3 pages are full

    - some class 4 pages have free chunks

    Data in a class 3 page will still be evicted.

    View full-size slide

  28. Q: Sometimes fetching a
    key returns None
    even though it’s not
    supposed to have expired
    yet…
    … AND memcached still
    has free memory?

    View full-size slide

  29. some useful command line flags
    memcached -v : verbose output (more verbose: -vv)

    memcached -M: doesn’t evict when out of memory, but errors

    "
    memcached -I1k : change slab page size (1k - 128m)

    memcached -I1m!
    memcached -I128m!
    "
    memcached -f1.5 : change growth factor (def. 1.25, >1)

    "
    "
    man memcached is your friend

    View full-size slide

  30. Let’s get our
    hands dirty.
    common memcached patterns &
    practices in Python

    View full-size slide

  31. MODEL VERSIONING

    View full-size slide

  32. FETCH STRAIGHT FROM CACHE

    View full-size slide

  33. THUNDERING HERD

    View full-size slide

  34. CACHING LARGE VALUES

    View full-size slide

  35. 2-PHASE FETCH

    View full-size slide

  36. 1. Break big object in smaller chunks

    (for example chunks of 10 books)
    PAGINATED CACHE
    2. Store each chunk as a separate

    object in memcached
    3. Store the list of keys
    To fetch the data, fetch the list of keys,

    iterate through it, and concatenate all fetched lists.

    View full-size slide

  37. Questions?
    THANKS!

    View full-size slide

  38. Great memcached resources
    Memcached doc:
    http://code.google.com/p/memcached/wiki/NewStart?tm=6

    "
    Especially those 2 pages:
    http://code.google.com/p/memcached/wiki/NewProgrammingTricks

    http://code.google.com/p/memcached/wiki/NewUserInternals

    "
    Various articles/posts about internals etc.:
    http://stackoverflow.com/questions/6868256/memcached-eviction-prior-to-key-
    expiry

    http://www.adayinthelifeof.nl/2011/02/06/memcache-internals/

    http://work.tinou.com/2011/04/memcached-for-dummies.html

    http://returnfoo.com/2012/02/memcached-memory-allocation-and-optimization-2/

    View full-size slide