Quick Optimization of CakePHP Application with YSlow

Posted on 25/5/09 by MalaMalca (2 comments)

I run a busy Slovenian site featuring weather radar image on Google maps - vreme.malamalca.com . It is a simple CakePHP app which collects weather and radar data, stores it in cache and outputs it in friendly format.

Recently my site is receiving some heavy traffic due to high probability of hail storms which are result of high temperatures - around 35° (95°F). Everyone wants to check if their car /or home/ is in immediate danger. Bad weather is causing high spikes of traffic and sluggish response times.

After checking my site with YSlow, it got "F" grade. My site needed some improvements. There are some steps I took which got my site to grade "B" of YSlow:

  1. I changed my cache engine to APC. This had a tremendous effect in responsiveness of my app (although it is not graded by YSlow)
  2. Next step was implementation of some Apache directives in .htacces in /webroot folder:

Here are new .htaccess contents:

	RewriteEngine On
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

	# compress content with type html, text, and css
	AddOutputFilterByType DEFLATE text/plain
	AddOutputFilterByType DEFLATE text/html
	AddOutputFilterByType DEFLATE text/xml
	AddOutputFilterByType DEFLATE text/css
	AddOutputFilterByType DEFLATE text/javascript
	AddOutputFilterByType DEFLATE application/xml
	AddOutputFilterByType DEFLATE application/xhtml+xml
	AddOutputFilterByType DEFLATE application/rss+xml
	AddOutputFilterByType DEFLATE application/javascript
	AddOutputFilterByType DEFLATE application/x-javascript

	BrowserMatch ^Mozilla/4 gzip-only-text/html
	BrowserMatch ^Mozilla/4\.0[678] no-gzip
	BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

	#AddOutputFilter DEFLATE js css
	# properly handle requests coming from behind proxies
	Header append Vary User-Agent

	ExpiresActive On
	ExpiresByType application/javascript "access plus 10 years"
	ExpiresByType text/css "access plus 10 years"
	ExpiresByType text/js "access plus 10 years"
	ExpiresByType text/javascript "access plus 10 years"
	ExpiresByType application/x-javascript "access plus 10 years"
	ExpiresByType image/png "access plus 10 years"
	ExpiresByType image/gif "access plus 10 years"
	ExpiresByType image/jpeg "access plus 10 years"

	FileETag none

These adjustments made my site usable again even in spikes of 500 users per minute.

Note: some aditional warnings... this directives extend duration of cached items in your browser. You should know what are you trying to achieve.

Comments

1 Lilblogs said on 24/6/09:

That's a whole different story. Besides, mod_deflate is used maily on static files.

2 m said on 18/6/09:

Hi. Why do you use mod_expires instead of CakePhp's built-in cache?

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to contact us.