Channels
Posted about 20 hours ago at igvita.com

Berkeley Sockets (BSD) are the de facto API for all network communication. With roots from the early 1980's, it is the original implementation of the TCP/IP suite, and arguably one of the most widely supported and critical components of any operating system today. BSD sockets that most of us are familiar with are peer-to-peer connections, which require explicit setup, teardown, choice of transport (TCP, UDP), error handling, and so on. And once you solve all of the above, then you are into the world of application protocols (ex: HTTP), which require additional framing, buffering and processing logic. In other words, it is no wonder that a high-performance network application is anything but trivial to write.

Wouldn't it be nice if we could abstract some of the low-level details of different socket types, connection handling, framing, or even routing? This is exactly where the ZeroMQ (ØMQ/ZMQ) networking library comes in: "it gives you sockets that carry whole messages across various transports like inproc, IPC, TCP, and multicast; you can connect sockets N-to-N with patterns like fanout, pubsub, task distribution, and request-reply". That's a lot buzzwords, so lets dissect some of these concepts in more detail.

Message-Oriented vs. Streams & Datagrams

ZeroMQ sockets provide a layer of abstraction on top of the traditional socket API, which allows it to hide much of the everyday boilerplate complexity we are forced to repeat in our applications. To begin, instead of being stream (TCP), or datagram (UDP) oriented, ZeroMQ communication is message-oriented. This means that if a client socket sends a 150kb message, then the server socket will receive a complete, identical message on the other end without having to implement any explicit buffering or framing. Of course, we could still implement a streaming interface, but doing so would require an explicit application-level protocol.

# create zeromq request / reply socket pair
ctx = ZMQ::Context.new
req = ctx.socket ZMQ::REQ
rep = ctx.socket ZMQ::REP
 
# connect sockets: notice that reply can connect first even with no server!
rep.connect('tcp://127.0.0.1:5555')
req.bind('tcp://127.0.0.1:5555')
req.send ZMQ::Message.new('hello' * (1024*1024))
 
msg = ZMQ::Message.new
rep.recv(msg)
msg.copy_out_string.size # => 5242880
 

Switching from a streaming/datagram to a message-oriented model is seemingly a minor change, but one that carries a lot of implications. Because ZeroMQ will handle all of the buffering and framing for you, the client and server applications become an order of magnitude simpler, more secure, and much easier to write.

Transport Agnostic Sockets

ZeroMQ sockets are also transport agnostic: there is a single, unified API for sending and receiving messages across all protocols. By default, there is support for in-process, IPC, multicast, and TCP, and switching between all of them is as simple as changing the prefix on your connection string. This means we can start with IPC for fast local communication, and then switch to TCP at any point for distributed cases with minimal effort. As an added benefit, ZeroMQ handles all connection setup, teardown, and reconnect logic under the hood. That's about as simple as it gets.

Routing & Topology Aware Sockets

ZeroMQ sockets are routing and network topology aware. Since we don't have to explicitly manage the peer-to-peer connection state - all of that is abstracted by the library, as we saw above - nothing stops a single ZeroMQ socket from binding to two distinct ports to listen to for inbound requests, or in reverse, send data to two distinct sockets via a single API call. How does ZeroMQ know who to listen to or push data to? That depends on the type of the socket pair we pick for our application: Request/Reply, Publish/Subscribe, Pipeline, and Pair (alpha).

ctx = ZMQ::Context.new
 
# create publisher socket, and publish to two pipes!
pub = ctx.socket(ZMQ::PUB)
pub.bind('tcp://127.0.0.1:5000')
pub.bind('inproc://some.pipe')
 
# generate random message, ex: '1 9'
Thread.new { loop { pub.send [rand(2), rand(10)].join(' ') } }
 
# create a consumer, and listen for messages whose key is '1'
sub = ctx.socket(ZMQ::SUB)
sub.connect('inproc://some.pipe')
sub.setsockopt(ZMQ::SUBSCRIBE, '1')
 
loop { p sub.recv } # => "1 9" ...
 

