Archive for December, 2007

Why Rails is better than whatever it is you use: ToDo List in 5 Lines

Tuesday, December 18th, 2007

Rob Mayhew prepared this ToDo List Tutorial

  1. rails todo
  2. cd todo
  3. rake db:create:all
  4. script/generate scaffold Todo title:string body:text done:boolean due:datetime
  5. rake db:migrate

Update: This code pretty much also provides you with a REST API.

Amazon SimpleDB: death of the database?

Saturday, December 15th, 2007

With the announcement of Amazon SimpleDB Scoble has asked if Amazon Web Services are going to kill MySQL and Oracle.

I think the short answer is no, but the game is changing rapidly, and Amazon is at the vanguard.

SimpleDB basically acts like a big structured bucket.
The Model is represented by Domains. A Domain can have Attributes. Attributes are key/value pairs.

There is no schema attached to Domains, you can PUT any combination of attribute-values in the domain:
PUT (item, 123), (description, sweater), (color, blue), (color, red)
PUT (item, 456), (description, dress shirt), (color, white), (color, blue)
PUT (item, 789), (description, shoes), (color, black), (material, leather)

The real issue here is that developers are strongly tied to the RDBMS. Frameworks assume you are running on top of a relational database, so there is an instant barrier to entry. However, I suspect it won’t be long before the first set of libraries and drivers for SimpleDB are developed.

The much bigger issues are in the features that SimpleDB doesn’t have:

  • No ACID
  • No transactions (see ACID, above)
  • No relationships
  • No data integrity
  • No SQL
  • Latency

SQL is actually pretty incredible - it makes accessing structured data very simple. If you’ve ever played with other types of data store you miss SQL almost instantly (Prevayler, anyone?).

All of that said, Amazon continues to lead the way with Web Services. Consider SimpleDB a warning shot to the database incumbents.

Facebook How-To: scheduling profile updates

Monday, December 10th, 2007

Several of the Facebook Applications I have developed have required scheduling updates to Facebook User Profiles.

The process for scheduling updates is a little involved, but basically involves using a Session Key to push data to each User’s Profile.

  • Store the user’s Session Key.
  • Schedule an update with Cron
  • User the User ID and Session Key to create a Facebook Client Object make a Facebook API call.

The following samples are from a CakePHP project I recently completed and use the Facebook PHP Library. I’ve expanded the SQL so you have a better idea of what is happening behind the scenes.

Store the Session Key:

function beforeFilter() {
$this->facebook_user_id = $this->facebook->require_login();
$this->sessionKey = $this->facebook->api_client->session_key;
$this->Settings->query("UPDATE settings SET sessionKey = '{$this->sessionKey}' WHERE id = '{$this->facebook_user_id}'");
}

Each time the user hits the application I generally refresh the Session Key in my normal login/authentication process.

Make the API Call:

$data = $this->Settings->query("SELECT * FROM settings WHERE lastUpdated <= DATE_SUB( NOW(), INTERVAL 1 DAY) ORDER BY lastUpdated ASC LIMIT 1");
$id = $data[0]["settings"]["id"];
$sessionKey = $data[0]["settings"]["sessionKey"];
$this->facebook = new Facebook(FB_API_KEY, FB_SECRET);
$this->facebook->set_user($id, $sessionKey);
$this->facebook_user_id = $id;
$this->facebook->api_client->profile_setFBML($fbml);

This code grabs the first Faccbook User from the database who last updated a day ago and then creates a Facebook Client using the Facebook User Id and Session Key to make an API Call.

Note: When scheduling updates I generally use Cron to call a URL on my application with wget or Curl. Using this technique you can leverage the existing logic of your app and don’t have to write a standalone script.

Rails 2.0 has been released

Saturday, December 8th, 2007

Oh yes: Rails 2.0 has been released

For some tips on moving your codebase to version 2.0:

I’ll be moving some of my projects and will be blogging about my experiences.

Help Wanted: Advanced Slicehost with Capistrano

Monday, December 3rd, 2007

I’m looking for someone to help me with some advanced server setup adminstration.

I have configured a Slicehost VPS to deploy my applications, but I need to get MySQL Replication working, along with some Capistrano/Deprec Recipes to automate deployment and configuration.

If anyone is interested in some paid work, drop me a line:

Performance testing Mongrel and Rails with httperf and autobench

Saturday, December 1st, 2007

I posted recently about my experiences with Slicehost, Capistrano and Deprec.

As part of my approach for migrating the Top Secret Project to Slicehost, I’ve been testing my new VPS using httperf and autobench.

Maybe it’s just me, but performance testing and statistics is a whole lot of fun.

My 256 Slice running Apache proxied to two Mongrel instances peaks at about 41.6 requests/second, but can handle about 40 req/sec as consistent average. The single instance averages about 15 req/sec but can burst up to 30.

I think 40 requests/second is more than enough for my needs at the moment … it provides a pretty good peak capacity, and with Slicehost you can always get more resources when you need them - running extra Mongrels is wonderfully straight forward, but can also create slice to act as a dedicated database host. The options are endless.

One useful lesson: your client connection makes a BIG difference to these tests.

On my laptop running a wireless ADSL connection, the results were all over the place. Running the tests from one of my servers with a much fatter pipe, gave consistent test results.
For more information on using httperf and autobench, this is your best friend: HTTP performance testing with httperf, autobench and openload

Capistrano, Deprec: Best Things Ever

Saturday, December 1st, 2007

I signed up for a slice on Slicehost a little while ago (there’s quite a waiting list, which is a good sign).

I’m planning on moving some stuff from a local provider to Slicehost, but wanted to experiment first.My current host is a CPanel VPS and it’s proved a little challenging to get that configured for Rails - CPanel doesn’t provide great support out-of-the-box, so it’s all manual, but with the added complexity fo having to work with CPanel’s configuration. I’ve only been able to get a single Mongrel instance running, because CPanel doesn’t support Apache 2.2.x.

System Administration is definitely the weakest part of my skill set - I have always been focussed on the code, and have had the advantage of working with dedicated sys admins - so configuring a VPS from scrach is a little daunting.

Enter Capistrano and Deprec to the rescue.

These tools automate the installation of your Rails stack and deployment of your applciation from Subversion (or GIT, or whatever is hot this week).

The basic steps:

cap setup_admin_account_as_root
cap setup_ssh_keys
cap install_rails_stack
cap deprec_setup
cap deploy_with_migrations
cap restart_apache

And that’s it, you’re done …

You bneed to create a deploy.rb file with your Apache and Mongrel configuration details and Cap will work the rest out.

It’s pretty incredible.

Slicehost has some detailed instructions for configuring Configuring Rails on Ubuntu using Capistrano and Deprec. The instructions pretty much worked out of the box for me.