Memcached Code Camp 2009

download Memcached Code Camp 2009

If you can't read please download the document

  • date post

  • Category


  • view

  • download


Embed Size (px)


memcached best practices presentation at Silicon Valley Code Camp 2009,

Transcript of Memcached Code Camp 2009

  • 1. memcached scaling your websitewith memcachedby: steve yen

2. about me Steve Yen NorthScale Escalate Software Kiva Software 3. what youll learn what, where, why, when how especially, best practices 4. mem cache dee latest version1.4.1 5. open source 6. distributed cache 7. livejournal 8. helps your websites run fast 9. popular 10. simple 11. KISS 12. easy 13. small bite-sized steps not a huge, forklift replacement rearchitecture / reengineering project 14. fast 15. i only block for memcached 16. scalable 17. many client libraries might be TOO many the hit list... Java ==> spymemcached C ==> libmemcached Python, Ruby, etc ==> libmemcached wrappers 18. frameworks rails django spring / hibernate cakephp, symphony, etc 19. applications drupal wordpress mediawiki etc 20. it worksit promises to solve performance problemsit delivers! 21. problem? 22. your website is too slow 23. RDBMS melting down 24. urgent! emergency 25. one server web app + RDBMS 26. 1 + 1 servers web appRDBMS 27. N + 1 serversweb app, web app, web app, web appRDBMS 28. RDBMS 29. EXPLAIN PLAN? 30. buy a bigger box 31. buy better disks 32. master write DB + multiple read DB? 33. vertical partitioning? 34. sharding? 35. uh oh, big reengineering risky! touch every line of code, every query!! 36. and, its 2AM 37. you need a band-aid 38. a simple band-aid now 39. use a cache 40. keep things in memory! 41. dont hit disk 42. distributed cache to avoid wasting memory 43. dont write one of these yourself 44. memcached 45. simple API hash-table-ish 46. your code beforev = db.query( SOME SLOW QUERY ) 47. your code afterv = memcachedClient.get(key) if (!v) { v = db.query( SOME SLOW QUERY ) memcachedClient.set(key, v) } 48. cache read-heavy stuff 49. invalidate when writing db.execute(UPDATE foo WHERE ...) memcachedClient.delete(...) 50. and, repeat each day... look for the next slowest operations add code to cache a few more things 51. your life gets better 52. thank you memcached! 53. no magic 54. you are in control 55. now for the decisions 56. memcached adoption rst, start using memcached poorly but you can breathe again 57. memcached adoption next, start using memcached correctly 58. memcached adoption later queueing persistence replication ... 59. an early question 60. where to run servers? 61. answer 1 right on your web servers a great place to start, if you have extra memory 62. serversweb app web app web app web app memcached memcached memcached, memcached RDBMS 63. add up your memory usage! having memcached server swap == bad! 64. answer 2 run memcached right on your database server? WRONG! 65. answer 3 run memcached on separate dedicated memcached servers congratulations! you either have enough money or enough trafc that it matters 66. running a server daemonize dont be root! no security 67. server lists mc-server1:11211 mc-server2:11211 mc-server3:11211 68. consistent hashingsource: 69. client-side intelligence no server master bottleneck 70. libmemcached fast C memcached client supports consistent hashing many wrappers to your favorite languages 71. updating server lists push out new congs and restart? moxi memcached + integrated proxy 72. keys no whitespace 250 char limit use short prexes 73. keys & MD5 dont stats become useless 74. values any binary object 1MB limit change #dene & recompile if you want more and youre probably doing something wrong if you want more 75. values query resultset serialized object page fragment pages etc 76. nginx + memcached 77. >1 language? JSON protocol buffers XML 78. memcached is lossy memcached WILL lose data 79. thats a good thingremember, its a CACHE 80. why is memcached lossy? 81. memcached node dies 82. when node restarts... you just get a bunch of cache misses (and a short RDBMS spike) 83. eviction more disappearing data! 84. LRU can cong memcached to not evict but, youre probably doing something wrong if you do this 85. remember, it forgets its just a CACHE 86. expiration aka, timeouts memcached.set(key, value, timeout) 87. use expirations or not? 88. 1st school of thought expirations hide bugs you should be doing proper invalidations (aka, deletes) coherency! 89. school 2 its 3AM and I cant think anymore business guy: sessions should auto-logout after 30 minutes due to bank security policy 90. put sessions in memcached? just a cong change eg, Ruby on Rails 91. good can load-balance requests to any web host dont touch the RDBMS on every web request 92. bad could lose a users session 93. solution save sessions to memcached the rst time, also save to RDBMS ideally, asynchronously on cache miss, restore from RDBMS 94. solution save sessions to memcached the rst time, also save to RDBMS ideally, asynchronously on cache miss, restore from RDBMS 95. in the background... have a job querying the RDBMS cron job? the job queries for old looking session records in the sessions table refresh old session records from memcached 96. add vs replace vs set 97. append vs prepend 98. CAS compare - and - swap 99. incr and decr no negative numbers 100. queueing hey, with those primitives, I could build a queue! 101. dont memcached is lossy protocol is incorrect for a queue instead gearman beanstalkd etc 102. cache stampedes gearman job-unique-id encode a timestamp in your values one app node randomly decides to refresh slightly early 103. coherency 104. denormalization or copies of data 105. example: changing aproduct price 106. memcached UDFs another great tool in your toolbox on a database trigger, delete stuff from memcached 107. memcached UDFs works even if you do UPDATES with fancy WHERE clauses 108. multigets they are your friend memcached is fast, but... imagine 1ms for a get request 200 serial gets ==> 200ms 109. a resultset loopforeach product in resultset c = memcached.get(product.category_id) do something with c 110. 2 loops for product in resultset multiget_request.append(product.category_id) multiget_response = memcachedClient.multiget( multiget_request) for c in multiget_response do something with c 111. memcached slabber allocates memory into slabs it might learn the wrong slab sizes watch eviction stats 112. losing a node means your RDBMS gets hit 113. replication simple replication in libmemcached >= 2x memory cost only simple verbs set, get, delete doesnt handle apping nodes 114. persistence 115. things that speak memcached tokyo tyrant memcachedb moxi 116. another day monitoring & statistics near caching moxi 117. thanks!!! love any feedback your memcached war stories your memcached wishlist [email protected] 118. thanks!photo credits