In the case of a Publish/Subscribe socket pair (unidirectional communication from publisher to subscribers), the publisher socket will replicate the message to all connected clients (local IPC clients, remote TCP listeners, etc). In the case of a Request/Reply socket pair (bi-directional communication: server, client), the messages will be automatically load balanced by the socket generating the request to one of the connected clients. Finally, a Push/Pull socket pair (pipeline: unidirectional, load-balanced) will allow you to simulate a staged message passing architecture with built-in load balancing.

ZeroMQ allows us to encode the topology of our services directly via the socket API, without having to define and maintain a separate coordination layer of routers, load balancers, and message brokers. Of course, nothing stops us from using any of these tools in combination with ZeroMQ, but in many cases, the ZeroMQ route can yield better performance and much simpler operational complexity.

ZeroMQ under the hood

By default, all communication in ZeroMQ is done in asynchronous fashion. To enable this, anytime you create an application with ZeroMQ, you will have to explicitly declare the number of background I/O threads - in most cases, a single dedicated I/O thread will suffice. All of the thread logic is handled by the C++ core of the library itself, but it does mean that at very minimum, your application will have two scheduled threads.

This asynchronous processing model allows ZeroMQ to abstract all connection setup, teardown, reconnect logic, and also to minimize message delivery latency: no blocking means the messages can be dispatched, delivered and queued (sender or receiver side) in parallel to the regular processing done by your application. Of course, you can also control the queuing behavior of ZeroMQ sockets by setting an allowed memory bound and even a swap size for each socket. Hence, you can simulate the blocking API if desired, but asynchronous I/O is the default. Combined with zero copy semantics, optimized framing, and no locking data structures, the end result is a high performance and throughput oriented messaging middleware with a modern API.

ZeroMQ in the Wild: Mongrel 2

Mongrel2 offers an interesting case-study of applying ZeroMQ to the world of web-servers: all inbound requests are routed by Mongrel2 via a "Push" socket which automatically load-balances the requests to connected handlers. The handlers, in turn, process the incoming requests (via Pull socket) and publish them to a "Pub" socket, to which the Mongrel2 server itself is subscribed to and is listening for its process ID (via a topic filter).

Hence, the processing is not tied to a simple request-response cycle we are commonly used to, where a single backend has to handle the full request start to finish. Instead, we can setup several processing stages (via pipeline pattern), and emit our reply only after it is processed by all stages.

Ambitious and worth exploring

Needless to say, ZeroMQ is an ambitious project, and this short introduction only scratches the surface of the full feature set. The stated goal of ZeroMQ is to "become part of the standard networking stack, and then the Linux kernel". Whether they will succeed, remains to be seen, but it is definitely a very promising and arguably a much needed layer of abstraction on top of the "traditional" BSD sockets. ZeroMQ makes writing high performance networking applications incredibly easy and fun.

The best way to get started with ZeroMQ is to work through some hands-on examples - the concepts are not new, but the ease with which you can compose them takes some getting use to. For Rubyists, Andrew Cholakian has put together a great set of examples to get you started (check out dripdrop as well), and for everyone else, head to the ZeroMQ site, grab your language bindings and dive into the code.

back to top
Favorite
Posted about 23 hours ago at Ruby5

Refinery, During Construction, Saki, Andriod SpyCam, JRuby on Rails3, Inploy, Bundler meets Capistrano, Rake-remote_Task, and cached commons are all featured on this episode of Ruby5

Listen to this episode on Ruby5

This episode is sponsored by NewRelic RPM
Did you know that New Relic Has deployment tracking? All you do is modify your capistrano task to let new relic know when you do a deployment. Then when you view you stats on new relic after the deployment, you'll actually see a line on each of your new relic graphs so you'll be able to see how performance was affected. Check out the video over on NewRelic.com to learn more.

RefineryCMS
There is a new release of Refinery, the rails-based content management system. This release has been updated to use Rails 3, the architecture has been changed to use rails engines internally, a bunch of bugs have been fixed, and the file upload plugin moved from attachment_fu to Dragonfly.

