Scott
Scott

Reputation: 711

Asynchronous database access with EventMachine

I've started work on a simple EventMachine project to accept data from a set of network clients, record it into a database, and simultaneously send it out to a different set of clients. The A client's => B client's part is the sort of thing a reactor makes crazy easy, but the database access not so much - at least, in a non-blocking, evented kind of way. I've been trying to find a decent ORM that supports asynchronous access in a fashion that will play nicely with EventMachine, while still providing all the ORM abstractions I know and love - I am hoping to avoid opening a bunch of sockets and talking SQL over them! Also, maintaing a reasonable spread of DB support is desirable (i've seen a couple of articles explaining how to make async ActiveRecord work with mysql only, for instance).

So far, all i've found is swift, which looks like it should do the trick, but seems quite minimal in comparison to your ActiveRecord's and DataMappers.

Are there any other paths worth pursuing here? Perhaps one of the major ORM powerhouses has a little known async branch? :P

Upvotes: 2

Views: 1594

Answers (2)

deepfryed
deepfryed

Reputation: 56

agree as gregory says, if you can use the native adapters that support async operations - use them. mysql2 & pg gems have pretty good async support.

as a side note, swift (disclaimer: i'm a co-author) has had some api cleanup work done to simplify the async use-cases. you may find this handy.

require 'swift/synchrony'

EM.run do
  3.times.each do |n|
    EM.synchrony do
      db     = Swift.setup(:default, Swift::Adapter::Postgres, db: "swift")
      result = db.execute("select pg_sleep(3 - #{n}), #{n + 1} as qid")

      p result.first
      EM.stop if n == 0
    end
  end
end

Upvotes: 2

Gregory Mostizky
Gregory Mostizky

Reputation: 7261

Personally I prefer to use DB drivers directly in EM based servers.

The reason for this is that the type of projects that require EM servers also require high performance but fairly minimal amount of processing. As such, it is often simpler to put together few SQL statements rather than configure ORM properly and have it work with EM.

With ORM, especially advanced ones, it is far too easy to introduce a bottleneck of some kind. Additionaly, EM is still not very widespread so the chances of hitting some weird bug are larger if you try to use ORM over a simple driver.

Mysql2 gem has builtin support for async operations. Postgresql I haven't used lately so not sure.

Upvotes: 2

Related Questions