Available for Hire!

ProgFu

programming tips & tricks

Dec 23

Sinatra and SQLite on 64MB RAM

This article is a direct followup of Ruby on 64MB RAM. If you didn’t read it, please do it now.

No MySQL

Since we’re running on very low resources, we need to think carefuly about every process we want to run on our server.

First database that comes to mind is MySQL. We could probably squeeze it somewhere in the remaining 40MB RAM, but is that really worth it? Our application obviously doesn’t have to worry about scaling, so let go with SQLite.

No Apache or nginx

Same logic will apply to Apache, there’s literally room for it. Instead of Apache, we’re going to use Thin, so let’s install it now.

$ gem install thin
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed eventmachine-0.12.10
Successfully installed thin-1.2.7
2 gems installed

Before we get into SQLite, let’s try to do some benchmark on Thin and see how much faster it is. First we need to change our application to use config.ru file.

# config.ru
require 'rubygems'
require 'sinatra'
require 'app'

run Sinatra::Application

# app.rb
get '/' do
  "Hello, World!"
end

Now we need to start thin

$ thin start -R config.ru 
>> Thin web server (v1.2.7 codename No Hup)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop

and yet again, nuke it from the host machine, this time with much bigger firepower.

# Again substitute 10.0.0.4 for your guest machine's IP
$ ab -n 10000 -c 100 http://10.0.0.4:3000/ | grep Requests
Completed 1000 requests
...    
Finished  10000 requests

Requests per second:    1080.52 [#/sec] (mean)

Let’s compare this to the result from the previous article, which got us

Requests per second:    7.50 [#/sec] (mean)

Even though this benchmark is hardly accurate, the difference between 7.5 and 1000 is big enough to see that we’re on the right track.

SQLite with DataMapper

It’s time to create a simple application that will allow its users to post their christmas wishes. We’re going to use the DataMapper gem, since it is very easy to set up.

$ gem install datamapper dm-sqlite-adapter

We’re not using Bundler here, because it consumes quite a lot of RAM while installing gems.

Since this is not Sinatra tutorial, I’m not going to cover a step by step creation of the application

We have a simple Sinatra application with one Data Mapper model. In the configure block, we’re also going to insert some records to our database. Let’s see how our application performs one last time:

Don’t forget to restart thin to load the new, updated application.

$ ab -n 10000 -c 100 http://10.0.0.4:3000/ | grep Requests
Completed 1000 requests
...
Finished 10000 requests
Requests per second:    300.12 [#/sec] (mean)

Now that’s quite an interesting result. While we dropped to one third from the last benchmark, it’s important to note that we added a database. We’re also not implementing any cache - yet.

And we’re still running on only 64MB RAM, isn’t that at least a little impressive?


  1. progfu posted this
Page 1 of 1