Caching for Cash, part 4 DPC 2009

Embed Size (px)

Citation preview

Caching For Cash

Dutch PHP Conference Amsterdam, June 2009

Pareto Principle

80 / 20 Rule

80 / 20 Rule

20% of sales people produce 80% of sales

80 / 20 Rule

20% of the population receives 80% of the income

80 / 20 Rule

20% of the causes produce 80% of the effects

The golden rule80% of response time is spent downloading content

PerceptionTrick the end user intothinking your site is fast!The users perception is more relevant than actual unload-to-onload response time.

Definition of "user onload" is undefined or varies from one web page to the next.

It all depends on how we look at things, and not how they are in themselves.

- Carl G. Jung

Perception40 60% of all users arrivewith an empty cacheUser needs to download all components

Coming for the first time

Force refresh of the site

Browser automagically cleared the cache based on settings

Perception

User needs to download all components

Coming for the first time

Force refresh of the site

Browser automagically cleared the cache based on settings

Key factor in performanceFewer HTTP RequestsEasy to improve on

HTTP Requests

Cookies

Sent with static contentSlow upstream speedBig cookies

Cookie SizeResponse Time (Delta)

0 bytes78 ms ( 0 ms)

500 bytes79 ms ( +1 ms)

1000 bytes94 ms (+16 ms)

1500 bytes109 ms (+31 ms)

2000 bytes125 ms (+47 ms)

2500 bytes141 ms (+63 ms)

3000 bytes156 ms (+78 ms)

Times are for page loads on DSL (~800 kbps).

Experimenting time!Yahoo ran the experiment.Ran against an empty page

Eliminate unnecessary cookiesKeep sizes lowAppropriate domain levelsSet Expires date appropriately

Cookies

Be aware of max connection limits in browsersCNAME to point multiple sub domains to the same IP

Maximize Parallel DownloadsOlder IE: 6 overall, 2 per host

Older FF had 24 as overall and 8 per host

Newer FF: 30 overall, 15 per host

One domain = 2 Parallel connections

Maximize Parallel Downloads

Two domains = 4 Parallel connections

Maximize Parallel Downloads

Too many hostnames can cause complications

2 4 sub domains is a good average

Maximize Parallel DownloadsMany host names = more DNS lookups, adds delay

Consider the effects of CPU thrashing for the client

Lookup times vary across ISPs and geographic locations

Combining 6 scripts into 1 eliminates 5 requests

Challenges: develop as separate modules

combinations vs. loading more than needed

Combine filesdynamically combine and cache

Combine and minimize before a release

JavascriptJS does not comply with the parallel downloading rule

script defer attribute is not a solutionblocks rendering and downloads in FF

slight blocking in IE

Solution: move them as low in the page as possible

Javascript

Javascript MinificationCrushes the filesStrips out all cruftVoodoo magic

Javascript MinificationJSMinDojo ShrinksafePackerYUI Compressor

http://short.ie/compression-rates

You can affect users download times

GzipGzip can be used on

JS

CSS

XML

JSON

GzipGzip should not be used on

Images

PDFs

Compressed data formats

404robots.txtfavicon

CSS Sprites

ImagesDon't use HTMTL attributes to scale

Favicon keep it around, keep it small

ImagesBadly optimizedThumbnailsPNG can be compacted 50%, no quality lossOptiPNG

Tread the users' pathTest on slow connections

Slow proxies for developers

Content Delivery NetworkData served through a single domainSmart routing softwareData closer to the end user Less latency

Content Delivery Network

Amazon CloudFront

Expires HeadersAppropriate datesCache far in the futureChange file names on releases

ETagsHash tag to compare between browser and serverGives more flexibility than last-modified-dateGreat on a single server setup

Hash like tag to compare browser cache against the server version

Only download if hash has changed

Gives more flexibility than last-modified date

Great on a single server setup

ProblemsTypically unique to a serverDisable if not being used!

Typically unique to a server

Default behavior in Apache and IIS

If hash don't match between server, then browser cache is not used

Proper Expires Headers won't help

Remove ETags if not being used to its fullest

FarmsUse options that are not unique per server

Firefox ExtensionsFirebugFirecookieYslowPage SpeedHammerhead

http://stevesouders.com/hammerhead/

http://code.google.com/speed/page-speed

Knowledge rests not upon truth alone, but upon error also.

- Carl G. Jung

HTMLImageImageImageImageImageImageImageImageImage0.40.81.21.622.4Seconds

???Page ??? (???)05/13/2009, 02:52:53Page / HTMLImageImageImageImageImageImageImageImageImage0.40.81.21.622.4Seconds

???Page ??? (???)05/13/2009, 02:52:53Page / HTMLJavaScriptImageImageImageImageImageImageImageImage0.40.81.21.622.4Seconds

???Page ??? (???)11/10/2008, 01:44:39Page /