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:
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:
Service Implementation:
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:
| Component | Description |
| Service API | Defines the service and its topics to be consumed. |
| Implementations | Contains the business logic to handle the data consumed from or published to Kafka. |
| TopicProducer | A utility to aid in consuming event streams and publishing them to Kafka topics. |
| Serialization | Handles 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.