During Construction
During Construction is a hosted 'coming soon' page that looks a lot better than the default one the registrars give you, and it has a couple of nice features like a countdown timer, and an email collector.

Saki
Saki is a dsl alternative to cucumber for use with RSpec. Written by Nate Kidwell, he was aiming for a descriptive language that was not quite as verbose as cucumber, while being more expressive.

Spy Cam
Mike Leone turned his Android phone into a spy camera using Sinatra and JRuby. This article show how he did it.

JRuby on Rails3
Greg Moreno took a classic starter howto and added the letter 'J' and the number '3' to it. This is an easy walkthrough if you want to work through getting a rails 3 app running on Tomcat on JRuby.

Rails3/Inploy
Inploy is a new deployment tool on the Ruby scene, and Thomas Ritz wrote a deployment template that makes Rails3 deployments easy. Diego Carrion wrote this article showing how, and it looks super-simple. Go Diego, Go!

Bundler/Capistrano
Bundler now provides some default tasks for hooking in with Capistrano, so all the bundler goodness can run when needed in the scope of a deploy. This blog entry by Richard Huang shows how easy it is.

rake-remote_task
Rake-remote_task is an extraction from Vlad the Deployer that adds the concept of remote tasks to Rake. If you ever need to run rake tasks across a bunch of machines, this is the tool for you.

Cached Commons
Cached commons is a collection of user-contributed javascript libraries that have been optimized, compressed, and cached on github's fast content delivery network. It also has a really nice index, and links to the home page and docs for each library.

back to top
Posted about 24 hours ago at Plataforma Tecnologia Blog

Rails 3 was released this week but the minds of the Rails Core team members are already focused on the 3.1 release for quite some time. DHH was the first one to give a hint on what we would like to see in Rails 3.1 in his RailsConf talk and, as Ruby Summer of Code is close to its end, we are able to see the work of several students getting solid enough to be an important part of Rails 3.1 release.

In between all this work, I was invited to participate in three important conferences in the following months and lately I’ve prepared enough material to give a talk entitled “Rails 2.3, 3.0 and 3.1: Past, Present and Future“!

In this talk I plan to discuss many of the conceptual changes done in Rails 3 and how these changes were given life in the Rails source code, comparing, as much as possible, with Rails 2.3. After the current and past scenarios are throughly discussed, I will show how much of the work done in Rails 3 can still be improved and how several Ruby Summer of Code Projects are helping us to achieve it. And if you ever wondered how much Merb affected the Rails community, you will have a few surprises while watching this talk!

The three different conferences I mentioned above will be held in Ukraine, Brazil and Sweden. But I’m not sure if there will be anyone recording them, so I’d suggest you not to miss any of them. ;)

Here they are…

1) RubyConf Ukraine: 16th and 17th October

If you have never been to Kyev before (just like me), here’s a great opportunity to visit it for the first time! It will be two days of deep immersion into Ruby with nice city visits during the night!

Kiev

Other active developers in the community as Oleg Andreev and Piotr Sarnacki will be present as well. By the way, RubyConf Ukraine is still accepting both sponsors and talk proposals! We are waiting for you!

2) RubyConf Brazil: 26th and 27th October

Right after RubyConf Ukraine, I’ll be flying back to Brazil to present a portuguese version of this talk. RubyConf Brazil is the former “Rails Summit Latin America” (which has been the largest Ruby and Rails conference in Latin America for the last two years). And this year it won’t be different. Several Ruby and Rails developers (like Yehuda Katz, Charles Nutter, Evan Phoenix and many others) have confirmed their presence. If  you get the chance, don’t miss it!

3) Oredev Sweden: 8th to 12th November

And finally, my last stop will be in Sweden at the developer conference held in Malmö. This will be different from the previous two, since it is not focused in Ruby nor Rails. It’s a multitrack conference that hosts different technologies (by the way, the keynotes and tutorials programme is very interesting!). Since I’m expecting several non-Rails developers in the audience, I will slightly change my talk to focus more on the conceptual side and less on technical discussions. Also, I’ll be hosting a a workshop about Rails 3, where I’ll demonstrate a few of @plataformatec’s open source projects.

