Search code examples
rubyprogramming-languagesconcurrencyclojurenode.js

Eventdriven app - what language or VM to choose?


I'm considering writing an app that has the following requirements. I'm proficient with Ruby, but I'm willing to learn a new language like Scala, Clojure or Python.

Concurrency / Best performance

This is my main goal. It needs to be amazingly fast and support concurrency in a decent way.

Use Redis as a back-end

This won't be a big problem, redis has a wide range of drivers available, but it may influence the final decision on a language/platform.

Websockets support

Good support for websockets is a must. Using an add-on library (like Cramp for Ruby::EM) is okay.

Options

I've gathered the following options:

  • Ruby EventMachine
  • Python Twisted
  • Node.js
  • Clojure
  • Scala
  • Java

Writing raw C or assembler are not viable options at this time.

Concurrency

Ruby 1.9 still uses the GIL, where as all JVM based solutions can use native threads. I'm not sure about Node.js in this case.

How does the selected language affect performance?

The question

What do you recommend and why? Do you have hands-on experience? Please enlighten me (and the rest of StackOverflow)


Solution

  • I'd vote for Clojure if high performance concurrency is your main criteria. Clojure was basically designed for concurrent development from the beginning, and there have been some impressive Clojure demos running on 800+ core Azul boxes.

    It is very much worth looking at this video presentation to understand Clojure's approach to concurrency:

    http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey

    The main trick in Clojure's concurrency performance is a clever implementation of Software Transactional Memory (STM) that lets you conduct many concurrent transactions without complex and expensive locking schemes. It also uses persistent data structures to give immutability and efficient management of multiple versions of data. It's very cool.

    As for general purpose performance, Clojure is pretty fast already and getting even faster with the new 1.3 alpha branch. A stated aim of Rich Hickey (Clojure's creator) is to allow you to do anything in Clojure with the same speed that you can do it pure Java.

    Other things in Clojure that I really like but may or may not be relevant to you:

    • Hugely powerful LISP-style macro system - "code is data" and you can manipulate it as such
    • It's a fully fledged functional language
    • It's dynamically typed by default (for flexibility and quick prototyping), but you can add static type hints if you need to (for better performance)
    • Excellent JVM / Java integration, so you can make use of all the good Java libraries and tools out there (e.g. Netty for event-driven server communications)