NeverBlock and ActiveRecord, Concurrent DB Access Without Threads

A new neverblock_postgresql activerecord adapter has been released. This new adapter makes it possible for applications using activerecord to utilize multiple connections to do queries in parallel in single threaded applications. This results in much higher performance and better CPU utilization.

What do I need to do?

If you were using the vanilla postgresql adapter you would’ve initialized the connection as follows:

Now, to use NeverBlock all you need to do is:

Note: you must have the pg, neverblock, neverblock-pg and the activerecord-neverblock-postgresql-adapter gems installed

This way the AR library will pick the gem and use neverblock_postgresql instead of the postgresql adapter

Is that all?

To be able to utilize NeverBlock, you need to be running in an EventLoop, namely EventMachine's event loop. Establishing a connection should happen in the EM.run block or in a later event. The other thing is that you need to be running within a fiber, using NeverBlock's FiberPool class. Does it sound confusing? don't worry, soon we will release an end to end solution where you wont worry about any of this to reap all the benefits of NeverBlock.

Example

We benchmarked the new adapter against the vanilla postgresql adapter. This time our workloads consisted of normal AR operations repeated for a 1000 times. We increased the workload by increasing the count and complexities of the queries we ran in each fiber.

Running the work loads against postgresql-adapter vs neverblock-postgresql-adapter

NeverBlock advantage for different workloads

Active Record Connection Scaling With NeverBlock

Testing NeverBlock with different connection counts for slow and fast queries

The benchmarks page has been updated with the NeverBlock::ActiveRecord performance results.

What's next? I think you already guessed it!

Comments

1.

2 questions : Do you need ruby 1.9 for this to work ? Are you working on a mysql adapter ? ;...(

2.

1 - Yes you need Ruby1.9 for this to work 2 - We are checking Asymy tbqf's async mysql driver

3.

Looking forward to seeing that end-to-end solution. BTW - send a maid to clean up your spam :)

4.

This looks really cool. I have a great place where I could use this in a current project I'm working on but I can't seem to get rails to load the neverblock activerecord adapter properly. Is there going to be a gem released for this soon?

5.

It looks like those github links point to non-existent pages.

6.

@ehsanul: i was thrown for a loop by this as well, he has recently integrated the adapters to the main repo http://github.com/oldmoe/neverblock/tree/master