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.

Win32 OpenSSL::SSL::SSLError at /auth/facebook/callback

If you’re getting this:

OpenSSL::SSL::SSLError at /auth/facebook/callback
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

It means that open-ssl cannot find your ca_certs (which live under /etc/ssl/certs), which is correct if you’re on windows anyway.

If in rails (using Windows), try disabling verification:

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :facebook, FACEBOOK_KEY, FACEBOOK_SECRET, {:client_options => {:ssl => {:verify => false}}}

You’re going to deploy on Linux any way (and you shouldn’t be developing on an env that isn’t identical to your prod env), but you can at least go on.

ExecJS, CoffeeScript and Ruby

Now that Rails 3.1 can love you back with CoffeeScript, you might wonder how it gets a streamlined ruby-esque experience without falling back to node.js being installed on your windows machine.

Using Rr With Rspec 2.0 and Rails 3.0

Here is a quick guide to get rr together with rspec 2.0 and Rails 3.0 (using Mongoid).

Add to Gemfile:

gem 'rr'


$ bundle install
$ cd vendor/plugins
$ git clone -b rspec2

in your spec_helper.rb:

require 'rspec/rr'
config.mock_with :rr

Now in your controller specs:

describe CommentsController do
  include Devise::TestHelpers
  def mock_comment(stubs={})
	@mock_comment ||= mock_model(Comment, stubs)

  describe "voting" do
	it "should up vote" do
	  @user = new_user
	  @user.aid = 'account'

	  sign_in @user
	  mock(Comment).find("4def8b7a53bfd22388000002") { mock_comment }
	  put :up, :id => "4def8b7a53bfd22388000002"

Vim on Windows

Getting a recent Vim build – some of the plugins require a more recently patched Vim (Command-T). For this, you can find a prebuilt one here.

One of the things people like about Vim is the fact that they customize and fit it to their specific needs. Vim gives you tons of control, at many levels of time investments on your part – be it a quick local vim script tweaking or a proper bundle written either in vimscript or ruby or python or whatever.