DEV Community

Cover image for Event Sourcing with Lagom on GCP
Ido Shamun
Ido Shamun

Posted on • Originally published at Medium

Event Sourcing with Lagom on GCP

Lagom is a (very) opinionated microservice framework made by Lightbend (creators of Scala, Akka, Play more). Lagom is built on top of Akka and Play, supercharging it with an amazing engine to provide great performance. For those of you who still does not know Akka or Play I highly recommend to search it and look further. Two frameworks which I find very useful and reliable.

Lagom helps you build reactive systems, not just microservices, providing elasticity and resiliency out-of-the-box. Building Reactive Systems can be hard, but Lagom abstracts the complexities away. Akka and Play do the heavy lifting so you can focus on a simpler event-driven programming model on top, while benefiting from a message-driven system under the hood.

Event Sourcing

A paradigm shift of how we used to model our application’s state. Usually when something changes the state, we update the it and save it back to the database. In event sourcing instead of storing the state, you have to store the events (obviously…) which manipulates the application state. Using this event log we can later on reconstruct our state when needed. It is also easier to share events between services, keep everything immutable and change the data models whenever we want.

Martin Fowler has a great explanation if you would like to read further.

CQRS (Command Query Responsibility Segregation)

The core concept is to decouple the write model and read model. Usually we have one model which is used for everything but in CQRS we use two different models, one for inserting or updating data and the other for querying. It may be highly valuable to some cases and a total overhead to others. In my experience when your system evolves you will end up doing some kind of a CQRS.

Again, Marting Fowler has an amazing explanation.

Lagom promotes these two concepts and make it ridiculously easy to build an Event Sourcing & CQRS systems. At the heart of every service there are entities which react to commands. Each command persists one or more events and each event leads to the entity’s state change. We usually use HTTP calls or a message queue of some sorts to interact and send commands to these entities . To see a Lagom entity in action:

This sample application is archived. See https://github.com/lagom/lagom-samples for more Example projects showcasing Lagom usage.

Lagom

Gitter

Introduction

Lagom is a Swedish word meaning just right, sufficient. Microservices are about creating services that are just the right size, that is, they have just the right level of functionality and isolation to be able to adequately implement a scalable and resilient system.

Lagom focuses on ensuring that your application realises the full potential of the Reactive Manifesto, while delivering a high productivity development environment, and seamless production deployment experience.

This is a sample Scala auction site using the Lagom Framework. A java version of the auction site is also available.

Getting started

To get started make sure you have sbt and git installed on your system. You will also need an active internet connection.

Running: Prerequisites

  • You will need to download and run an Elastisearch server:
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.0.2.tar.gz
tar -xvf elasticsearch-5.0.2.tar.gz

But what this has to do with GCP?

Up until not so long ago, it had nothing to do with GCP but recently two major milestones were achieved.

The first one is the release of Lightbend Orchestration which ease and automates the deployment of Lightbend based systems, Lagom fortunately is one them. As a GCP customer, you probably love Kubernetes and lucky enough, Kubernetes is a first class citizen in Lightbend Orchestration. It includes out-of-the-box service discovery based on the DNS mechanism of Kubernetes, automatic bootstrapping of Akka clusters and it even generates all the YAMLs needed for deploying your services to Kubernetes. Up until now we had to do everything manually and let just say it is not a walk in the park.

The second milestone is the support of Google Pub/Sub as a message broker for Lagom services instead of Kafka (discolsure the support provided by a module written by me and not by Lagom team). Lagom has an amazing abstraction of the message broker API so you can basically migrate from Kafka to Pub/Sub and backwards by changing the class your service inherit from. The communication using a message broker decouples the services making them transparent to each other and improves the system resiliency.

I am using Lagom for over half a year now, been tackling very complicated issues and I am very happy with this choice. It makes everything much easier to develop and test. The community is still quite small but very active and helpful.

I encourage you to give Lagom a shot and let me know how it goes, I am here to help :)

Top comments (2)

Collapse
 
polentino911 profile image
Diego Casella

Glad to see posts targeting Scala/Akka ecosystem, keep posting!

Collapse
 
idoshamun profile image
Ido Shamun

Thanks! I'll try to keep them coming :)