How to configure Kafka to behave like a FiFo queue?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a high-throughput, low-latency publishing and subscribing solution, it also comes with features that allow it to be configured as a FIFO (First In, First Out) queue. Although Kafka is not a traditional messaging system and is designed primarily for distributed data streaming, with proper configuration, it can emulate FIFO queue behavior where messages are processed in the exact order they are produced. Here's how you can configure Kafka to behave like a FIFO queue:
Step 1: Create a Single Partition Topic
Kafka retains messages in the order they are received only within a single partition. Therefore, to maintain the order of all messages, you must use a topic with only one partition. Here's how you can create a single partition topic:
Step 2: Producer Configuration
Ensure that the producer's acks setting is all to guarantee that messages are replicated across all replicas before an acknowledgment is sent. This enhances the reliability of the system.
Always use the same key or a null key to ensure that all messages go to the same partition.
Step 3: Consumer Configuration
Make sure you configure your consumer properly:
- Enable auto commit to false to manually control the record offset.
- Set
max.poll.recordsto1to process one message at a time.
Step 4: Managing Offsets
Handling offsets manually is crucial. After processing each message, manually commit the offset. This ensures that each message is processed in order.
Best Practices and Considerations
- Immutability of Messages: Once a message is written to a Kafka topic, it cannot be changed. Ensure that messages are correct before sending them.
- Failure Handling: Carefully handle possible exceptions in your consumer logic, such as processing errors from which the application must recover.
Performance Considerations
While configuring Kafka to act as a FIFO queue, one must be aware of potential performance impacts due to:
- Single Partition: Limits the scalability as throughput is capped by a single partition's performance.
- Manual Offset Management: Can slow down processing if not handled efficiently.
Summary Table
| Configuration Key | Recommended Setting | Description |
| Partitions | 1 | Ensures message order within the topic. |
Producer acks | all | Ensures data durability and consistency. |
Consumer max.poll.records | 1 | Forces consumer to process one message at a time. |
| Consumer commit | Manual (commitSync) | Gives full control over when a message is considered "consumed." |
| Key Serialization | Consistent or null | Ensures all messages go to the same partition. |
In conclusion, configuring Kafka as a FIFO queue involves strategic settings and trade-offs. Maintaining message order requires sacrificing some of Kafka's natural advantages like horizontal scalability and high-throughput performance for multiple partitions. However, for use cases where message order is paramount and large-scale throughput is less critical, these trade-offs can be justified.
Related reading
- How to configure kafka topic retention policy during creation with Spring?
- How to configure logging for Kafka producers?
- How to configure RabbitMQ connection with spring-rabbit?
- How to configure RabbitMQ connection with spring-rabbit?
- How to configure RabbitMQ using Active/Passive High Availability architecture
- how to configure redis ttl with spring boot 2.0
- How to configure spring-data-mongodb to use a replica set via properties
- How to consolidate date ranges in a list in C

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.