Website Optimisation   CLUG March 2012
contents●   Network●   Compression●   HTTP Caching●   Structuring●   Measurement●   Application Caching●   Monitoring
contents                                 Client●   Network●   Compression●   HTTP Caching●   Structuring●   Measurement●  ...
Network mtr ­c 10 ­r www.openquery.comHOST: spaceman.localhost          Loss%   Snt   Last   Avg  Best  Wrst StDev  1.|­­ ...
Network (DNS)●    Check whois is showing the right nameserver    whois openquery.com    NS1.LINODE.COM    NS2.LINODE.COM  ...
Network (SSL/TLS)Session Tickets - 8 packets 3 roundtrips before application dataEDH crypto – computational expensive by h...
Network (HTTP keep-alive)●   Request / Response headerConnection:keep-aliveOn by default. Cant handle variable length pages
Compression●   Gzip/deflate●
Compression●   Apaches HTTPd    ●   Enable mod_deflate    <IfModule mod_deflate.c>        SetOutputFilter DEFLATE    </IfM...
Compression●   PHP    ●   Php.ini   zlib.output_compression On
HTTP Caching●   RFC 2616 Hypertext Transfer Protocol –    HTTP/1.1
HTTP Caching●   Etag    ●   Unique string – usual some form of digest    Browsers request with HTTP header:    If-None-Mat...
HTTP Caching (Etag)●   HTTPd    FileETag Mtime
HTTP Caching (Expires)●   HTTP Response header    Expires: Thu, 01 Dec 2012 16:00:00 GMT    Date:Thu, 22 Mar 2012 01:00:17...
HTTP Caching (Cache-Control)Cache-Control = "Cache-Control" ":" 1#cache-directivecache-directive = cache-request-directive...
Gzip + Caching                                            Savingstrunk/roundcubemail/.htaccess ¶             On initial re...
Use Yslow / Firebug                      Structuring
Measurement (Application)create table `timings` ( `id` int(4) NOT NULL AUTO_INCREMENT, host varchar(20),page varchar(120),...
Application Caching●   Be strategic – cost / benefit●   Hard to generate – graphs, database queries,    complex maths●   F...
Application Caching (duplicate)  if (!$db->count(name,            $name, DB_CACHE))      {          // if no cache entry e...
Application Caching●   Cache an entire page●   Memcache is your friend – RAM, autoexpire,    overload intelligent expire● ...
Application scaling●   If the application scales by adding webservers    you will save on hosting on high power servers●  ...
Application APCGraph from apc.php as part of the php_apc package
Database●   Know what indexes are and which queries arent    using them.●   EXPLAIN {sql expression} (Postgresql and Mysql...
Database (continued)●   Searching text in SQL – Use Sphinxsearch    instead●   Same sql interfaces, pulls off database – m...
Databases (application)●   Eventually one DB will be too little●   Plan to buy big hardware (Oracle) or plan to    use a r...
Monitoring
Monitoring●   Include resource usage●   Cpu / RAM / processes / forking / swap / IO /    Bandwidth...●   Web server – page...
Clug 2012 March   web server optimisation
Clug 2012 March   web server optimisation
Upcoming SlideShare
Loading in …5
×

Clug 2012 March web server optimisation

1,091 views

Published on

These slides show how to reduce latency on websites and reduce bandwidth for improved user experience.

Covering network, compression, caching, etags, application optimisation, sphinxsearch, memcache, db optimisation

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Clug 2012 March web server optimisation

  1. 1. Website Optimisation CLUG March 2012
  2. 2. contents● Network● Compression● HTTP Caching● Structuring● Measurement● Application Caching● Monitoring
  3. 3. contents Client● Network● Compression● HTTP Caching● Structuring● Measurement● Application Caching● Database tuning● Monitoring Server
  4. 4. Network mtr ­c 10 ­r www.openquery.comHOST: spaceman.localhost          Loss%   Snt   Last   Avg  Best  Wrst StDev  1.|­­ 192.168.1.1                0.0%    10    0.8   2.6   0.7  19.3   5.8  2.|­­ 192.168.3.254              0.0%    10    1.1   1.2   1.1   1.5   0.1  3.|­­ lns20.cbr1.internode.on.n  0.0%    10   22.6  22.7  22.3  23.2   0.2  4.|­­ gi0­0­2.cor3.cbr1.interno  0.0%    10   22.2  22.5  22.0  23.1   0.4  5.|­­ te6­0­0.bdr1.syd4.interno  0.0%    10  207.1 207.7 207.1 208.4   0.4  6.|­­ te0­0­0.bdr1.syd7.interno  0.0%    10  208.4 210.1 207.1 228.2   6.4  7.|­­ po3­0­0.bdr1.sjc2.interno  0.0%    10  207.5 207.2 206.7 207.9   0.4  8.|­­ 10gigabitethernet2­3.core  0.0%    10  185.9 187.0 185.0 190.7   2.0  9.|­­ 10gigabitethernet1­1.core  0.0%    10  186.3 205.2 186.0 367.5  57.1 10.|­­ linode­llc.10gigabitether 10.0%    10  186.1 189.1 185.4 210.8   8.1 11.|­­ bluefish.lentz.com.au     10.0%    10  186.5 186.6 186.0 187.9   0.6
  5. 5. Network (DNS)● Check whois is showing the right nameserver whois openquery.com NS1.LINODE.COM NS2.LINODE.COM NS3.LINODE.COM NS4.LINODE.COM NS1.CREATIVECONTINGENCIES.COM NS2.CREATIVECONTINGENCIES.COM NS3.CREATIVECONTINGENCIES.COM● Ensure correct record is returned by querying each nameserver● dig www.openquery.com @ns1.creativecontingencies.com
  6. 6. Network (SSL/TLS)Session Tickets - 8 packets 3 roundtrips before application dataEDH crypto – computational expensive by has perfect forwards secrecy
  7. 7. Network (HTTP keep-alive)● Request / Response headerConnection:keep-aliveOn by default. Cant handle variable length pages
  8. 8. Compression● Gzip/deflate●
  9. 9. Compression● Apaches HTTPd ● Enable mod_deflate <IfModule mod_deflate.c> SetOutputFilter DEFLATE </IfModule>● Lighty ● Enable “mod_compress” in server.modules● Nginx gzip on;
  10. 10. Compression● PHP ● Php.ini zlib.output_compression On
  11. 11. HTTP Caching● RFC 2616 Hypertext Transfer Protocol – HTTP/1.1
  12. 12. HTTP Caching● Etag ● Unique string – usual some form of digest Browsers request with HTTP header: If-None-Match: “uniquestring....”● Server response: 304 not modified if nothing changes
  13. 13. HTTP Caching (Etag)● HTTPd FileETag Mtime
  14. 14. HTTP Caching (Expires)● HTTP Response header Expires: Thu, 01 Dec 2012 16:00:00 GMT Date:Thu, 22 Mar 2012 01:00:17 GMT Last-Modified:Sun, 11 Mar 2012 20:55:16 GMT● HTTP Request If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
  15. 15. HTTP Caching (Cache-Control)Cache-Control = "Cache-Control" ":" 1#cache-directivecache-directive = cache-request-directive | cache-response-directivecache-request-directive = "no-cache" ; Section 14.9.1 | "no-store" ; Section 14.9.2 | "max-age" "=" delta-seconds ; Section 14.9.3, 14.9.4 | "max-stale" [ "=" delta-seconds ] ; Section 14.9.3 | "min-fresh" "=" delta-seconds ; Section 14.9.3 | "no-transform" ; Section 14.9.5 | "only-if-cached" ; Section 14.9.4 | cache-extension ; Section 14.9.6cache-response-directive = "public" ; Section 14.9.1 | "private" [ "=" <"> 1#field-name <"> ] ; Section 14.9.1 | "no-cache" [ "=" <"> 1#field-name <"> ]; Section 14.9.1 | "no-store" ; Section 14.9.2 | "no-transform" ; Section 14.9.5 | "must-revalidate" ; Section 14.9.4 | "proxy-revalidate" ; Section 14.9.4 | "max-age" "=" delta-seconds ; Section 14.9.3 | "s-maxage" "=" delta-seconds ; Section 14.9.3 | cache-extension ; Section 14.9.6
  16. 16. Gzip + Caching Savingstrunk/roundcubemail/.htaccess ¶ On initial response: 104.2K (vs 255.6K) / 44<IfModule mod_deflate.c> requests)SetOutputFilter DEFLATE On next page: 6.2K 2 requests (vs 204.7K / 44</IfModule> requests)<IfModule mod_headers.c># replace append with merge for Apache version 2.2.9 and laterHeader append Cache-Control public env=!NO_CACHE</IfModule><IfModule mod_expires.c>ExpiresActive OnExpiresDefault "access plus 1 month"</IfModule>FileETag MTime
  17. 17. Use Yslow / Firebug Structuring
  18. 18. Measurement (Application)create table `timings` ( `id` int(4) NOT NULL AUTO_INCREMENT, host varchar(20),page varchar(120), int t1,int t2, int t3.....)And in the index.php$sample = rand(0,100) < 5; # 5% sample rateif ($sample) $t0 = microtime(TRUE);..bits of codeif ($sample) $t1 = microtime(TRUE) - $t0;..if ($sample) $t2 = microtime(TRUE) - $t1;...if ($sample) { insert into timings .....$t1*1000, $t2*1000....}
  19. 19. Application Caching● Be strategic – cost / benefit● Hard to generate – graphs, database queries, complex maths● Frequent pages: front pages, rss, ajax
  20. 20. Application Caching (duplicate) if (!$db->count(name, $name, DB_CACHE)) { // if no cache entry exists, fetch the appropriate html and insert... } else { // row exists - determine if it is still valid $last_update = $db->getRecord(time, DB_CACHE, "name=$name") ; .... if (strtotime($last_update) >= strtotime($max_age)) // value is still valid - retrieve existing { $result = $db->getRecord(value, DB_CACHE, "name=$name") ;Fetching the same database entry three times.
  21. 21. Application Caching● Cache an entire page● Memcache is your friend – RAM, autoexpire, overload intelligent expire● Share between servers● Nice API in all languages ● e.g. PHP public mixed Memcached::get ( string $key [, callable $cache_cb [, float &$cas_token ]] )
  22. 22. Application scaling● If the application scales by adding webservers you will save on hosting on high power servers● Ensure application is stateless● Reverse proxy state-full bits to central server if needed● CDNs or separated servers for static content saves lots of cpu (latency, keep alives)
  23. 23. Application APCGraph from apc.php as part of the php_apc package
  24. 24. Database● Know what indexes are and which queries arent using them.● EXPLAIN {sql expression} (Postgresql and Mysql)● EXPLAIN ANALYSE {sql expression}● Slow query log – enable● Know differences between MySQL engines● Tune your server ● Defaults well out of proportion with your sever.
  25. 25. Database (continued)● Searching text in SQL – Use Sphinxsearch instead● Same sql interfaces, pulls off database – much quicker at full text search
  26. 26. Databases (application)● Eventually one DB will be too little● Plan to buy big hardware (Oracle) or plan to use a replicated database (for reads)● Some reads will need to occur on the master. The ones where the data was likely just changed very recently
  27. 27. Monitoring
  28. 28. Monitoring● Include resource usage● Cpu / RAM / processes / forking / swap / IO / Bandwidth...● Web server – pages/second, threads● Database – query cache hits/misses, connections, replication delay● Business value: page load time.● Alert if boundaries exceeded

×