Can we use any other database like MariaDB or MongoDB for Storing states in Kafka Streams instead of Rocks DB, is there any way to configure it?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka Streams is a client library for building applications and microservices where the input and output data are stored in Kafka clusters. By default, Kafka Streams uses RocksDB as a local storage engine to manage state. This choice is due to several beneficial characteristics of RocksDB, including high performance, reliability, and efficiency particularly for workloads involving large data sets and high throughput.
Why RocksDB is the Default
RocksDB is an embeddable persistent key-value store for fast storage. Here are several reasons why it is the default choice for Kafka Streams:
- Performance: Optimized for SSDs and fast storage, enabling high-performance read and write operations.
- Integration: It provides seamless integration with Kafka Streams, allowing for efficient state management and fault tolerance strategies.
- Compactness: Effective storage handling through compaction and compression mechanisms.
Considerations for Alternative Databases
When contemplating the use of other databases like MariaDB or MongoDB in place of RocksDB in Kafka Streams, several factors must be considered:
- Persistence and Durability: Kafka Streams applications often require the state store to be durable and recoverable in the case of failures.
- Latency: Access to stateful information needs to be efficient to maintain the high performance of stream processing.
- Scalability and Fault Tolerance: The database should scale seamlessly and support fault tolerance mechanisms in line with Kafka Streams.
Technical Feasibility: Using External Databases
Currently, Kafka Streams is tightly coupled with RocksDB, and there isn’t native support for any other databases directly within Kafka Streams for storing states. That being said, Kafka Streams allows for the implementation of custom state stores, which can provide some flexibility. This can be complex and requires significant custom development work, including:
- Implementing the State Store interface provided by Kafka Streams.
- Ensuring that the chosen database (e.g., MariaDB or MongoDB) can effectively serialize and deserialize the data.
- Handling the fault-tolerance and recovery mechanisms, likely using Kafka’s changelog topics to repopulate the state after a failure.
Example: Custom State Store with MongoDB
Below is a high-level conceptual strategy for configuring MongoDB as a state store:
- Implement the State Store Interface: Create new classes that implement the
StateStoreand possiblyStateStoreSupplierinterfaces from Kafka Streams. - Handling Serialization/Deserialization: Ensure efficient conversion mechanisms between native Kafka data types and MongoDB documents.
- Change Log Management: Implement logic to record state changes to a Kafka topic, which would be used to restore state in case of database or application restarts.
Challenges and Considerations
- Maintenance Overhead: Managing custom implementations adds complexity and requires ongoing maintenance.
- Performance: The performance might not match the one offered by native RocksDB, especially under heavy loads or high throughput scenarios.
- Compatibility and Updates: Ensuring compatibility with future Kafka Streams updates could be challenging.
Summary Table
| Feature/Aspect | RocksDB | MariaDB/MongoDB |
| Native Support in Kafka Streams | Yes | No |
| Custom Implementation Required | No | Yes |
| Performance | High | Varies |
| Complexity | Low | High |
| Fault Tolerance | Built-in | Requires custom setup |
Conclusion
While you can technically configure Kafka Streams to use databases like MariaDB or MongoDB, doing so involves substantial custom development and may introduce complexities related to performance, maintenance, and fault tolerance. Therefore, it’s generally advisable to stick with RocksDB unless there’s a compelling reason that justifies the additional overhead and complexity of an alternative database system.
Related reading
- Can we use multiple kafka template in spring boot?
- Can we use RabbitMQ and Mediatr together using masstransit?
- Can you scale DOWN the number of Kafka brokers in an Amazon MSK cluster?
- Can you split a stream into two streams?
- Can we use transact_write_items without specifying attribute types?
- Can you add a global secondary index to dynamodb after table has been created?
- Can you use Consul instead of Zookeeper for Kafka
- Cannot Connect from Kafkacat running in docker to Kafka broker running locally on windows machine

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.