Pragmatic Concurrency With Ruby

I’m coming from a parallel computation, distributed systems background by education, and have relatively strong foundations in infrastructural concurrent/parallel libraries and products that I’ve built and maintained over the years both on the JVM and .Net.

Recently, I’ve dedicated more and more time building and deploying real concurrent projects with Ruby using JRuby, as opposed to developing with Ruby (MRI) with concurrency the way it is (process-level and GIL thread-level). I’d like to share some of that with you.

Feel free to bug me on twitter:

Administrative notes«EOF:

This may come as a lengthy information-packed read. You can put the blame on me for this one because I wanted to increase the value for the reader as much as possible and pack something that could have been a lengthy book, into a single highly concentrated no-bullshit article.

As an experiment, I also put most of the example code in a repository including the source of this article. Please feel free to fork and apply contributions of any kind, I’ll gladly accept pull requests.

Github repo:



This article was recently translated to Serbo-Croatian language by Anja Skrba from - Thanks Anja!

Concurrency is Awesome!

Remember those old 8-bit games you used to play as a child?. In a hindsight - you know its awesome, but if you’re a gamer or just a casual gamer, and you’re forced to play it today, the graphics will feel bad.

This is because it’s a detail thing; just like childhood computer games, as time passes, it seems like your brain doesn’t care (or forgets) the proper details.

So given that one is an MRI Ruby developer, her mindset would be that concurrency just works, and it is easy and awesome. But you might be right guessing that due to the level of cynicism going around here - it isn’t the end of it.

The MRI Ruby GIL is gracefully keeping some details away from you: yes things are running in parallel with the help of properly built I/O libraries (for example: historically, the MySQL gem was initially not doing it properly, which meant your thread would block on I/O), but surely, code isn’t running in parallel. It’s just like what your brain did when it covered up for those horrific 8-bit graphics that you were sure are still awesome.

Building Your Tools With Thor

Thor is not new; first built as a rake and sake replacement, first commit is well over 4 years ago.

Jump ahead several years and Thor is part of the foundation of the new-generation rails generator, and very popular tools such as Bundler and Foreman.

Recently, @wykatz emerged a fantastic looking (and much deserved) Thor website, and although I’ve started doing Thor based projects over two years ago, I think its the right time to write about Thor itself.

Today, Thor can serve as a rake replacement, great generator building framework, and a general purpose CLI toolkit.

First Look at Mruby

mruby is minimalistic Ruby, developed by Matz (Ruby’s creator) and funded by the Japanese ministry of Economy.

I’ve been waiting for this to go public since Matz’ early announcements of him being working on it. This is very exciting.


  $ git clone
  $ make

Compilation is a fantastic error-less breeze, around 20 seconds.

Hello mruby

Lets see how this thing should work.

$ cd bin
$ cat > hello.rb
puts "hello mruby!"
$ ./mruby hello.rb
hello mruby!

Programming Is a Culture


You might or might not have heard about the 501 manifesto. A 501 programmer is the one that runs out of the office at 5:01, regardless of any importance of him staying and keep his responsibilities aligned.

While I do believe in family values and socializing, I think that part of the manifesto is pretty generic, and the addendum of pitying open source or programmers who love what they’re doing is pretty insulting. We (programmers) are a culture now, and usually the passionate ones are the ones that are being mocked.

Fast ID Generation Part I

Update: Hello HN!, Please use this for discussion


In a distributed storage environment you may no longer generate IDs in traditional ways. You need a fast service that generates IDs, which, given constraints such as bit size, isn’t obvious to build.

Twitter’s Snowflake does it following a set of their own constraints, but given more lenient constraints there are other ways such as compositing an ID with a time component and random jitter. There are also important risks and guarantees to take into consideration.

Parting Ways with Auto Increments

You want to generate IDs when you’re working in a sharded setup; in which non of the shards can take responsibility for generating IDs on its own.

Further, you might want an independent service or component to be responsible for ID generation, in a complex transactional usecases, where multiple systems might be affected.

In other cases you’ll want to perform intricate tracking which requires generation at the client side, or far from the server.

Build Management for Javascript, Coffescript and Friends

Update, 16-feb: I’ve added Brewer.js to the review.


Find a tool that can compile coffeescript, take javascripts and vendor javascript libs, join them preserving order, and minify them into one file good for release. If that tool can do CSS/stylus/sass all the better. I pick sprockets eventually and show how you can tailor it to your needs.

Sinatra 1.3 Streaming: Redirecting Processes

Process Management

In ruby, when you want to run and redirect process output, you have many options.
Here are some:

(1) result = `command`
# stdout, stderr if redirected. Buffered until completion.

(2) pipe = IO.popen("command", "r")
# stdout, stderr if redirected. Get chunks while running.

(3) Open3.popen3("command")
# all 3 streams (in, out, err). Get chunks while running.

Streaming with Sinatra

From 1.3.0, sinatra supports streaming. It gets really impressive on an evented server such as Thin, considering that such behavior was reserved either to dedicated frameworks such as cramp and goliath, or even nodejs.

CoffeeScript Fat Arrows

Lets look at the CoffeeScript usage of the fat arrow aka =>:

foo = (hello)=>

Which translates to this javascript code:

var foo;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
foo = __bind(function(hello) {
  return console.log(hello);
}, this);

Backbone Image Gallery - Diving In

I’d like to discuss some interesting issue that surfaced after I’ve published my previous tutorial. Most of these come from HN.

MVC and Selectedness

rgarcia asked:

Not a backbone expert, but how “correct” is it to have a presentation variable like “selected” tied to a backbone model? …

bricestacy said:

…Second, the author makes the claim that Backbone is an MVC and makes things simpler and yet violates that. His way you literally have to go through the view, the collection, the model, a trigger, and back to the view to select a new image.

Note that bricestacy perhaps unknowingly, summarized the way it should work in MVC. You should go to the view, collection, model, and then your view should react back to events from your model!

Image Gallery With Backbone

update: dive into some of the finer details regarding this post here: image gallery aftermath.

Backbone.js. By now you have probably already heard about it, and you’re assuming it can solve all of your problems.

Lets start by clearing up the dust. Backbone is a backbone. Backbone. It is a skeleton, an aggregation of best practices, a great MVC implementation, but at 4.6kb (min) it cannot possibly solve all of your problems.

Also, regarding some criticism with Knockout vs Backbone arguments, many Rails and Rubyists come to backbone - it doesn’t give you Rails-like productivity magically.