Mike Perham

On Ruby, software and the Internet

Cassandra and EventMachine

February 9th, 2010 · 3 Comments

I spent this past weekend adding eventmachine support for the Cassandra gem. We’re using Cassandra at OneSpot as our next-gen data store and need EM support. They were nice enough to pull my changes yesterday so the next release of the thrift_client and cassandra gems should work in EM. You just need to do this:

    require 'thrift_client/event_machine'
    EM.run do
      Fiber.new do
        @twitter = Cassandra.new('Twitter', "127.0.0.1:9160", :transport => Thrift::EventMachineTransport, :transport_wrapper => nil)
        @twitter.clear_keyspace!
        EM.stop
      end.resume
    end

The key is the :transport and :transport_wrapper options which override the default, Socket-based implementation. Like all of my EventMachine code, this requires Ruby 1.9.

Tags: Ruby

3 responses so far ↓

  • 1 Ryan King // Feb 10, 2010 at 11:43 am

    I’m glad we’re going to have this support. Perhaps you can add an example like the above to the README or a wiki page?

  • 2 Blair // Mar 14, 2010 at 3:01 am

    Correct me if I am wrong, but looking at this, it seems to be a blocking implementation, that is using a separate thread to communicate with Cassandra.

    Are there any Cassandra Protocols for EventMachine that can run within the Reactor thread? Is that a silly thing to expect with Cassandra (I am new to Cassandra and Eventmachine)

  • 3 mperham // Mar 14, 2010 at 10:56 am

    Blair, look at the Cassandra and thrift_client gems. Their APIs are blocking – there’s no way the gems could be used with the more traditional EM callback style. You can certainly have a callback-based Cassandra client but you would need to start from scratch and develop a new API.

Leave a Comment