Meetup: Akka in Kotlin

Every last Friday of the month we organise an internal meetup at Sourcelabs. This time it was my turn to prepare the subject: Akka in Kotlin.

Akka is an implementation of an Actor Model system. Whilst preparing the workshop I read parts of “Reactive Messaging Patterns with the Actor Model: Applications and Integration in Scala and Akka” which uses the following notation (often used to describe actor systems):

Parent creates child:

Actor self terminates:

And parent terminates child:

Actors self-terminating, parents terminating children. The world of software engineering can be brutal sometimes.

The actor model is a way of creating a system where each node is an Actor.

An Actor:

  • Has state

  • Is isolated from other actors and does not share memory

Every Actor can:

  • create another actor (a child actor)

  • receive messages and upon which change state

  • send messages

BUT! actors can only send messages to actors they know about. Actors can learn about other actors by:

  • creation, you know your children

  • references, you can pass an actor reference

Akka is an implementation of an Actor system. And like other implementations (like Elixir) it follows the paradigm but there are a few minor exceptions.

Akka offers a Scala and Java DSL. Sourcelabs has mainly Kotlin programmers and so we decided to program in Kotlin against the Java DSL which wasn’t too difficult, even for someone like me who is still very much working in Java on a daily basis.

At our meetup, we experimented with a couple of Hello world examples, for instance, this one showing the basics of message handling:

After a couple more simple examples, like playing around with the Akka Receptionist, we were left with the question “Ok… this is nice and all but what does a real-life implementation look like?”

To answer this question we followed this tutorial: Shopping Cart example architecture :: Akka Platform Guide  Where you implement a shopping cart using technologies that felt a bit more tangible and more akin to what we’re used to dealing with on a day to day basis, namely:

  • gRPC

  • Persistent Event Sourcing

  • PostgreSQL

  • Kafka logs

To be honest, the biggest challenge for us wasn’t the application itself, or even converting the sample code from Java to Kotlin but getting the Maven Java/Kotlin/gRPC compiler plugins to all play nicely with each other. But being an incredibly stubborn person and not being officially “on the clock” as I am when I’m working at my consultancy post I insisted we hack away until it was all working perfectly. But it did mean we didn’t quite reach the end of the Shopping Cart tutorial.

However! We did get quite a bit of it done, enough to realise a few things

  • Akka offers a tremendous amount of functionality. Just check out this Modules overview Overview of Akka libraries and modules • Akka Documentation

  • Akka is easy to implement (at least at the start). It’s very Plug’n’Play and follows the same pattern of “Add dependency->add a couple of lines of config->implement a small class the extends/implements something” that we’re all used to.

At the end of the day I was still left rather perplexed as to the relationships between Akka and the Actor system. It would have been the same as if I was a C programmer who spent the morning learning about Java classes and the afternoon implementing a Spring Boot microservice. There’s just too much ground to cover in-between to really appreciate the thing as a whole.

Still, I was very happy to be doing something new in IT and dip my toes into a new ecosystem, I learned a bit more about Kotlin and Maven and I got to spend the day with my Sourcelabs colleagues in our big new office.

Did you enjoy this content? We have got more on the way! Sign up here for regular updates!

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *