Caching Mephisto with lighttpd and mod_magnet

Digg!
Posted by marco
Thu, 15 May 2008 00:15:00 GMT

This blog uses Mephisto, a rails application, and Thin as backend webserver. The frontend webserver is Lighttpd.

While Mephisto has a good caching system, producing a static html file for every visited page, the file is still served via Thin, reducing the speed and the scalability of the system and introducing an unnecessary step.

Lighttpd is very good at serving static files, thus I searched a way to serve a file from the cache directory of Mephisto, if present; I found a hack using mod_magnet.

Hereafter I wrote the relevant snippets of configuration files and how to put every piece in the right position.

Note: the same procedure should apply if you use Mongrel instead of Thin, for the relevant point is to bypass the backend rails webserver and serve all files (whether they exist) from the Mephisto cache.

First I configure Lighttpd to proxy to Thin for the rails application. I assume Thin (or Mongrel) is listening on port 3000 on two hosts.

  HTTP[''host"] == "virtualhost.example.org" {
        magnet.attract-physical-path-to = ( "/var/www/cache_mephisto.lua" ) 
        proxy.balance = "hash"
        proxy.server  = ( "" => ( ( "host" => "10.0.0.1", "port" =>  3000 ),
                                ( "host" => "10.0.0.2", "port" =>  3000 ) ) )
  }

The magnet.attract-physical-path-to = ( "/var/www/cache_mephisto.lua" ) is the magic trick. cache_mephisto.lua is a script written in lua programming language. The whole script is:

  local dir =  "/path/to/mephisto/public/cache/" .. lighty.request["Host"]

  if ( lighty.env["request.uri"] == nil or lighty.env["request.uri"] == "/") then
    file = dir .. "/index.html"
  else
    file = dir .. lighty.env["request.uri"] ..  ".html"
  end

  if lighty.stat(file) then
  lighty.header["Content-Type"] = "text/html"
  lighty.content = { { filename = file } }
  return 200
  end

 return lighty.RESTART_REQUEST

Inside the script you should adapt the variable dir to your Mephisto installation path. I need to append the virtual host name lighty.request["Host"] to the directory, for I run a multisite Mephisto installation; if you use a single site mephisto installation, probably you need to remove this append.

Now for some benchmarks:

  • serving the file via Thin:

       Requests per second:    1.40 [#/sec] (mean)
       Time per request:       716.007 [ms] (mean)
    
  • serving the file from the cache

       Requests per second:    11.46 [#/sec] (mean)
       Time per request:       87.247 [ms] (mean)
    

Almost a tenfold improvement!

Future works: serve from the cache the image files.

Mephisto database optimization

Digg!
Posted by marco
Tue, 26 Jun 2007 11:59:00 GMT

This blog runs on mephisto. In a standard installation, the database is not optimized. A few indexes (for postgresql, but I think that the could esaily adapted for other database engines) may optimize the performance (YMMV):

1
2
3
4
5
6
7
8
CREATE INDEX cached_pages_url ON cached_pages (site_id, url);
CREATE INDEX contents_type ON contents(site_id, type);
CREATE INDEX tags_name ON tags(name);
CREATE INDEX cached_pages_url2 ON cached_pages (site_id, url,id);
CREATE INDEX contents_pub_at ON contents(published_at);
CREATE INDEX contents_pub_at_type ON contents(published_at, type);
CREATE INDEX contents_perma_type ON contents(site_id,type,permalink, published_at);
CREATE INDEX contents_approv ON contents( article_id,approved, type );

Blog engine

Digg!
Posted by marco
Sun, 03 Dec 2006 23:41:00 GMT

Oh, I just forgot to say that I had to switch from the previous blog engine, ozimodo, to mephisto. Ozimodo was very apt to my needings and feelings, but now it is a necessity for us to have a more sophisticated blog engine.