Bookie currently checks prices at 9 shops. When you view a new book or refresh the pricing, it checks each of those shops in sequence. A nice little optimisation is to check the shops in parallel. Threading in rails appears to be pretty straight forward – provided you clean up before the request-response cycle is complete.

Here’s the sequential version:

for shop in Shop.find(:all)
   shop.get_pricing(@book)
 end
And the threaded version:
threads =[]
for shop in Shop.find(:all)
  threads 
    lookup.get_pricing(@book)
  end
end
threads.each{|thr| thr.join}
ActiveRecord::Base.verify_active_connections!()
By default ActiveRecord doesn’t allow multiple threads to access its mysql connection, so you need to allow concurrency by adding the following line to your environment.rb file:
config.active_record.allow_concurrency = true
That last line in the example is used to close database connections which are no longer attached to a thread. You’d have thought that joining the thread would have closed it’s database connection, but there you go. Without this you’ll eventually exhaust your database connections.