Advertisement

Caching and tuning fun for high scalability

Consultant at Cu.be Solutions
Mar. 19, 2011
Advertisement

More Related Content

Advertisement
Advertisement

Caching and tuning fun for high scalability

  1. Caching and tuning fun for high scalability Wim Godden Cu.be Solutions
  2. Owner of Cu.be Solutions (http://cu.be)
  3. PHP developer since 1997
  4. Developer of OpenX
  5. Zend Certified Engineer
  6. Zend Framework Certified Engineer
  7. MySQL Certified Developer
  8. System/network engineers ?
  9. Managers ?
  10. Caching experience ?
  11. Caching and tuning fun for high scalability Wim Godden Cu.be Solutions
  12. 5 visitors/day -> 5 million visitors/day
  13. (Don't expect miracle cure !)
  14. LAMP
  15. LAMP
  16. Architecture
  17. Our test site
  18. Result ?
  19. Caching
  20. What's caching ?
  21. What is caching ? select * from article join user on article.user_id = user.id order by created desc limit 10
  22. Hey, that's frontend !
  23. Theory of caching DB
  24. Theory of caching DB
  25. Blogs
  26. Full pages that don't change
  27. Render -> Store in cache -> retrieve from cache
  28. Usually a small block in a page
  29. Best effect : reused on lots of pages
  30. Resets on every insert/update/delete
  31. free up DB resources for more hits !
  32. XML file parsing
  33. Modification frequency ?
  34. Retrieval frequency ?
  35. Make a complete query log (don't forget to turn it off !)
  36. Check page loading times
  37. Don't rely on it
  38. lots of different queries
  39. Caching SQL queries : preferably not
  40. high latency
  41. locking issues !
  42. -> Hard to scale
  43. Distributed memory caching system
  44. Multiple machines ↔ 1 big memory-based hash-table
  45. Values - max. 1Mbyte
  46. Distributed memory caching system
  47. Multiple machines ↔ 1 big memory-based hash-table
  48. Memcache - where to install
  49. Memcache - where to install
  50. PECL
  51. memcached -d -m <mem> -l <ip> -p <port>
  52. ex. : memcached -d -m 2048 -l 127.0.0.1 -p 11211
  53. Lose session data
  54. Lose shopping cart data
  55. ...
  56. Lose session data
  57. Lose shopping cart data
  58. Caching in PHP <?php $memcache = new Memcache(); $memcache->addServer( '172.16.0.1' , 11211); $memcache->addServer( '172.16.0.2' , 11211); $myData = $memcache->get( 'myKey' ); if ($myData === false ) { $myData = GetMyDataFromDB(); // Put it in Memcache as 'myKey', without compression, with no expiration $memcache->set( 'myKey' , $myData, false , 0); } echo $myData;
  59. Let's give that a go ! /** * Retrieves the 10 highest rated articles * @return array List of highest rated articles */ static public function getTopRatedArticleList () { if (!$articleList = $cache->load( 'topRatedArticleList' )) { $articleList = self :: getTopRatedArticleListUncached (); $cache->save($articleList, 'topRatedArticleList' ); } return $articleList; }
  60. Slab 2 : 480 bytes (400 * 1.2)
  61. Each larger slab has room for fewer items (chunks)
  62. -> Store a lot of very large objects
  63. -> Large slabs might be full
  64. -> Rest of slabs might be free
  65. -> Try to store more -> eviction of data !
  66. Use Cacti or other monitoring tools STAT pid 2941 STAT uptime 10878 STAT time 1296074240 STAT version 1.4.5 STAT pointer_size 64 STAT rusage_user 20.089945 STAT rusage_system 58.499106 STAT curr_connections 16 STAT total_connections 276950 STAT connection_structures 96 STAT cmd_get 276931 STAT cmd_set 584148 STAT cmd_flush 0 STAT get_hits 211106 STAT get_misses 65825 STAT delete_misses 101 STAT delete_hits 276829 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 613193860 STAT bytes_written 553991373 STAT limit_maxbytes 268435456 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT bytes 20418140 STAT curr_items 65826 STAT total_items 553856 STAT evictions 0 STAT reclaimed 0
  67. Memcache - backing up
  68. Memcache - deleting <?php $memcache = new Memcache(); $memcache->delete( 'myKey' ); $myData = $memcache->get( 'myKey' ); // $myData === false
  69. Prefix / namespace your keys !
  70. Only letters, numbers and underscore
  71. Document your key names !
  72. Updating data
  73. Updating data
  74. Adding/updating data $memcache->delete( 'ArticleDetails__Toshiba_32C100U_32_Inch' ); $memcache->delete( 'Homepage_Popular_Product_List' );
  75. Adding/updating data
  76. Adding/updating data - Why it crashed
  77. Adding/updating data - Why it crashed
  78. Adding/updating data - Why it crashed
  79. Cache stampeding elePHPants
  80. Cache stampeding
  81. Memcache code ? DB
  82. Run it before starting Webserver !
  83. -> lots of new connections
  84. -> memory spike
  85. What if the process that created the lock fails ?
  86. Exception to the rule : things that have an end date (calendar items)
  87. Reverse proxy
  88. Lightweight, fast
  89. 7.5% of all Websites
  90. Uses epoll / kqueue
  91. Low memory footprint
  92. 10000 active connections = normal
  93. mod_dav*
  94. Some 3 rd party modules (needs recompilation !)
  95. Win32 binaries
  96. Build from source (./configure; make; make install)
  97. Nginx - Configuration server { listen 80; server_name www.domain.ext *.domain.ext; index index.html; root /home/domain.ext/www; } server { listen 80; server_name photo.domain.ext; index index.html; root /home/domain.ext/photo; }
  98. Put Nginx at port 80
  99. Nginx serves all statics (images, css, js, …)
  100. Forward dynamic requests to Apache
  101. Nginx for static files only server { listen 80; server_name www.domain.ext; location ~* ^.*(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|rtf|js)$ { expires 30d; root /home/www.domain.ext; } location / { proxy_pass http://www.domain.ext:8080; proxy_pass_header Set-Cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
  102. Nginx - let's give that a go !
  103. Now : PHP-FPM (in PHP 5.3.3 !)
  104. Runs on port 9000
  105. Nginx connects using fastcgi method location / { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param SCRIPT_FILENAME /home/www.phpbenelux.eu/$fastcgi_script_name; fastcgi_param SERVER_NAME $host; fastcgi_intercept_errors on; }
  106. Spawn new processes under high load
  107. Chroot
  108. Slow request log !
  109. Spawn new processed under high load
  110. Chroot
  111. Slow request log !
  112. fastcgi_finish_request() -> offline processing
  113. Nginx + PHP-FPM - performance ?
  114. LAMP
  115. Reverse proxy cache / http accelerator / …
  116. Caches (parts of) pages in memory
  117. Nginx is faster (but doesn't have VCL)
  118. /etc/varnish/*.vcl
  119. Varnish - backends + load balancing backend server1 { .host = &quot;192.168.0.10&quot;; } backend server2{ .host = &quot;192.168.0.11&quot;; } director example_director round-robin { { .backend = server1; } { .backend = server2; } }
  120. Varnish - backends + load balancing backend server1 { .host = &quot;192.168.0.10&quot;; .probe = { .url = &quot;/&quot;; .interval = 5s; .timeout = 1 s; .window = 5; .threshold = 3; } }
  121. ACLs
  122. ESI
  123. Varnish with ESI - hold on tight !
  124. Images, js, css
  125. Requests with Set-Cookie
  126. Very large files (it's not a file server !)
  127. User-specific content
  128. Set AllowOverride to None
  129. Don't mod_proxy -> use Nginx or Varnish !
  130. High load on an SSL-site ? -> put SSL on a reverse proxy
  131. Use an opcode cache
  132. Caching storage - Opcode caching
  133. Use an opcode cache
  134. Let's see what difference opcode caching and profilers make...
  135. KCachegrind is your friend
  136. Select the right storage engine
  137. Don't use persistent connect
  138. Caching & Tuning @ frontend http://www.websiteoptimization.com/speed/tweak/average-web-page/
  139. Expires/Cache-Control header
  140. HTTP 304 First request Next requests
  141. HTTP 304 with ETag First request Next requests
  142. Seconds to expiry
  143. Date to expire on
  144. Used by old proxies
  145. Requires clock to be accurate !
  146. HTTP/1.1 : &quot;Cache-Control: no-store&quot;
  147. Test frontend
  148. Check what requests frontend sends to backend
  149. Use inline images in CSS/XHTML (not supported on all browsers yet)
  150. Frontend tuning - inline CSS/XHTML images #navbar span { width: 31px; height: 31px; display: inline; float: left; margin-right: 4px; } .home { background-image: url(data:image/gif;base64,R0lGODlhHwAfAPcAAAAAAIxKAKVjCLW1tb29tcbGvc7OxtZ7ANbWztbW1tbe1t7e1uelMefn1ufn3ufn5+fv3u+MAO/v5+/v7/fGCPf35/f37//nY////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////........MEl0nGVUC6tObNnPceSFBaQVMJAxC4lo3gNOrUaFnTHoAxNm3XVxPfRq139e8BEGAjWD5bgIALw287T8AcAXLly2kjOACdc17higXSIKDO/Lpv7Qq4bw7APgBq8eOzX69InrZ6xe3dbxZffyTGkb8tdx8F+b0Xn2sFsCSBAgTM5lp63RHYnoHUudZgRgkGOGCB+43nGk4OGcQTabKx5dyJKJ7ImoUNCaRRAZYN1ppsrT3Y2gIwyjSQBAtUpABml/0IJGYd6VjQUDH9uBFkGxGm5I8dPQaRUAQUMBdhhBV25ZYUJZBcSAtSJBddWZZ5UAGPOTXlgkNVOSZdBxEwIkYu7VhYnAol5GaadRqF0Uaz0TgXnX2umVFyGakJUUAAADs=); margin-left: 4px; } <img border=0 src=&quot;data:image/gif;base64,R0lGODlhHwAfAPcAAAAAAIxKAKVjCLW1tb29tcbGvc7OxtZ7ANbWztbW1tbe1t7e1uelMefn1ufn3ufn5+fv3u+MAO/v5+/v7/fGCPf35/f37//nY/......Uaz0TgXnX2umVFyGakJUUAAADs=&quot;>
  151. Use inline images in CSS (not supported on all browsers yet)
  152. Use CSS Sprites
  153. CSS Sprites
  154. Tuning content - CSS sprites
  155. Tuning content - CSS sprites 11 images 11 HTTP requests 24KByte 1 images 1 HTTP requests 14KByte
  156. Use inline images in CSS (not supported on all browsers yet)
  157. Have a favicon.ico (don't 404 it !)
  158. Split requests across subdomains
  159. Put statics on a separate subdomain (without cookies !) www.phpbenelux.eu www.phpbenelux.eu images.phpbenelux.eu
  160. HttpGzipModule in Nginx (HttpGzipStaticModule for pre-zipped statics !)
  161. No native support in Varnish
  162. Try to avoid redirects anyway
  163. Watch the logging process ->
  164. Logging = disk I/O -> can kill your site !
  165. Slashdot effect
  166. Use a cache abstraction layer (disk -> Memcache)
  167. Don't install for the worst -> prepare for the worst
  168. Have a test-setup
  169. Slides http://www.slideshare.net/wimg
  170. Twitter @wimgtr
  171. E-mail [email_address]
  172.  
Advertisement