If you are coming to any of these events, please let me know in the comments!

back to top
Posted 1 day ago at MongoTips by John Nunemaker

In between posts that I write, I try to share links here to other cool articles around the web about MongoDB. The problem is that up until now I have felt like I needed to write a whole post about the link, which usually means it takes me longer to get the post up here.

In the interest of lowering the barrier of entry, I have added a link type. Links will be simple a title, url and short description. You probably have noticed a few come through already tonight. I hope this makes it easier for me to get you interesting information and that you find it useful. Also, if so desired, you can still comment on the links here with questions and opinions.

From now on, I will do my best to curate interesting links about MongoDB in a timely fashion and occasionally drop some of my own knowledge in more full posts. Now back to our regularly scheduled program…

back to top
Posted 1 day ago at MongoTips by John Nunemaker

A “massively multiplayer”, online crossword puzzle. Uses MongoDB’s geospatial indexing. The creators posted Building a Scrabble MMO in 48 hours if you are curious about how they did it. Crazy!

back to top
Posted 1 day ago at MongoTips by John Nunemaker

Kristina Chodorow is on a roll posting about Mongo’s assumptions, history, sharding, and now an introduction to mongosniff.

back to top
Posted 2 days ago at Ruby Inside

It's been a great year for Ruby on Android, but no one knows it. You can start writing Ruby apps for Android devices TODAY. You don't need to install any SDK, you don't need to install some giant Eclipse IDE, and you certainly don't need to write any Java.

Mike Leone

In Turn your Android Phone Into a Remote Spy Camera with Ruby in 15 Minutes, Mike Leone demonstrates how to use Ruby, Sinatra and Scripting Layer for Android (SL4A) to build and deploy a phone-hosted "spy camera" Web service.

SL4A is a system that allows you to run "scripting language" scripts and interactive interpreters on the Android platform. It currently supports JRuby, Python, Perl, Lua, JavaScript, BeanShell, and Tcl. Mike demonstrates how to set up a Sinatra project to use SL4A to run on an Android phone using JRuby. Upon receiving a request, Mike's app takes a picture using the phone's camera and serves it back over HTTP. He has also released the source code to a larger Ruby app called Broadcast that implements general Android device management functionality over HTTP.

Even if you don't want to build a "spy camera", Mike's walkthrough is a must-read if building Web services in Ruby that can run directly on the Android platform is of interest to you.

back to top
Posted 2 days ago at RubyLearning Blog

Programming Challenge for Newbies in Clojure and Python too?

RubyLearning has been conducting the monthly Ruby Programming Challenge for Newbies for over a year now and so far 12 challenges have been completed. The 13th challenge is in progress. All this was possible due to the extensive support we got from Rubyists across the world. Also, you all indicated that we continue with these challenges in the months to come.

Recently, my colleague Dhananjay Nene posted a Python based solution to the 13th Ruby challenge. While discussing the solution it struck me that it would help Clojure and Python Newbies, if we opened up these challenges in these languages too. Dhananjay and some of my Clojure colleagues are interested in evaluating the submitted solutions in Clojure and Python and maybe we could start the challenges from Oct. 2010.

Clojure, Python enthusiasts interested? What Do you Think? What is Your Opinion? Please share in the comments below.

Technorati Tags: Clojure, Python, RPCFN, Ruby Challenge, Ruby, Programming

back to top
Posted 3 days ago at Jay Fields' Thoughts

