Benjamin Curtis

Speculations on Web Development

Excessive Use of Redis

| Comments

Yesterday I read a post over at the EngineYard blog about a use case for Redis (in the name of being a polyglot, trying new things, etc.), and I just had to scratch my head. I love Redis — it rocks my world — but that example was too much for me. If you just want to store a set of ids somewhere to avoid normalization headaches, introducing Redis is overkill… just do it in MySQL!

The example was the classic one of non-recipirocal friendships (like twitter) where person A can follow (or friend) person B but person B doesn’t have to follow (or friend) person A. While there’s a tried-and-true schema that can easily handle this in your plain-vanilla SQL store, sometimes that causes a bit of implementation friction when you just want a quick list of friends. So I give you the “new hotness” sets in the “old and busted” MySQL database:

First, the schema:

create_table "users", :force => true do |t| t.string "name" t.text "friend_list" t.datetime "created_at" t.datetime "updated_at" end

end

And then some data:

And finally the model:

Oh noes! An interpolated string in the SQL! Yes, I know, our knee-jerk reaction is that this is a really bad thing, but thanks to attr_protected and coercing the id to an integer in the follow and unfollow methods, it’s safe, really. :) If it gives you too much anxiety, though, you can use a placeholder and just pass in friend_list.split(‘,’) as the data.

Will this perform as well as Redis when it comes to updating sets of ids? Probably not. Will it do for the vast majority of Rails apps out there? Yes. And if you’re lucky enough to have an app that has this code as a major bottleneck, then you can go to town with Redis, and with only minor changes to this model. Until then, you can be happy living in the familiarity of MySQL-land, and keep your deployment environment that much simpler.

Interesting Breakage in Rails 3 RC

| Comments

My latest project, UploadJuicer is running on Rails 3, and I’m loving it. Back in the Rails 3 beta 1 days, there were still a lot of rough edges, but beta 4 has been great so far. Until I wanted to upgrade UploadJuicer to Rails 3 RC (1). After the upgrade, I got this:

ActionController::RoutingError (uninitialized constant ApplicationController::AuthenticationHelpers)

AuthenticationHelpers happens to be the first module I include from ApplicationController, and it lives in lib/authentication_helpers.rb. After a bid of head-scratching as to why something that has worked for ages and ages in Rails suddenly stopped working (and in between a beta and an RC release, to boot!), I remembered this change in config/application.rb from running rake rails:update:

# Custom directories with classes and modules you want to be autoloadable. # config.autoload_paths += %W(#{config.root}/extras)

That looked promising, so I uncommented it, changed extras to lib, and bam, problem solved. Life on the edge is an adventure. :)

Unwrapping UploadJuicer

| Comments

It says something about the sorry state of my blogging lately that my blog is the last place I’m announcing my latest project, Juicer. :) It’s a handy service for offloading image uploading and resizing from your web app. Now you don’t have to tie up your server to handle long uploads or to do a bunch of thumbnails. You can keep your app super-responsive for the core of what it does, and leave the uploads and resizes to us.

Though my blog is the last place I announced the project, it’ll be the first place I announce the Ruby gem that goes along with it. :) I just published a gem that uses the Juicer API (via RestClient) and makes it pretty easy to integrate Juicer with a Rails 3 app. I’ve done the hard work of getting the direct-to-s3 upload with swfupload working for you, so all you have to do is make a few tweaks to your model and you have uploads and thumbnails done for you. You can get at the gem (and a sample Rails 3 app) at Github.

We’ll be posting more about Juicer at the Juicer blog, but I imagine I’ll mention things about it here from time to time. :) And hopefully I’ll get around to posting some of the fun code-related things that make up the service.

Facebook Rails Integration With OAuth2

| Comments

This is simply awesome.  Facebook launches a new API using OAuth2, making it much easier to integrate with Facebook from your Rails app, and Michael Bleigh quickly churns out a new oauth2 gem.

Having dealt with the pain of Facebook integrations recently, and having really enjoyed using OAuth, I had to dive in. The result is a new Rails Kit! :) The Facebook API Rails Kit gives you a sample Rails app that integrates with Facebook and shows you how to post to a Facebook user’s stream. Check it out – I think you’ll like it. I would have loved to have had this a few months ago. :)

Testing an OAuth Provider in Your Rails App

| Comments

Recently I received a request from another B2B SaaS vendor to integrate with Catch the Best, so their users could pull their data from Catch the Best into this app. This is a classic use-case for OAuth, so I dove in to learning how to make my Rails app an OAuth provider. Well, thanks to the OAuth gem and the OAuth Rails plugin, it only took an hour or two to do.

However, since it took a little longer than it should have to do some testing of the app from the console, I thought I’d post this code in case it proves helpful to someone else working on this. My goal was to manually verify that the access control (the before filter) was working properly, so I could email the details on the OAuth endpoint to this other vendor and be reasonably sure it would work well for them. Here’s the code: