I had the pleasure of giving a presentation on memcached usage at Ruby on Rails Camp at IBM Almaden in San Jose yesterday. I just posted the slides online for anyone who is interested.
Ruby on Rails Camp
I’m heading to Ruby on Rails Camp in San Jose tomorrow. I’ll be giving a speed demo of Zvents and hope to lead a session on memcached. I threw some slides together covering lessons and best practices that we’ve learned while implementing memcached on our site. Hope to see you there.
Rails Plugin: Extended Fragment Cache
One more plugin went out the door today. If you use fragment caching in your Rails application, you’ll want to add this plugin. It cuts the number of requests sent to memcached in half and provides some helper functions for more efficient memcached utilization.
Rails Plugin: Association Collection Tools (aka Habit Forming)
3 plugins in 3 days. I’ll the first to admin that I need help, but it feels so good that I just can’t stop. If Evgeny Malkin can become the first NHL player in 89 years to open his career with goals in his first 6 games then it’s the least I can do to release buggy code on a daily basis. Just wrapped up the association_collection_tools plugin. Here’s an overview.
Adds methods to Rails has_many and HABTM associations that speed up common operations:
a. fast_copy
A method called fast_copy is added to has_and_belongs_to_many association collections that makes the process of cloning HABTM associations MUCH more efficient. Simply replace person1.items = person2.items with person1.items.fast_copy(person2) and your database, network and RAM will thank you. See below for more details.
b. ids
A method called ids is added to has_many and has_and_belongs_to_many association collections. It returns the list of object ids in the association collection without unnecessarily instantiating the objects.
Rails Plugin: Memcache Client Extensions
The memcache-client_extensions plugins adds three new commands to the memcache client API:
- get_multi : retrieve more than 1 key in parallel
- stats : retrieve server performance and utilization statistics
- flush_all : empty all information stored in memcached
It's the small things in life
Ben Curtis just wrote a post saying that my first plugin was number 400 registered in the Rails plugin directory. I know it doesn’t mean anything in the grand scheme of things, but it hasn’t stopped me from feeling like a proud father. I’ll be releasing a couple more plugins over the next couple days, but chances are that I’ll miss out on number 500.
Anyhow, thanks to the AWD guys from providing a great service.
Rails Gotcha: nil values for has_one associations not cached
I’ll be making note of random Rails gotchas as I come across them. Here’s one that bit me about a month ago. If you have a has_one association set up between models, Rails will not cache a nil value for that association.
class Car < ActiveRecord::Base has_one :owner end
Now let’s say you have a car object whose owner is nil. Each time you refer to car.owner, Rails hits the database with a fresh SQL call looking for the car’s owner. It doesn’t keep track of the fact that there is no owner for this car. It doesn’t matter whether you eager load the owner association or not. If you refer to car.owner 10 times while rendering your page, you’ll get 10 separate SQL calls looking for that same nil value.
Once you know this, it’s easy enough to work around. The good news is that the problem appears to be fixed in Rails 1.2. See patches 5757 and 5767 if you want the details.
Rails Plugin: Custom Benchmarks
I finally got around to releasing my first Rails plugin and it was so much fun that I’m packaging up a handful of other plugins to be released over the coming week. It’s called Custom Benchmark and I’ve included a short description below. Read the full announcement for more info.
Custom Benchmarks allow you to easily add your own information to the benchmark line logged at the end of each request. Here’s an example where we’ve added Search latency logging and the dispatch.fcgi process ID to the benchmark log line.
Completed in 0.40306 (2 reqs/sec) | Rendering: 0.05003 (12%) | DB: 0.04571 (11%) | Search: 0.16429,1 (40%) | PID: 22426 | 200 OK [http://www.zvents.com/welcome/index]
Typically, the log line includes the latency associated with executing specific parts of a request. In the example above, we have added a measurement of search latency. But you can use Custom Benchmarks to add any information to the log line. The example above also shows the ID of the process (PID) that served this request. The PID is useful when parsing information from logs that contain data from multiple processes.
Dancing Cosby Dependency
I’ve never met Andy Baio in meatspace. And even though we run in the same (work) circles, I’ve never even had any interactions with him in cyberspace. Nevertheless, I feel like he has impacted my life by introducing me to the House of Cosbys.
It’s a really odd series of animated shorts that was taken off the Net once Cosby’s lawyers got a whiff of it. I can’t recommend it because it takes a certain sense of humor to enjoy it. For some reason, I just can’t get enough. Must be the long hours at work...
So long and thanks for all the fish
This is really the beginning, but in recognition of how my life has been upside-down recently I thought I’d start with one of my favorite closing quotes of all time. Welcome to my blog. Yep, I’m really late to the game, but at least I showed up. Wait, what do you mean it’s over? That always happens to me…
