Lagom Framework
Kafka
Microservices
Data Streaming
Java

Lagom service consuming input from Kafka

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Lagom is a microservices framework built on top of the Lightbend Reactive Platform, particularly useful for creating scalable, resilient systems. Lagom is designed to simplify the development of such systems by providing integrated tooling and promoting best practices. Kafka, on the other hand, is a distributed streaming platform capable of handling trillions of events a day. Integrating these two technologies allows developers to harness the robust messaging capabilities of Kafka with the microservices architecture of Lagom.

Understanding Lagom's Kafka Integration

Lagom provides an easy way to communicate between services using asynchronous message brokers, with Apache Kafka being a first-class citizen for message delivery. Kafka’s key capabilities, such as high-throughput, scalable, and fault-tolerant pub-sub messaging, are ideal complements to the reactive design principles that underpin Lagom.

Key Components:

  • Service API: Used to define the interfaces that services will implement.
  • Impl Project: Contains the implementation of the services defined in the API.
  • Broker API: Lagom’s API for interacting with message brokers like Kafka.
  • Subscriber: A service that reads messages from a Kafka topic.
  • Publisher: A service that writes messages to a Kafka topic.

Setting Up Kafka with Lagom

Before consuming messages from Kafka, you must ensure that your Lagom setup includes the Kafka broker. Modification in the build.sbt or equivalent Maven file is required to include Kafka dependencies:

scala
libraryDependencies += lagomScaladslKafkaBroker

Also, you will need to start Kafka and Zookeeper in your local or development environment, which can typically be done using Confluent's platform or with Docker containers.

Consuming Messages from Kafka

To consume messages from Kafka, a service needs to subscribe to a topic. This process involves defining a Subscriber in the service implementation.

Example:

Here is a basic example of a Lagom service subscribing to a Kafka topic.

Service API:
scala
1trait UserService extends Service {
2  def greetingsTopic(): Topic[String]
3
4  override final def descriptor = {
5    import Service._
6    named("user-service")
7      .withTopics(
8        topic("greetings", greetingsTopic)
9          .withProperty(KafkaProperties.partitionKeyStrategy, PartitionKeyStrategy[String](_.name))
10      )
11      .withAutoAcl(true)
12  }
13}
Service Implementation:
scala
1class UserServiceImpl(readSide: ReadSide, db: Database) extends UserService {
2
3  override def greetingsTopic() = TopicProducer.singleStreamWithOffset { fromOffset =>
4    persistentEntityRegistry.eventStream(UserEvent.Tag, fromOffset)
5      .mapConcat(transformEventToEnvelope)
6  }
7
8  private def transformEventToEnvelope(event: EventStreamElement[UserChanged]): Seq[Envelope[String, UserMessage]] = {
9    event.event match {
10      case UserUpdated(user) => List(Envelope(user.name, UserMessage(user.name, user.status)))
11      case _ => List.empty
12    }
13  }
14}

In this example, UserService defines a topic named greetings. The service implementation UserServiceImpl uses TopicProducer.singleStreamWithOffset, which is a method provided by Lagom to consume events from the event journal and publish to a Kafka topic.

Key Concepts:

  • Event Stream: Events are published by various parts of your application and consumed by others.
  • TopicProducer: Helps produce messages to a topic based off the event stream.

Handling Serialization

Messages sent over Kafka need to be serialized in order to be transmitted over the network. Lagom provides built-in support for JSON serialization and also allows the use of other formats, such as Avro, if needed.

To correctly serialize and deserialize data, one would typically define SerializerRegistry in their service.

Summary

Below is a table highlighting the main components involved in consuming Kafka messages with Lagom:

ComponentDescription
Service APIDefines the service and its topics to be consumed.
ImplementationsContains the business logic to handle the data consumed from or published to Kafka.
TopicProducerA utility to aid in consuming event streams and publishing them to Kafka topics.
SerializationHandles conversion of messages into a format suitable for message transmission.

Conclusion

Integrating Lagom services with Kafka allows developers to take advantage of a fully reactive system that can handle high-throughput and distributed data streams efficiently. The usage of Lagom’s built-in features for Kafka integration minimizes the boilerplate code and setup needed, letting developers focus more on delivering business value.


Course illustration
Course illustration

All Rights Reserved.