Kafka as an Akka-persistence journal
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, a distributed event streaming platform capable of handling trillions of events a day, is commonly used for building real-time data pipelines and streaming applications. Integrating Kafka with Akka Persistence as a backend journal storage system offers numerous benefits including high-throughput, scalability, fault-tolerance, and the harnessing of stream processing capabilities alongside event sourcing.
Understanding Akka Persistence
Akka Persistence allows actors in Akka to recover their state after restarts or crashes by persisting their state changes as events in a journal. It supports event sourcing, which is a practice of capturing all changes as immutable events and replaying them to restore the last state of an entity.
Kafka As an Akka-persistence Journal
Kafka can be used as a journal storage system in Akka Persistence through custom plugins. This setup leverages Kafka's robust message handling capabilities to store the sequence of events generated by Akka actors.
Technical Implementation
To use Kafka as a journal for Akka Persistence, developers typically employ a plugin where Kafka topics are used to store the events. Each persistent actor has its messages persisted into a unique Kafka topic, or a shared topic partitioned by the persistence ID.
In this sample, each command received by the actor results in an event that is persisted into Kafka. Recovery involves reading these events back and using them to restore the actor’s state.
Benefits of Using Kafka
- Scalability: Kafka’s distributed nature and partitioning capabilities make it inherently scalable.
- Durability: Kafka stores data on disk and replicates for fault tolerance.
- High Performance: Kafka’s append-only log structure on disk provides high write and read throughput.
Challenges and Considerations
- Event Ordering: Kafka guarantees order within a partition, but across partitions, this is not assured. Care must be taken when partitioning event topics.
- Replay and Snapshots: Kafka is not optimized for random reads, which can be a challenge when replaying events to recover actor state. Using snapshots effectively can mitigate this issue.
Additional Features
- Stream Processing: Kafka’s stream processing capabilities can be utilized for real-time event processing and aggregation directly from the journal.
- Tooling and Ecosystem: Kafka’s rich tooling and ecosystem can be leveraged for monitoring, security, and operational management.
Summary Table
| Feature | Description |
| Scalability | Distributed system, naturally partitioning and scalable. |
| Durability | Events are stored on disk and replicated for fault-tolerance. |
| Throughput | High performance with append-only storage. |
| Event Ordering | Order is guaranteed within a partition. |
| Stream Processing | Can use Kafka Stream for real-time analytics and processing. |
| Integration Effort | Requires implementation of a custom Akka Persistence plugin for Kafka. |
Conclusion
Using Kafka as a journal storage for Akka Persistence provides a robust, scalable, and high-throughput environment suitable for event-driven applications. While some challenges exist, particularly around event ordering and state recovery, careful design and leveraging Kafka’s strengths can lead to a powerful reactive system architecture. This integration not only capitalizes on Kafka’s performance but also on its stream processing capabilities, making it an excellent choice for modern, reactive architectures that require durable event sourcing.
Related reading
- kafka as event store in event sourced system
- Kafka async Commit Offset Replication
- kafka asynchronous send not really asynchronous?
- Kafka Authentication Producer Unable to Connect Producer
- kafka cluster configuration
- kafka connect hdfs sink connector is failing even when json data contains schema and payload field
- Kafka AVRO - conversion from long to datetime
- kafka avro console consumer does not deserialize DECIMAL correctly as decimal

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.