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

Cache me if you can: memcached, caching pattern...

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. Key/Object Store • Give it an object, identified by a

    key
 • Ask for a key
 • If it isn’t expired, you get the object
  2. primary data:
 relational database data you can lose, regenerate fast:

    
 RAM store data you can lose, regenerate slowly: 
 persistent store Where does memcached fit?
  3. HOW DO I LIST ALL KEYS IN MEMCACHED? YOU DON’T

    (or all keys matching a regex, or all...)
  4. your amazing application memcached box A memcached box B memcached

    box C memcached client set $KEY send $KEY
 to box C Received!
  5. EXPIRATION It’s 8am, a request stores an object under the

    key ‘foo’, with a 2 hour expiration time. What happens at 10am?
  6. But what happens when a client tries to access 'foo'

    at 1pm? …'foo' gets removed. NOTHING
  7. Memcached has a fixed amount of memory." If you try

    to store data when memory is full, data will get evicted.
  8. 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
  9. Q: Sometimes fetching a key returns None even though it’s

    not supposed to have expired yet… … AND memcached still has free memory?
  10. page 1 page 2 page 3 page 4 page 5

    page 6 page 7 ... 1 page = 1MB
  11. 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.
  12. 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
  13. 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.
  14. Q: Sometimes fetching a key returns None even though it’s

    not supposed to have expired yet… … AND memcached still has free memory?
  15. 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
  16. 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.
  17. 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/