An introduction to clojure.test is easy, but it doesn't take long before you feel like you need a mocking framework. As far as I know, you have 3 options.

  1. Take a look at Midje. I haven't gone down this path, but it looks like the most mature option if you're looking for a sophisticated solution.

  2. Go simple. Let's take an example where you want to call a function that computes a value and sends a response to a gateway. Your first implementation looks like the code below. (destructuring explained)
    (defn withdraw [& {:keys [balance withdrawal account-number]}]
    (gateway/process {:balance (- balance withdrawal)
    :withdrawal withdrawal
    :account-number account-number}))
    No, it's not pure. That's not the point. Let's pretend that this impure function is the right design and focus on how we would test it.

    You can change the code a bit and pass in the gateway/process function as an argument. Once you've changed how the code works you can test it by passing identity as the function argument in your tests. The full example is below.
    (ns gateway)

    (defn process [m] (println m))

    (ns controller
    (:use clojure.test))

    (defn withdraw [f & {:keys [balance withdrawal account-number]}]
    (f {:balance (- balance withdrawal)
    :withdrawal withdrawal
    :account-number account-number}))

    (withdraw gateway/process :balance 100 :withdrawal 22 :account-number 4)
    ;; => {:balance 78, :withdrawal 22, :account-number 4}

    (deftest withdraw-test
    (is (= {:balance 78, :withdrawal 22, :account-number 4}
    (withdraw identity :balance 100 :withdrawal 22 :account-number 4))))

    (run-all-tests #"controller")
    If you run the previous example you will see the println output and the clojure.test output, verifying that our code is working as we expected. This simple solution of passing in your side effect function and using identity in your tests can often obviate any need for a mock.

  3. Solution 2 works well, but has the limitations that only one side-effecty function can be passed in and it's result must be used as the return value.

    Let's extend our example and say that we want to log a message if the withdrawal would cause insufficient funds. (Our gateway/process and log/write functions will simply println since this is only an example, but in production code their behavior would differ and both would be required)
    (ns gateway)

    (defn process [m] (println "gateway: " m))

    (ns log)

    (defn write [m] (println "log: " m))

    (ns controller
    (:use clojure.test))

    (defn withdraw [& {:keys [balance withdrawal account-number]}]
    (let [new-balance (- balance withdrawal)]
    (if (> 0 new-balance)
    (log/write "insufficient funds")
    (gateway/process {:balance new-balance
    :withdrawal withdrawal
    :account-number account-number}))))

    (withdraw :balance 100 :withdrawal 22 :account-number 4)
    ;; => gateway: {:balance 78, :withdrawal 22, :account-number 4}

    (withdraw :balance 100 :withdrawal 220 :account-number 4)
    ;; => log: insufficient funds
    Our new withdraw implementation calls two functions that have side effects. We could pass in both functions, but that solution doesn't seem to scale very well as the number of passed functions grows. Also, passing in multiple functions tends to clutter the signature and make it hard to remember what is the valid order for the arguments. Finally, if we need withdraw to always return a map showing the balance and withdrawal amount, there would be no easy solution for verifying the string sent to log/write.

    Given our implementation of withdraw, writing a test that verifies that gateway/process and log/write are called correctly looks like a job for a mock. However, thanks to Clojure's binding function, it's very easy to redefine both of those functions to capture values that can later be tested.

    The following code rebinds both gateway/process and log/write to partial functions that capture whatever is passed to them in an atom that can easily be verified directly in the test.
    (ns gateway)

    (defn process [m] (println "gateway: " m))

    (ns log)

    (defn write [m] (println "log: " m))

    (ns controller
    (:use clojure.test))

    (defn withdraw [& {:keys [balance withdrawal account-number]}]
    (let [new-balance (- balance withdrawal)]
    (if (> 0 new-balance)
    (log/write "insufficient funds")
    (gateway/process {:balance new-balance
    :withdrawal withdrawal
    :account-number account-number}))))

    (deftest withdraw-test
    (let [result (atom nil)]
    (binding [gateway/process (partial reset! result)]
    (withdraw :balance 100 :withdrawal 22 :account-number 4)
    (is (= {:balance 78, :withdrawal 22, :account-number 4} @result)))))

    (deftest withdraw-test
    (let [result (atom nil)]
    (binding [log/write (partial reset! result)]
    (withdraw :balance 100 :withdrawal 220 :account-number 4)
    (is (= "insufficient funds" @result)))))

    (run-all-tests #"controller")
In general I use option 2 when I can get away with it, and option 3 where necessary. Option 3 adds enough additional code that I'd probably look into Midje quickly if I found myself writing a more than a few tests that way. However, I generally go out of my way to design pure functions, and I don't find myself needing either of these techniques very often.

back to top
Posted 3 days ago at The GitHub Blog

While Twitter may see traffic spikes during Apple events, we see quite the opposite:

back to top

cached-commons: API for Common Javascripts and Stylesheets Cached and Optimized:

You probably already use Google’s AJAX Libraries as a CDN for your JavaScripts. But what do you do when Google is slow to add your favorite JavaScript library? Lance Pollard has created Cached Commons, an API for common Javascripts and stylesheets, cached and optimized on GitHub’s CDN.

Screenshot

Cached Commons lists dozens of scripts under categories like visualization, Ajax, syntax highlighting, HTML5, Flash, and testing, complete with links to project sources, demos, even documenation. Don’t see a script you want? Just fork the project and your script for everyone to share.

It looks like CSS libraries are on the TODO list, just like I had asked of Google in Episode 0.3.2!

[Source on GitHub] [Homepage]

back to top
Posted 3 days ago at Obie Fernandez

back to top

Mastering Node: Open source eBook for Node.js:

GitHub is for more than just code you know. It’s a great collaboration tool for hackers. Nathan, Chris, and I use GitHub as a big part of our workflow for our upcoming book.

TJ (do we really have to say his last name by now?), of Express and now Sencha wants you to Master Node.js by reading and hacking on his own community-driven ebook.

The book will walk you through Node step-by-step from installation to topics like EventEmitter:

Typically an object inherits from EventEmitter, however our small example below illustrates the api. First we create an emitter, after which we can define any number of callbacks using the emitter.on() method which accepts the name of the event, and arbitrary objects passed as data. When emitter.emit() is called we are only required to pass the event name, followed by any number of arguments, in this case the first and last name strings.

var EventEmitter = require('events').EventEmitter;

var emitter = new EventEmitter;

emitter.on('name', function(first, last){
    console.log(first + ', ' + last);
});

emitter.emit('name', 'tj', 'holowaychuk');
emitter.emit('name', 'simon', 'holowaychuk');

Go ahead, fork it, contribute, and add ePub support.

[Source on GitHub]

back to top

Episode 0.3.3 - Node Knockout:

Micheil and Wynn caught up with Gerad and Visnu from the Node Knockout to talk about the 48 hour Node.js development competition and its entries.

Download MP3

Items mentioned in the show:

  • Lone Star Ruby Conference - Texas’ regional Ruby conference in Austin
  • Mike Perham - The awesome Rubyist whose name gives Wynn fits
  • Node Knockout - The 48 hour Node.js coding competition
  • Rails Rumble - Ruby’s own 48 hour coding bash
  • Gerad - of Gerad & Visnu, the “data-y” and “product-y” guy
  • Visnu - the “developer-y” and “designer-y” guy
  • Fortnight Labs - the proper name for Gerad & Visnu, Inc.
  • List of great Node Knockout Judges
  • Joyent & Heroku are great places to host your Node.js apps.
  • Express High performance, high class web development for Node.js
  • Connect - high performance middleware framework for node featuring robust middleware for serving static files, advanced routing, cookie and session implementations, error handling and much more.
  • npm is a package manager for node. You can use it to install and publish your node programs. It manages dependencies and does other cool stuff.
  • Node Inspector is a web inspector based Node.js debugger
  • Go vote for your favorite entries!

back to top

dalli: High performance memcached client for Ruby:

After maintaining memcached-client for a couple of years, Mike Perham decided it was time to build a new Ruby client for Memcached from the ground up. Dalli aims to be a simpler, high performance client for Memcached the simple, ultra-fast in-memory key value store.

Among the improvements in Dalli:

  • Use of the faster binary Memcached protocol vs. the slower text protocol
  • Almost 500 fewer lines of Ruby
  • Monitoring hooks for tools like New Relic and Rack::Bug
  • SASL support for managed environments

To get started, just install the gem

sudo gem install dalli

You can then write to and read from your cache:

require 'dalli'
dc = Dalli::Client.new('localhost:11211')
dc.set('abc', 123)
value = dc.get('abc')

[Source on GitHub]

back to top