Fork me on GitHub

Saturday, January 16, 2010

DNS lookup Experiment in Ruby - Blocking and NonBlocking Modes

Normal DNS lookup in Ruby (Blocking Mode)

require "resolv"

hosts = %w[]

start =

hosts.each do |host|
p "Getting address for #{host}"
p Resolv.getaddress(host)

finish =

p "Time take to finish #{finish - start} seconds"

Execution Result -

"Getting address for"
"Getting address for"
"Getting address for"
"Getting address for"
"Time take to finish 40.313232 seconds"

Non Blocking DNS Lookup in Ruby (Using EventMachine Deferrable)

require "rubygems"
require "resolv"
require "eventmachine"

class Dns
include EM::Deferrable
def resolve_hostname(hostname)
ip = Resolv.getaddress(hostname)
set_deferred_status :succeeded, ip
rescue Exception => ex
set_deferred_status :failed, ex.to_s

start = {
p "Requesting DNS info for yahoo"
dns0 =
dns0.callback {|response| p "For yahoo #{response}"}
dns0.errback {|response| p "For yahoo #{response}"} { dns0.resolve_hostname ""}

p "Requesting DNS info for google"
dns1 =
dns1.callback {|response| p "For google #{response}"}
dns1.errback {|response| p "For google #{response}"} { dns1.resolve_hostname ""}

p "Requesting DNS info for twitter"
dns2 =
dns2.callback {|response| p "For twitter #{response}"}
dns2.errback {|response| p "For twitter #{response}"} { dns2.resolve_hostname ""}

p "Requesting DNS info for github"
dns3 =
dns3.callback {|response| p "For github #{response}"}
dns3.errback {|response| p "For github #{response}"} {dns3.resolve_hostname ""; EM.stop }
finish =

p "Time take for querying #{finish - start} seconds"

Execution Result -

"Requesting DNS info for yahoo"
"Requesting DNS info for google"
"Requesting DNS info for twitter"
"Requesting DNS info for github"
"For google"
"For yahoo"
"For github"
"For twitter"
"Time take for querying 20.311271 seconds"

The results are obvious :). You can see the difference between the blocking and non blocking modes and the effect they may have on your systems. I am also planning to write a DNS library based on NeverBlock which uses Fibers as a way to do non blocking concurrency.

EventMachine is an excellent piece of software which gives a lot of functionality to do things asynchronously. Especially the Deferrable pattern is an awesome way to do long running jobs or calculations and still not block the whole world from running. I have been exploring EventMachine for sometime since I started writing em-couchdb.

The code is written in a simplified way and may not be of direct use to production but hope it reflects the principles of value of doing things in non blocking way.

No comments: