Concurrency, Threads, Actors and Akka

Concurrency is a topic that’s close to my heart. It all started when I was introduced to Pthreads in my CS 241 Systems Programming class at UIUC. Having the ability to write code that could be doing multiple things simultaneously was (and is) pretty awesome. The assignments in that class, which included writing a parallel sorting function, a parallel version of make and a simple HTTP Server, really helped us appreciate the power of concurrency and threading. My love for concurrency has only grown since then and I’ve written concurrent code in both Java and Python.

This Summer I spent some time with Scala Actors and Akka (I got familiar with the concept of actors on Scala 2.8.1 and then upgraded to Scala 2.10.2 for Akka 2.1.4. As mentioned, Scala 2.10 and above will use Akka for actors). I feel that the actor model is a great way to think of and write concurrent programs. By relying only on immutable message passing (Yes, I know that you can pass mutable messages. But please don’t do that.) between actors for communication and coordination I feel that the likelihood of common errors seen in threading based concurrent programs like deadlock, livelock, race conditions etc. are reduced. Moreover, there are certain classes of applications, for example distributed systems applications, that can easily be modeled as a system with entities that communicate by sending messages to each other. Actors would be great for something like this as the actor code would map pretty neatly to the message flows in the system.

Akka builds on top of the actor model and adds a bunch of cool features. Some of my favorite include: supervision and monitoring, being able to refer to any actor using a hierarchical path, FSM support, Netty based remoting, and routers. This is a great example of why I’m a fan of Akka. The author of the post was able to model his problem as an FSM and translate it pretty neatly into code. This example ties in with what I was saying earlier about actors and distributed systems. By decomposing the system into a set of entities and possible messages that can be exchanged between the two the author of the blog post was able to come up with an elegant solution for the problem.

For programmers who’ve only ever used the threading model of concurrency in the past, I say give actors a try! You might be able to come up with a more awesome and well-structured solution to your problem. Akka also has a Java API in case you do not want to use Scala.

Akka resources:

  1. Akka Java and Scala guide (these are amazing)
  2. The Akka Team Blog

Summer 2012 – Part I: The Internship

This Summer I was a Software Engineering intern at LinkedIn in Mountain View, CA. I worked on the Presentation Infrastructure team under CORE. You can find out more about what I worked on here.

What was really unique about my internship (apart from the amazing people I worked with, the excellent food and the ridiculous perks) is that I got to spend nearly equal amounts of time working on application and infrastructure development. Application development is something that I had “done” before, in that all hackathons I’d taken part in I’d essentially built applications. But I’d never gotten a chance to work on applications that function at “LinkedIn-scale” before and getting an opportunity to do so was a great learning experience. What I’d never done though was infrastructure development and I really enjoyed working on infrastructure components this Summer. The whole concept of “building applications used to build applications” really appealed to me.

I also got to code a little bit in Scala this Summer. I’d read about Scala and had gone through a few “Hello World”-ish tutorials before but had never actually built anything in it. I have to say, from whatever little work I did in it and from the code I saw others had written, that I really like Scala. The OO + functional form of the language appeals to me. The language has a lot of beautiful concepts (like pattern matching and objects) and, from what I’ve heard, it performs well too. Another concept that I really like are Actors (though I never got to write any code that used Actors this Summer). Side note: one of the reasons that I wanted to learn Scala was to learn a language that uses the Actor system, which I first heard of while looking into Erlang. This Summer got me quite excited about Scala and I will definitely try to learn the language properly, i.e. delve more into advanced Scala topics like the Parallel Collections, writing DSLs using Scala, Actors, Views etc. My experience with Java, Python and OCaml helped me get really good at Scala over the Summer and I want to retain this skill. I also want to build a complete application in Scala; I have this idea to build something similar to Scrapy using Scala + Akka, though I don’t see when I will find the free time to do so.

Overall, my internship was excellent. It was all that I wanted and more. Thanks for an awesome Summmer LinkedIn! 🙂