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
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.
7. Network (SSL/TLS)
Session Tickets - 8 packets 3 roundtrips before application data
EDH crypto – computational expensive by has perfect forwards secrecy
8. Network (HTTP keep-alive)
● Request / Response header
Connection:keep-alive
On by default. Can't handle variable length pages
13. 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
20. 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 rate
if ($sample) $t0 = microtime(TRUE);
..bits of code
if ($sample) $t1 = microtime(TRUE) - $t0;
..
if ($sample) $t2 = microtime(TRUE) - $t1;
...
if ($sample) {
insert into timings .....$t1*1000, $t2*1000....
}
21. Application Caching
● Be strategic – cost / benefit
● Hard to generate – graphs, database queries,
complex maths
● Frequent pages: front pages, rss, ajax
22. 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.
23. 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 ]] )
24. 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)
26. Database
● Know what indexes are and which queries aren't
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.
27. Database (continued)
● Searching text in SQL – Use Sphinxsearch
instead
● Same sql interfaces, pulls off database – much
quicker at full text search
28. 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