How do I implement in memory or embedded kafka not for testing purposes?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When setting up an in-memory or embedded Kafka instance, it is common to think of it as a tool exclusively for testing purposes. However, there are scenarios outside of testing where such a setup can be incredibly useful, like in development environments, during prototyping, or for educational purposes. This article will guide you through implementing an in-memory or embedded Kafka, focusing primarily on non-testing applications.
Understanding Embedded and In-Memory Kafka
Embedded Kafka refers to running Kafka embedded within another Java application, typically for testing purposes. The Kafka server runs in the same JVM as the application, easing configuration, management, and interaction. In-Memory Kafka, on the other hand, is typically a subset or an extended version of embedded Kafka and does not persist data to disk, making it volatile and fast.
Why Use it Outside Testing?
- Development: Facilitates developers to work with Kafka without needing to set up an external cluster.
- Prototyping: Quickly prototype ideas without setting up complex infrastructure.
- Training and Education: Allows learners to experiment with Kafka without any setup overhead.
Key Components
- Apache Kafka: The core brokers that handle message storage and distribution.
- ZooKeeper: Manages the Kafka cluster configuration and coordination.
- Kafka Streams: For stream processing directly integrated into the JVM application.
Setup Using Kafka Streams
Kafka Streams includes an in-memory state store and stream processing capabilities, which can provide a light impression of using Kafka in-memory. Below is a simple setup:
This simple Kafka Streams application reads from the input-topic, filters messages based on value length, and writes to the output-topic. Kafka Streams handles the heavy lifting of interacting with the Kafka broker.
Embedded Kafka with Spring
For a more genuine in-memory or embedded usage, you can integrate Kafka with Spring Kafka:
This example initializes an embedded Kafka broker and interacts with it through a Spring-based Kafka consumer. Even though this is set up in a test (annotated with @Test), the same approach can be applied outside tests by configuring your Spring application correctly.
Considerations
| Consideration | Details |
| Data Persistence | In-memory Kafka does not persist data, making it inappropriate for production scenarios. |
| Resource Usage | It can consume significant application resources, affecting overall performance. |
| Scalability | Scaling an embedded or in-memory instance for large applications can be impractical or complex. |
| Use Case Limitation | Best suited for development, small-scale prototyping, or educational purposes. |
Conclusion
Using in-memory or embedded Kafka outside of testing environments can be beneficial for development, learning, and experimentation. However, understanding its limitations in terms of data persistence, resource usage, and scalability is crucial. Always ensure the Kafka setup aligns with the application's goals and requirements. For actual production environments, a dedicated Kafka cluster is recommended.
Related reading
- How do I implement Kafka Consumer in Scala
- How do I initialize a CuratorFramework for a ZooKeeper cluster with dynamic size?
- How do I initialize the whitelist for Apache-Zookeeper?
- How do I install an extension of Kafka for PHP?
- How do I run celery status/flower without the -A option?
- How do I set a number of retry attempts in RabbitMQ?
- How do I set a number of retry attempts in RabbitMQ?
- How do I set the size of messages in Kafka?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.