Caching and tuning fun for high scalability

Embed Size (px)

DESCRIPTION

Caching has been a 'hot' topic for a few years. But caching takes more than merely taking data and putting it in a cache : the right caching techniques can improve performance and reduce load significantly. But we'll also look at some major pitfalls, showing that caching the wrong way can bring down your site. If you're looking for a clear explanation about various caching techniques and tools like Memcached, Nginx and Varnish, as well as ways to deploy them in an efficient way, this talk is for you.

Citation preview

  • 1. Caching and tuning funfor high scalabilityWim GoddenCu.be Solutions

2. Who am I ?Wim Godden (@wimgtr) 3. Where I'm from 4. Where I'm from 5. Where I'm from 6. Where I'm from 7. Where I'm from 8. Where I'm from 9. My town 10. My town 11. Belgium the traffic 12. Who am I ?Wim Godden (@wimgtr)Founder of Cu.be Solutions (http://cu.be)Open Source developer since 1997Developer of OpenX, PHPCompatibility, Nginx SLIC, ...Speaker at PHP and Open Source conferences 13. Who are you ?Developers ?System/network engineers ?Managers ?Caching experience ? 14. Goals of this talkEverything about caching and tuningA few techniquesHow-toHow-NOT-toLot of ways, this is just one ;-) Increase reliability, performance and scalability5 visitors/day 500.000 visitors/day(Don't expect miracle cure !) 15. LAMP 16. Test page3 DB-queriesselect firstname, lastname, email from user where user_id = 5;select title, createddate, body from article order by createddate desc limit 5;select title, createddate, body from article order by score desc limit 5;Page just outputs result 17. Our base benchmarkApachebench = useful enoughResult ?Single webserver ProxyStatic PHP Static PHPApache + PHP 3900 17.5 6700 17.5Limit :CPU, networkor diskLimit :database 18. CCaacchhiinngg 19. What is caching ?CCAACCHHEE 20. What is caching ?x = 5, y = 2n = 50 Same resultCCAACCHHEEselect*fromarticlejoin useron article.user_id = user.idorder bycreated desclimit10Doesn't changeall the time 21. Caching goalsSource of information :Reduce # of requestReduce the loadLatency :Reduce for visitorReduce for webserver loadNetwork :Send less data to visitorHey, that's frontend ! 22. Theory of cachingDBCacheif ($data == false)$data = falseget('key')PageGET /pageselect data from table$data = returned resultset('key', $data) 23. Theory of cachingDBCacheHIT 24. Caching techniques#1 : Store entire pages#2 : Store part of a page (block)#3 : Store data retrieval (SQL ?)#4 : Store complex processing result#? : Your call !When you have data, think :Creating time ?Modification frequency ?Retrieval frequency ? 25. How to find cacheable dataNew projects : start from 'cache everything'Existing projects :Check page loading timesLook at MySQL/PgSQL/Oracle/... slow query logMake a complete query log (don't forget to turn it off !) Use Percona Toolkit (pt-query-digest) 26. Caching storage - DiskData with few updates : goodCaching SQL queries : preferably notDON'T use NFShigh latencypossible problem for sessions : locking issues ! 27. Caching storage - Disk / ramdiskLocal5 Webservers 5 local cachesHow will you keep them synchronized ? Don't say NFS or rsync ! 28. Caching storage - Memcache(d)Facebook, Twitter, YouTube, need we say more ?Distributed memory caching systemMultiple machines 1 big memory-based hash-tableKey-value storage systemKeys - max. 250bytesValues - max. 1Mbyte 29. Caching storage - Memcache(d)Facebook, Twitter, YouTube, need we say more ?Distributed memory caching systemMultiple machines 1 big memory-based hash-tableKey-value storage systemKeys - max. 250bytesValues - max. 1MbyteExtremely fast... non-blocking, UDP (!) 30. Memcache - where to install 31. Memcache - where to install 32. Memcache - installation & running itInstallationDistribution packagePECLWindows : binariesRunningNo config-filesmemcached -d -m -l -p ex. : memcached -d -m 2048 -l 172.16.1.91 -p 11211 33. Caching storage - Memcache - some notesNot fault-tolerantIt's a cache !Lose session dataLose shopping cart dataFirewall your Memcache port ! 34. Memcache in code