The yarn of Lagom: Bye-bye Monoliths!

The Microservice architecture is in an uproar, in the industry and the business domain. The Microservice architecture tackles the drawbacks associated with the Monolithic architecture. The Lagom framework is a front-runner in the development of modern enterprise applications.

This blog is the first in a multi-part series on The Lagom framework. In this blog, I’d take you through the following topics:

  • Why Microservices and not Monoliths?!
  • What is Lagom? And the motivation behind it
  • Creating and running a project from the Lagom Giter8 template.

What are Microservices?

Microservices – also known as the microservice architecture – is an architectural style that structures an application as a collection of services that are: 

  • Highly maintainable and testable
  • Loosely coupled
  • Independently deployable
  • Organized around business capabilities.

In gist, The Microservice architecture specifies that a monolith must be broken down into a collection of small autonomous services that are modelled around a Business Domain.

Why Microservices and not Monoliths?!

Monolith means “composed all in one place“. A monolithic application describes an application with different components combined into a single program. Think of it like a Lasagna gone bad; This architecture worked successfully for many years. In fact, many of the giants were initially developed as a monolith. But with the ever-changing IT industry and with the emergence of new technologies, a paradigm shift was long overdue. Ergo, The Microservice architecture. When I talk about Microservices I want you to think about Netflix and Amazon.

Microservices are business-driven and can be scaled quickly and cheaply with high performance and availability. Designing new apps as collections of small, granular services means they can be coded quickly while also being available, scalable and able to evolve. When deployed correctly, microservices can help enterprises transition to a responsive infrastructure and improve the services delivered with monolithic architectures.

The Microservice architecture aims to tackle the drawbacks associated with the relatively ancient Monolithic Architecture(No hate, please!). Following are some of the problem areas where the Microservice architecture outshines the Monolithic architecture:

  • Reliability: Since Microservices involve breaking down what would otherwise be a ginormous system into smaller, autonomous services that are responsible for their own state and persistence thereof, reliability increases. If one of the services go down the whole system doesn’t get affected, only the services that are dependent on that failed service get affected, because of that particular service’s downtime.
  • Continuous deployment: Continuous deployment becomes easier. In order to update one component, we have to redeploy only that particular microservice.

What is Lagom?

Lagom is a Swedish word meaning “just the right amount“. Lagom helps us to build micro-services as reactive systems so that the micro-services are resilient, elastic and scalable from within.

Lagom provides an opinionated framework that acts like guide rails to speed you along the journey. Lagom tools and APIs simplify the development and deployment of a system that includes microservices.

The Lagom design philosophy emphasises that one should not focus on splitting a system into smallest possible services, but to split the system into services that are just the right size.

Creating and running a project from the Lagom Giter8 template

Lagom exposes two APIs, Java and Scala, and provides a framework and development environment as a set of libraries and build tool plugins.
To take advantage of Lagom’s high productivity development environment, it is suggested to use it with one of the following supported build tools:

SBT – as the build tool for the Lagom Scala Api
Maven – as the build tool for the Lagom Java Api

Prerequisites

  • Java Development Kit (JDK), version 8
  • sbt 1.x

Create the Build

Probably the easiest way to create the build is by downloading the Tech Hub Project Starter, which uses the Lagom Giter8 template to generate a compressed file which contains the project.

Browse the Build

The created project contains the following elements:

hello                   → Project root
 └ hello-api            → hello api project
 └ hello-impl           → hello implementation project
 └ hello-stream-api     → hello-stream api project
 └ hello-stream-impl    → hello-stream implementation project
 └ project              → sbt configuration files
   └ build.properties   → Marker for sbt project
   └ plugins.sbt        → sbt plugins including the declaration for Lagom itself
 └ build.sbt            → Your project build file

Each service is broken up into two projects: API and implementation. The API project contains a service interface which specifies the contract between the service and its implementation. The IMPL project contains the actual service implementation.

Run Hello World

Lagom includes a development environment that let you start all your services by simply typing inrunAll the sbt console. 
To run Hello World, change directories to the top-level directory and start sbt, when the command prompt displays, invoke runAll

cd hello
sbt
... (booting up)
> runAll

It would take a bit of time to build the project and start the services. Among other things you should see the following:

[info] Starting embedded Cassandra server
..........
[info] Cassandra server running at 127.0.0.1:4000
[info] Service locator is running at http://localhost:9008
[info] Service gateway is running at http://localhost:9000
[info] Service hello-impl listening for HTTP on 0:0:0:0:0:0:0:0:24266
[info] Service hello-stream-impl listening for HTTP on 0:0:0:0:0:0:0:0:26230
(Services started, press enter to stop and go back to the console...)

Now verify that the services are indeed up and running by invoking one of its endpoints from any HTTP client, such as a browser:

http://localhost:9000/api/hello/Knoldus

Now you know how to set up a Lagom project and run it. This is probably the easiest way to get started with Lagom. Stay tuned for the next part in the series where I’d write about setting up a project from scratch and some of Lagom’s core concepts.

Until next time! 🍺 🚀

References

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!