Introducing BrB, extremely fast interface for doing distributed ruby

BrB is a simple, transparent and extremely fast interface for doing distributed ruby easily.
It’s inspired from the original Ruby Drb library (Distributed Ruby) but it is build on top of EventMachine for performance.

The concept

BrB use a simple concept : Create an object instance and expose it to the world.
Any other ruby process will be able to call method on that object after having created a communication tunnel.

  • It’s simple as a method call
  • It’s efficient, by default BrB do simple message passing (no return value)
  • You can pass over network every object that is dumpable through Marshal

Example 1 – Simple communication

Start communicating between your different Ruby processes in two easy steps :

Start accepting connections :

class ExposedCoreObject
  def simple_api_method(parameter)
    puts " > Receive #{parameter} in the main ruby process"
  end
end

EM::run do # Start event machine
  # Start BrB Service, expose an instance of core object to the outside world
  BrB::Service.instance.start_service(:object => ExposedCoreObject.new, :host => 'localhost', :port => 5555)
end

In any other ruby process, start communicating :

# Create a communication tunnel to the core process
# nil as first parameter as we do not expose any object in exchange
core = BrB::Tunnel.create(nil, "brb://localhost:5555")

core.simple_api_method('a message')
# Results :
# On core process :  "> Receive a message in the main ruby process"

At the current point, the client call the simple_api_method on our core process.
All the ruby magic is happening again, and number of processes communicating that way is unlimited !

Example 2 – Both side communication

Our previous example was great, but clients can receive method’s call too.

Core code :

EM::run do # Start event machine
  # Start BrB Service, expose an instance of core object to the outside world
  BrB::Service.instance.start_service(:object => ExposedCoreObject.new, :host => 'localhost', :port => 5555)  do |type, tunnel|
    # Get alerted that a new connection has been made :
    if type == :register
      tunnel.say_hi_in_return('I am the core saying Hi')
    end
  end
end

Client code :

class ExposedClientObject
  def say_hi_in_return(s)
    puts " > Core says : #{parameter}"
  end
end

# That time, we are exposing an object.
core = BrB::Tunnel.create(ExposedClientObject.new, "brb://localhost:5555")
core.simple_api_method('a message')
# Results :
# On client process :  "> Core says : I am the core saying Hi"
# On core process :  "> Receive a message in the main ruby process"

That’s it, our both processes are now communicating each others, it’s completely transparent as it’s just work like normal ruby method calls.

Example 3 – Waiting for a return value

By default, calling a method on a distant object is not blocking. That means that it do not wait for any return value. But sometimes, it’s useful to get a return, in order to do this, just add _block at the end of the method name.

core = BrB::Tunnel.create(nil, "brb://localhost:5555")
ret = core.simple_api_method_block('a message') # Wait for the return

What BrB is usable for ?

  • Doing Simple message passing between ruby processes.
  • Connecting hundred of ruby processes transparently.
  • Building a real-time scalable (game) server
  • Taking important load on a server easily just by distributing the load on multiple BrB instance.
  • Taking advantage of multi-core and multi-threaded systems.

If you want to know more about BrB, go to the BrB github.

  • Share/Bookmark

Comments (7)

Social comments and analytics for this post…

This post was mentioned on Twitter by luckwi: BrB: extremely fast interface for doing distributed ruby http://bit.ly/bizH7G #ruby…

Does it supports communicating via Unix sockets?

Hi,

Currently not, it’s just based on TCP, but as it is based on Event Machine, changing TCP to Unix sockets can be easy for a future version.

I have linked this post at the Drink Rails blog.

Now this is REALLY cool, cant wait to reach the point, when i finally ‘have to’ use it ;)

Thx

[...] Distributed ruby via the BrB interface [...]

Wow that’s really bringing two awesome ruby technologies together. I will give it a spin once I finish my thesis-paper.

Write a comment