My Realm

Pi is as good as Pie- Introduction to Akka in Scala

A year ago, I read about reactive functional programming through RxJava while searching for soundcloud api, which led me to google functional programming, which in turn showed me Scala. With scala, came Typesafe, and on the Typesafe site there was akka. Weird names attract me much(maybe because even I have one). So, I finally googled Akka. After a few articles and a video about scala, I understood what I have been missing from my life.

To me, Akka is a library which makes concurrency easy for newbies like me, and powerful for seasoned devs. The actor model of concurrency is quite new to me. At first, I didn't understand it's use. I thought of it more like syntactic sugar. I didn't get the scalability, and reactive sense of it. But, then I decided to make a program using it, and everything became crystal clear, and apparent.

In one tutorial of Akka, they were talking about getting the value of pi through actors.So, what ARE actors? Well, Akka is not just a library, and scala is not just a language. They both are paradigms. To use Akka, you need to think a little different than traditional JAVA threads. The Actor model forms the core of Akka. Give it a read and come back(please?).

Oh god, I thought you would never come. Now, that you know what actors are and how they work, I can share the code of the pie, i mean pi. So, what we have here is a simple formula for pi.


4 times the sum of the series would give me pi, and the larger the value of n, the precise the value of pi is. Now, what we will do is, we will think of actors as people(always think of them like that). You are the Project manager, and you need the value of pi to take over the world. What you can do is, you can tell an intern to use the series and get the value of pi. And the intern can tell another guy to give the value of individual terms of the series. Hence, the third guy gives value of each term, the intern adds em up, and when all is done, the sum*4 is printed. Simple enough. The people respond when you message them, it's all event driven. This is the reactive aspect of Akka. To understand Scalabilty, imagine that you need very very precise value. In that case, you would need to go real deep in the series i.e. the value of n should be quite high. The solution is to get more guys to calculate each term of series. And in akka it's too damn simple to get more guys. BUT, actors are very costly. In my first attempt, I summoned an actor for each term, like hiring a guy to calculate one single term and having n such guys. When I ran the code, it took like 10 seconds for n=1000. Utterly disappointed, I used just one guy to calculate value of every term. The result was expected, it took hardly half a second for n=1000. Have you seen the movie 'In Time'? You pay these actors/interns/guys with time. You hire more, you pay more. And at the same time you need to realize that you can't kill the intern with overwhelming workload. Sometimes, you will need another guy. So, next time you hire an actor, make it worth the cost.

In my pi code, first I used one actor, and things went well, then I increased n, and things started to go a little awry. The code took a looong time. So I decided to use one more guy for higher values. You see, it all depends on your need. You need one more guy, akka gives it you easily. This is scalability.
Here's my code for the pi, where n=1000000 and I use 2 actors to calculate to values of each term and another actor to sum em up.

Yes, I did note the timing difference between 1 actor code and 2 actor code.

1 Actor code

2 Actor code

You can see the difference yourself. It's all about getting the optimum no. of actors without breaking anything. I guess that's all about it. I hope you enjoyed reading this. And If you didn't, well here's something for you....

No comments:

Post a Comment

I am on Wordpress too.|No Copyright © 2014

Powered by Blogger.