Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

phptek13 - Caching and tuning fun tutorial

6,808 views

Published on

phptek13 Caching and tuning fun for high scalability tutorial

Published in: Technology
  • D0WNL0AD FULL ▶ ▶ ▶ ▶ http://1url.pw/uUCQf ◀ ◀ ◀ ◀
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • D0WNL0AD FULL ▶ ▶ ▶ ▶ http://1url.pw/uUCQf ◀ ◀ ◀ ◀
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • D0WNL0AD FULL ▶ ▶ ▶ ▶ http://1url.pw/uUCQf ◀ ◀ ◀ ◀
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • D0WNL0AD FULL ▶ ▶ ▶ ▶ http://1url.pw/uUCQf ◀ ◀ ◀ ◀
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

phptek13 - Caching and tuning fun tutorial

  1. 1. Caching and tuning funfor high scalabilityWim GoddenCu.be Solutions
  2. 2. Who am I ?Wim Godden (@wimgtr)Founder of Cu.be Solutions (http://cu.be)Open Source developer since 1997Developer of OpenX, PHPCompatibility, Nginx extensions, ...Zend / ZF Certified EngineerMySQL Certified DeveloperSpeaker at PHP and Open Source conferences
  3. 3. Who are you ?Developers ?System/network engineers ?Managers ?Caching experience ?
  4. 4. Goals of this tutorialEverything about caching and tuningA few techniquesHow-toHow-NOT-to→ Increase reliability, performance and scalability5 visitors/day → 5 million visitors/day(Dont expect miracle cure !)
  5. 5. LAMP
  6. 6. Architecture
  7. 7. 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
  8. 8. Our base benchmarkApachebench = useful enoughResult ?Single webserver ProxyStatic PHP Static PHPApache + PHP 3900 17.5 6700 17.5Limit :CPU, networkor diskLimit :database
  9. 9. CachingCaching
  10. 10. What is caching ?CACHECACHE
  11. 11. What is caching ?x = 5, y = 2n = 50Same resultCACHECACHEselect*fromarticlejoin useron article.user_id = user.idorder bycreated desclimit10Doesnt changeall the time
  12. 12. Theory of cachingDBCache$data = get(key)falseGET /pagePageselect data fromtable$data = returned resultset(key, $data)if ($data == false)
  13. 13. Theory of cachingDBCacheHIT
  14. 14. Caching goals - 1stgoalReduce # of concurrent requestReduce the load
  15. 15. Caching goals - 2ndgoal
  16. 16. Some figuresPageviews : 5000 (4000 on 10 pages)Avg. loading time : 200msCache 10 pagesAvg. loading time : 20ms→ Total avg. loading time : 56msWorth it ?
  17. 17. Caching goals - 3rdgoalSend less data across the network / InternetYou benefit → lower bill from upstream providerUsers benefit → faster page loadWait a second... thats mostly frontend stuff !
  18. 18. Caching techniques#1 : Store entire pagesCompany WebsitesBlogsFull pages that dont changeRender → Store in cache → retrieve from cache
  19. 19. Caching techniques#2 : Store parts of a pageMost common techniqueUsually a small block in a pageBest effect : reused on lots of pagesCan be inserted on dynamic pages
  20. 20. Caching techniques#3 : Store SQL queries↔ SQL query cacheLimited in size
  21. 21. Caching techniques#3 : Store SQL queries↔ SQL query cacheLimited in sizeResets on every insert/update/deleteServer and connection overheadGoal :not to get rid of DBfree up DB resources for more hits !Better :store returned objectstore group of objects
  22. 22. Caching techniques#4 : Store complex PHP resultsNot just calculationsCPU intensive tasks :Config file parsingXML file parsingLoading CSV in an arraySave resources → more resources available
  23. 23. Caching techniques#xx : Your callOnly limited by your imagination !When you have data, think :Creating time ?Modification frequency ?Retrieval frequency ?
  24. 24. How to find cacheable dataNew projects : start from cache everythingExisting projects :Look at MySQL slow query logMake a complete query log (dont forget to turn it off !)→ Use Percona Toolkit (pt-query-digest)Check page loading times
  25. 25. Caching storage - MySQL query cacheUse itDont rely on itGood if you have :lots of readsfew different queriesBad if you have :lots of insert/update/deletelots of different queries
  26. 26. Caching storage - Database memory tablesTables stored in memoryIn MySQL : memory/heap table↔ temporary table :memory tables are persistenttemporary tables are session-specificFaster than disk-based tablesCan be joined with disk-based tablesBut :default 16MByte limitmaster-slave = troubleif you dont need join → overhead of DB softwareSo : dont use it unless you need to join
  27. 27. Caching storage - Opcode cachingDO !
  28. 28. Caching storage - Opcode cachingAPCDe-facto standardWill be in PHP core in 5.4 ? 5.5 ? 6.0 ?PECL or packages
  29. 29. Caching storage - Opcode cachingZend Optimizer+Will be in PHP core in 5.5APCeAccelerator
  30. 30. Caching storage - Opcode cachingZend Optimizer+Will be in PHP core in 5.5APCeAcceleratorPHP PHP + Zend O+ PHP + APC42.18 req/sec 206.20 req/sec 211.49 req/sec
  31. 31. Caching storage - DiskData with few updates : goodCaching SQL queries : preferably notDONT use NFS or other network file systemshigh latencypossible problem for sessions : locking issues !
  32. 32. Caching storage - Memory disk (ramdisk)Usually faster than physical diskBut : OS file caching makes difference minimal(if you have enough memory)
  33. 33. Caching storage - Disk / ramdiskOverhead : filesystemLimited number of files per directory→ SubdirectoriesLocal5 Webservers → 5 local cachesHow will you keep them synchronized ?→ Dont say NFS or rsync !
  34. 34. 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
  35. 35. 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 (!)
  36. 36. Memcache - where to install
  37. 37. Memcache - where to install
  38. 38. Memcache - installation & running itInstallationDistribution packagePECLWindows : binariesRunningNo config-filesmemcached -d -m <mem> -l <ip> -p <port>ex. : memcached -d -m 2048 -l 172.16.1.91 -p 11211
  39. 39. Caching storage - Memcache - some notesNot fault-tolerantIts a cache !Lose session dataLose shopping cart data...
  40. 40. Caching storage - Memcache - some notesNot fault-tolerantIts a cache !Lose session dataLose shopping cart data…Different librariesOriginal : libmemcacheNew : libmemcached (consistent hashing, UDP, binary protocol, …)Firewall your Memcache port !
  41. 41. Memcache in code<?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;Careful : false is a valid value !
  42. 42. Memcache in code<?php$memcache = new Memcache();$memcache->addServer(172.16.0.1, 11211);$memcache->addServer(172.16.0.2, 11211);$myData = $memcache->get(myKey);if ($memcache->getResultCode() == Memcached::RES_NOTSTORED) {$myData = GetMyDataFromDB();// Put it in Memcache as myKey, without compression, with no expiration$memcache->set(myKey, $myData, false, 0);}echo $myData;
  43. 43. Benchmark with MemcacheSingle webserver ProxyStatic PHP Static PHPApache + PHP 3900 17.5 6700 17.5Apache + PHP + MC 3900 55 6700 108
  44. 44. Wheres the data ?Memcache client decides (!)2 hashing algorithms :TraditionalServer failure → all data must be rehashedConsistentServer failure → 1/x of data must be rehashed (x = # of servers)No replication !
  45. 45. Memcached ? Couchbase ? Redis ? … ?Memcached Couchbase RedisPurpose Key-value store Document store Key-value storeAdditional indexes No Yes NoReplication No Master-masterMaster-slaveMaster-slavePersistency No Yes YesInteresting read : http://kkovacs.eu/cassandra-vs-mongodb-vs-couchdb-vs-redis(Cassandra vs MongoDB vs CouchDB vs Redis vs Riak vs HBase vs Couchbasevs Neo4j vs Hypertable vs ElasticSearch vs Accumulo vs VoltDB vs Scalariscomparison)
  46. 46. Memcache slabs(or why Memcache says its full when its not)Multiple slabs of different sizes :Slab 1 : 400 bytesSlab 2 : 480 bytes (400 * 1.2)Slab 3 : 576 bytes (480 * 1.2) (and so on...)Multiplier (1.2 here) can be configuredStore a lot of very large objects→ Large slabs : full→ Rest : free→ Eviction of data !
  47. 47. Memcache - Is it working ?Connect to it using telnet"stats" command →Use Cacti or other monitoring toolsSTAT pid 2941STAT uptime 10878STAT time 1296074240STAT version 1.4.5STAT pointer_size 64STAT rusage_user 20.089945STAT rusage_system 58.499106STAT curr_connections 16STAT total_connections 276950STAT connection_structures 96STAT cmd_get 276931STAT cmd_set 584148STAT cmd_flush 0STAT get_hits 211106STAT get_misses 65825STAT delete_misses 101STAT delete_hits 276829STAT incr_misses 0STAT incr_hits 0STAT decr_misses 0STAT decr_hits 0STAT cas_misses 0STAT cas_hits 0STAT cas_badval 0STAT auth_cmds 0STAT auth_errors 0STAT bytes_read 613193860STAT bytes_written 553991373STAT limit_maxbytes 268435456STAT accepting_conns 1STAT listen_disabled_num 0STAT threads 4STAT conn_yields 0STAT bytes 20418140STAT curr_items 65826STAT total_items 553856STAT evictions 0STAT reclaimed 0
  48. 48. Memcache - backing up
  49. 49. Memcache - deleting<?php$memcache = new Memcache();$memcache->delete(myKey);
  50. 50. Memcache - caching a page<?php$output = $memcache->get(page_ . $page_id);if ($output === false) {ob_start();GetMyPageInRegularWay($page_id);$output = ob_get_contents();ob_end_clean();$memcache->set(page_ . $page_id, $output, false, 600); // Cache 10 mins}echo $output;
  51. 51. Memcache - tipPage with multiple blocks ?→ use Memcached::getMulti()But : what if you get some hits and some misses ?getMulti($array)Hashingalgorithm
  52. 52. Naming your keysKey names must be uniquePrefix / namespace your keys !Only letters, numbers and underscoreWhy ? → Change caching layermd5() is useful→ BUT : harder to debugUse clear namesDocument your key names !
  53. 53. Updating data
  54. 54. Updating dataLCD_Popular_Product_List
  55. 55. Adding/updating data$memcache->delete(ArticleDetails__Toshiba_32C100U_32_Inch);$memcache->delete(LCD_Popular_Product_List);
  56. 56. Adding/updating data
  57. 57. Adding/updating data - Why it crashed
  58. 58. Adding/updating data - Why it crashed
  59. 59. Adding/updating data - Why it crashed
  60. 60. Cache stampeding
  61. 61. Cache stampeding
  62. 62. Memcache code ?DBVisitor interface Admin interfaceMemcache code
  63. 63. Cache warmup scriptsUsed to fill your cache when its emptyRun it before starting Webserver !2 ways :Visit all URLsError-proneHard to maintainCall all cache-updating methodsMake sure you have a warmup script !
  64. 64. Cache stampeding - what about locking ?Seems like a nice idea, but...While lock in placeWhat if the process that created the lock fails ?
  65. 65. Quick word about expirationGeneral rule : dont let things expireException to the rule : things that have an end date (calendaritems)
  66. 66. So...DONT DELETE FROM CACHE&DONT EXPIRE FROM CACHE(unless you know youll never store it again)
  67. 67. Time for...a break (15 min)After the break :Byebye ApacheReverse proxyingThe importance of frontendlots of hints & tips...
  68. 68. LAMP...→ LAMMP→ LANMMP
  69. 69. NginxWeb serverReverse proxyLightweight, fast12.81% of all Websites
  70. 70. NginxNo threads, event-drivenUses epoll / kqueueLow memory footprint10000 active connections = normal
  71. 71. Nginx - a true alternative to Apache ?Not all Apache modulesmod_auth_*mod_dav*…Basic modules are availableLots of 3rdparty modules (needs recompilation !)
  72. 72. Nginx - InstallationPackagesWin32 binaries→ Not for production !Build from source (./configure; make; make install)
  73. 73. Nginx - Configurationserver {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;}
  74. 74. Nginx - phase 1Move Apache to a different port (8080)Put Nginx at port 80Nginx serves all statics (images, css, js, …)Forward dynamic requests to Apache
  75. 75. Nginx for static files onlyserver {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;}}
  76. 76. Nginx for PHP ?Bottleneck = PHP ? Keep it in ApacheBottleneck = memory ? Go for it !LANMMP to... LNMPP(ok, this is getting ridiculous)
  77. 77. Nginx with PHP-FPMSince PHP 5.3.3Runs on port 9000Nginx connects using fastcgi methodlocation / {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.domain.ext/$fastcgi_script_name;fastcgi_param SERVER_NAME $host;fastcgi_intercept_errors on;}
  78. 78. Nginx + PHP-FPM featuresGraceful upgradeSpawn new processes under high loadChrootSlow request log !
  79. 79. Nginx + PHP-FPM featuresGraceful upgradeSpawn new processes under high loadChrootSlow request log !fastcgi_finish_request() → offline processing
  80. 80. Nginx + PHP-FPM - performance ?Single webserver ProxyStatic PHP Static PHPApache + PHP 3900 17.5 6700 17.5Apache + PHP + MC 3900 55 6700 108Nginx + PHP-FPM + MC 11700 57 11200 112Limit :single-threadedApachebench
  81. 81. Nginx + PHP-FPM - performance ?Single webserver ProxyStatic PHP Static PHPApache + PHP 3900 17.5 6700 17.5Apache + PHP + MC 3900 55 6700 108Nginx + PHP-FPM + MC 11700 57 11200 112Apache (tuned) +PHP/MC10600 55 11400 108Limit :single-threadedApachebench
  82. 82. Reverse proxy time...
  83. 83. VarnishNot just a load balancerReverse proxy cache / http accelerator / …Caches (parts of) pages in memoryCareful :uses threads (like Apache)Nginx usually scales better (but doesnt have VCL)
  84. 84. Varnish - Installation & configurationInstallationPackagesSource : ./configure && make && make installConfiguration/etc/default/varnish/etc/varnish/*.vcl
  85. 85. Varnish - backends + load balancingbackend server1 {.host = "192.168.0.10";}backend server2 {.host = "192.168.0.11";}director example_director round-robin {{.backend = server1;}{.backend = server2;}}
  86. 86. Varnish - backends + load balancingbackend server1 {.host = "192.168.0.10";.probe = {.url = "/";.interval = 5s;.timeout = 1 s;.window = 5;.threshold = 3;}}
  87. 87. Varnish - VCLVarnish Configuration LanguageDSL (Domain Specific Language)→ compiled to CHooks into each requestDefines :Backends (web servers)ACLsLoad balancing strategyCan be reloaded while running
  88. 88. Varnish - whatever you wantReal-time statistics (varnishtop, varnishhist, ...)ESI
  89. 89. Article content page/page/732Article content (TTL : 15 min)/article/732Varnish - ESIPerfect for caching pagesHeader (TTL : 60 min)/topLatest news (TTL : 2 min) /newsNavigation(TTL :60 min)/navIn your /page/732 output :<esi:include src="/top"/><esi:include src="/nav"/><esi:include src="/news"/><esi:include src="/article/732"/>In your Varnish config :sub vcl_fetch {if (req.url == "/news") {esi; /* Do ESI processing */set obj.ttl = 2m;} elseif (req.url == "/nav") {esi;set obj.ttl = 1m;} elseif ….….}
  90. 90. Varnish with ESI - hold on tight !Single webserver ProxyStatic PHP Static PHPApache + PHP 3900 17.5 6700 17.5Apache + PHP + MC 3900 55 6700 108Nginx + PHP-FPM + MC 11700 57 11200 112Varnish - - 11200 4200
  91. 91. Varnish - what can/cant be cached ?Can :Static pagesImages, js, cssPages or parts of pages that dont change often (ESI)Cant :POST requestsVery large files (its not a file server !)Requests with Set-CookieUser-specific content
  92. 92. ESI → no caching on user-specific content ?Logged in as : Wim Godden5 messagesTTL = 5minTTL=1hTTL = 0s ?
  93. 93. Coming soon...Based on NginxReduces load by 50 – 95%Requires code changes !Well-built project → few changesEffect on webservers and database servers
  94. 94. Requesting /page (1sttime)NginxShared memory1234/page/page
  95. 95. Requesting /page ESI subrequests (1sttime)Nginx123/menu/news/top (in ESI session)
  96. 96. Requesting /page (next time)NginxShared memory12/page/menu/news/top (in ESI session)/page
  97. 97. New message is sent...POST /sendDBinsert into...set(...)top (in ESI session)
  98. 98. AdvantagesNo repeated GET hits to webserver anymore !At login : POST → warm up the cache !No repeated hits for user-specific contentNot even for non-specific content
  99. 99. News addedaddnews() methodDBinsert into...set(...)Memcache key /news
  100. 100. How many Memcache requests ?Logged in as : Wim Godden5 messages<scl:include key="news" src="/news" ttl="5m" /><scl:includekey="menu"src="/menu"ttl="1h" /><scl:include key="top" src="/top" session="true" ttl="1h" />
  101. 101. First release : ESIPart of the ESI 1.0 specOnly relevant features implementedExtension for dynamic session supportBut : unavailable for copyright reasons
  102. 102. Rebuilt from scratch : SCLSession-specific Caching LanguageLanguage details :Control structures : if/else, switch/case, foreachVariable handlingStrings : concatenation, substring, ...
  103. 103. Whats the result ?
  104. 104. FiguresSecond customer (already using Nginx + Memcache) :No. of web servers : 72 → 8No. of db servers : 15 → 4Total : 87 → 12 (86% reduction !)Latest customer :Total no. of servers : 1350 → 38072% reduction → €1.5 million / year
  105. 105. A real example : vBulletinDB Server Load Web Server Load Max Requests/sec (1 = 282)05101520253035Standard installWith MemcachedNginx + SCL + memcached
  106. 106. AvailabilityGood news :It will become Open SourceIts solid : stable at 3 customers, being installed at 1 moreBad news :First customer holds copyrightsTotal rebuild→ Open Source releaseBeta : Sep 2013Final : End 2013 (on Github !)
  107. 107. Time to tune...
  108. 108. PHP speed - some tipsUpgrade PHP - every minor release has 5-15% speed gain !Use an opcode cache (Zend O+, APC, eAccelerator, XCache)Profile your codeXHProfXdebugBut : turn off profilers on acceptance/production platforms !
  109. 109. KCachegrind is your friend
  110. 110. PHP speed - some tipsMost performance issues are in DB queries → look there first !Log PHP errors and review those logs !Shorter code != faster code → keep your code readable !Hardware cost < Manpower cost→ 1 more server < 30 mandays of laborKeep micro-optimizations in code = last thing on list
  111. 111. Apache - tuning tipsDisable unused modules → fixes 10% of performance issuesSet AllowOverride to None. Enable only where needed !Disable SymLinksIfOwnerMatch. Enable only where needed !MinSpareServers, MaxSpareServers, StartServers, MaxClients,MPM selection → a whole session of its own ;-)Dont mod_proxy → use Nginx or Varnish !High load on an SSL-site ? → put SSL on a reverse proxy
  112. 112. DB speed - some tipsAvoid dynamic functionsEx. : select id from calendar where startDate > curdate()Better : select id from calendar where startDate > "2013-05-14"Use same types for joinsi.e. dont join decimal with int, bigint with int, etc.RAND() is evil !count(*) is evil in InnoDB without a where clause !Persistent connect is sort-of evilIndex, index, index !→ But only on fields that are used in where, order by, group by !
  113. 113. Caching & Tuning @ frontendhttp://www.websiteoptimization.com/speed/tweak/average-web-page/
  114. 114. Caching in the browserHTTP 304 (Not modified)Expires/Cache-Control header2 notes :Dont use POST if you want to cacheDont cache user-specific pages in browser (security !)
  115. 115. HTTP 304Browser ServerNo headerLast Modified: Fri 28 Jan 2011 08:31:01 GMTIf-Modified-Since: Fri 28 Jan 2011 08:31:01 GMT200 OK / 304 Not ModifiedFirst requestNext requests
  116. 116. HTTP 304 with ETagBrowser ServerNo headerEtag: 8a53321-4b-43f0b6dd972c0If-None-Match: 8a53321-4b-43f0b6dd972c0200 OK / 304 Not ModifiedFirst requestNext requests
  117. 117. Expires/Cache-control headerCache-ControlHTTP/1.1Seconds to expiryUsed by browsersBrowser ServerNo headerExpires: Fri 29 Nov 2011 12:11:08 GMTCache-Control: max-age=86400First requestNext requests No requests until item expiresExpiresHTTP/1.0Date to expire onUsed by old proxiesRequires clock to be accurate !
  118. 118. Pragma: no-cache = evil"Pragma: no-cache" doesnt make it uncacheableDont want caching on a page ?HTTP/1.0 : "Expires : Fri, 30 Oct 1998 00:00:00 GMT" (in the past)HTTP/1.1 : "Cache-Control: no-store"
  119. 119. Frontend tuning1. You optimize backend2. Frontend engineers messes up → havoc on backend3. Dont forget : frontend sends requests to backend !SO...Care about frontendTest frontendCheck what requests frontend sends to backend
  120. 120. Tuning frontendMinimize requestsCombine CSS/JavaScript filesUse inline images in CSS/XHTML (not supported on all browsers yet)
  121. 121. 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=0src="data:image/gif;base64,R0lGODlhHwAfAPcAAAAAAIxKAKVjCLW1tb29tcbGvc7OxtZ7ANbWztbW1tbe1t7e1uelMefn1ufn3ufn5+fv3u+MAO/v5+/v7/fGCPf35/f37//nY/......Uaz0TgXnX2umVFyGakJUUAAADs=">
  122. 122. Tuning frontendMinimize requestsCombine CSS/JavaScript filesUse inline images in CSS/XHTML (not supported on all browsers yet)Use CSS Sprites
  123. 123. CSS Sprites
  124. 124. Tuning content - CSS sprites
  125. 125. Tuning content - CSS sprites11 images11 HTTP requests24KByte1 image1 HTTP requests14KByte
  126. 126. Tuning frontendMinimize requestsCombine CSS/JavaScript filesUse inline images in CSS/XHTML (not supported on all browsers yet)Use CSS Sprites (horizontally if possible)Put CSS at topPut JavaScript at bottomMax. no connectionsEspecially if JavaScript does Ajax (advertising-scripts, …) !Avoid iFramesAgain : max no. of connectionsDont scale images in HTMLHave a favicon.ico (dont 404 it !)→ see my blog
  127. 127. Tuning frontendDont use inline CSS/JavaScriptCSS/JavaScript need to be external files (minified, merged)Why ? → Cacheable by browser / reverse proxyUse GET for Ajax retrieval requests (and cache them !)Optimize images (average 50-60% !)Split requests across subdomainsPut statics on a separate subdomain (without cookies !)www.domain.extMax. 2requestswww.domain.extMax. 2requestsMax. 2requestsimages.domain.ext
  128. 128. Tuning miscellaneousAvoid DNS lookupsFrontend : dont use too many subdomains (2 = ideal)Backend :Turn off DNS resolution in Apache : HostnameLookups OffIf your app uses external dataRun a local DNS cache (timeout danger !)Make sure you can trust DNS servers (preferable run your own)Compress non-binary content (GZIP)mod_deflate in ApacheHttpGzipModule in Nginx (HttpGzipStaticModule for pre-zipped statics !)No native support in Varnish
  129. 129. What else can kill your site ?Redirect loopsMultiple requestsMore load on WebserverMore PHP to processAdditional latency for visitorTry to avoid redirects anyway→ In ZF : use $this->_forward instead of $this->_redirectWatch your logs, but equally important...Watch the logging process →Logging = disk I/O → can kill your server !
  130. 130. Above all else... be prepared !Have a monitoring systemUse a cache abstraction layer (disk → Memcache)Dont install for the worst → prepare for the worstHave a test-setupHave fallbacks→ Turn off non-critical functionality
  131. 131. So...CacheBut : never delete, always push !Have a warmup scriptMonitor your cacheHave an abstraction layerApache = fine, Nginx = betterStatic pages ? Use VarnishTune your frontend → impact on backend !
  132. 132. Questions ?
  133. 133. Questions ?
  134. 134. ContactTwitter @wimgtrWeb http://techblog.wimgodden.beSlides http://www.slideshare.net/wimgE-mail wim.godden@cu.bePlease...Rate my talk : http://joind.in/8195
  135. 135. Thanks !Please...Rate my talk : http://joind.in/8195

×