Day 70 and 71: Uncovering Akka
My Scala week is in its full swing, and therefore I thought it was time for a couple of more technical posts (today and tomorrow) to summarize my learnings. Topic of the day: Akka, as I found it is an incredibly useful and very often used toolkit, that I will definitely be encountering a lot more in the future.
So, what exactly is Akka?
To put simply, Akka is a toolkit that allows you to write concurrent and distributed applications, without having to worry much about all the underlying details behind threads and their management. Sounds pretty good, if you ask me. I remember how back in the University we had to really dig deep into the low-level thread management. I’d lie if I say I didn’t find it fun (up to a point), but having a set of tools that can do all that for me would certainly make things much easier.
As Akka is a toolkit, it - of course - consists of tools. Those are, namely:
- Akka Actors, which provides support for concurrency and distribution by implementing the Actor model
- Akka HTTP, which procides a way to build fast and asynchronous HTTP servers and clients
- Akka Streams, which provides support for asynchronous stream processing
I covered Akka Actors in one of my previous posts, while I decided to leave Akka Streams for the point when I will actually be needing them. Truth to be told, I did have them in my backlog for quite a while, but other stuff got in the way, unfortunately. Besides, perhaps it was not realistic to completely empty my backlog by the end of this apprenticeship (especially with me adding tasks to it at a much faster rate than I could possibly be able to finish them). Some compromises had to be made, and as a result Akka Http got bumped to the top of the priority list, while Streams remained to be done at some other time. But that’s ok, I think. I am prioritizing and adapting - and therefore learning how to live and work in a true Agile way :)
So Akka Http it is. Let’s dive a bit deeper into it.
According to the official documentation, Akka Http implements a full server- and client-side HTTP stack on top of Akka Actor and Akka Stream. At first I thought it was some kind of a web framework, but I guess it is a bit more general than that (perhaps a tool is the right term, after all). It contains a case-class based model of all major HTTP structures (requests, responses, headers, etc.), which allows you to provide and consume HTTP-based services in a rather straightforward way.
To be honest, I haven’t found much documentation on Akka Http besides the above (at least not as much as it’s the case with Akka Actors). That was a bit frustrating, but thankfully I came across this course on Codemunity, which provided me with some basics and a much needed practical example. Through a series of short videos, we built a small backend app for managing a To-do list.
In summary, here’s what I learned:
- How to define routes using the Akka Http DSL
- How to decode and encode JSON using the Circe library
- How to test the REST API using the Akka testkit
- How to dockerize the app using the sbt Native Packager
All in all, I found Akka Http quite simple to use and understand. The tasks themselves didn’t give me much trouble, thankfully. Not sure if the use case that I covered was way too simple, but I’m pretty confident that I would be able to adapt my learnings from it to more complex cases, with the usual help from StackOverflow, of course :)
Dockerizing the app, on the other hand, took a bit more time than I expected, until I got sbt completely set up on my Windows. But once I did, everything ran smoothly, so I don’t expect any more problems with that in the future. Needless to say, the fact that you can dockerize your app with just two simple commands:
sbt docker:stage and
sbt docker:publishLocal is one of the most useful features I’ve come across so far. No need to write a Dockerfile even. Sbt does all that for you.
That’s it for this post, but certainly not for this week. I have some more Scala-related matters to deal with before the weekend (the first skiing weekend of the season, yay!). I’ll make sure to write about it tomorrow, and also do a short reflection on my week, as always :)