Clojure for Fun and Profit

When we decided to build our Shutl iOS application we had some decisions to make. One of these decisions was the technology to use to build the back end.

clojure-logo-120bThis back end is a pretty simple service. First, it provides an API that combines and transforms data retrieved from different services so the client can present it. Secondly, it listens to events and notifies the clients accordingly. Both of these use cases could benefit from or require asynchronous processing.

At Shutl, Ruby is our default programming language, so usually it would be a very straightforward decision to choose Sinatra and Sidekiq. But the team is always willing to try new things, and Clojure has caught our attention for some time. Our back-end project sounded like a good opportunity to test out Clojure. It wasn’t a critical component, but it could potentially grow bigger. The logic would be pretty simple, mostly about transforming data, and our design goals — concurrency and parallelization — played to Clojure’s strengths. So we decided to give it a shot.

First choices

As complete beginners, we took a look at the Clojure ecosystem. Although some web frameworks exist in Clojure, the general approach seems to be combining small libraries that do one thing well.

After some research, this was our initial setup:

One big advantage of using Clojure is that it runs on the JVM and offers idiomatic forms to invoke Java code. Clojure therefore makes it very easy to use Java libraries and gives you access to the great ecosystem of Java libraries, like Logback. For some of them (I am looking at you, Hystrix), wrappers also allow a more idiomatic usage.

How we worked

Coming from a Ruby perspective into Clojure, we needed a different mindset but we were halfway there. We embraced immutability a long time ago, functions as first-class citizens are a familiar concept, and Ruby is also dynamically typed.

There were only two things we had to get used to: zero objects and lots of parentheses. The first one was easier than expected; it is kind of easy to get used to the way you write code in Clojure. You write small, concise functions and then you combine them. So simple, so easy. But what about the syntax?

I am not going to lie. It can be overwhelming at the beginning. Then, as you use it, you discover one of the things I love the most about Clojure: it is a very simple language. But simple doesn’t mean powerless, just the opposite. Being a dialect of Lisp, the core of the language is very small but the standard library is complete and well documented.

Clojure allows you to focus on what really matters — what your code is supposed to do — rather than on learning how to do things. This simplicity, combined with the use of the REPL, allows you to easily test and increase your knowledge.

And a tiny, little detail: it is fun.

Epilogue

A few months later, the application went live and everybody was happy. It has been running in production since the fall of 2015, we haven’t had any issue, and it is performing really well.

Obviously, we still have lots to learn. We didn’t use every single feature in the language (did somebody say writing macros?), but just because we didn’t need them. We are very proud of what we built.

All in all the experience was very positive. It was so positive that recently we decided to use Clojure as the back-end language for a new product… stay tuned!

5 thoughts on “Clojure for Fun and Profit

  1. Trevor Hartman

    Very cool to see Clojure being used in eBay.

    Macros are great but don’t use them unless you absolutely must! They bring on complexity and are not first class, like functions.

    How did you convince other team members who had no clj experience to learn it? Most people I try to introduce it to are afraid of it.

    Reply
    1. David Vilar Alcala Post author

      I didn’t really need to convince the team to use Clojure :). During the project kick-off someone put on the table the idea of using Clojure. The team discussed it and we agreed on giving it a shot.

      Now more people in the team is using Clojure and we need to teach them. The approach we are following is the same we use for new joiners, work in pairs developing some new feature. We are very used to pair programming so it is very natural to us and it is working very well.

      Reply
  2. Chris Oakman

    Great post on adopting Clojure! I’m always glad to see public examples of Clojure success in large companies.

    re: “lots of parenthesis” – have you seen Parinfer? https://shaunlebron.github.io/parinfer/ It’s a powerful new way to edit Lisp code that automatically handles most closing parens. Definitely worth a look.

    Cheers!
    Chris Oakman

    Reply
    1. Saeid Akbari

      Parinfer seems cool at first, but in the long run, you end up hating it. Its operations are usually irreversible.
      When you’re writing code in the usual flow (from top to down) it’s all good, but when you try to edit some previously written code, it silently breaks the code.

      I stick with the older Paredit until Parinfer gets improved.

      Reply
  3. Oriol Marti

    Very interesting post.

    It’s good to see that companies like eBay gives that freedom and ownership to their tems.

    Regards,
    Oriol

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *