meshIQ Blog | Apache Kafka®

Do you want to use Apache Kafka®? Or do you need a Queue?

meshIQ November 14, 2022

Do you want to use Apache Kafka®? Or do you need a message broker and queues? While they can seem similar, they have different purposes. I’m going to explain the differences, so you don’t try to brute force patterns and concepts in Apache Kafka® that are better used with a message broker. Partitioned Log Apache

Do you want to use Apache Kafka®? Or do you need a message broker and queues? While they can seem similar, they have different purposes. I’m going to explain the differences, so you don’t try to brute force patterns and concepts in Apache Kafka® that are better used with a message broker.

Partitioned Log

Apache Kafka® is a log. Specifically, a partitioned log. I’ll discuss the partition part later in this post and how that affects ordered processing and concurrency.

When a producer publishes new messages, generally events, to a log (a topic with Apache Kafka®), it appends them.

Events aren’t removed from a topic unless defined by the retention period. You could keep all events forever or purge them after a period of time. This is an important aspect to note in comparison to a queue.

With an event-driven architecture, you can have one service publish events and have many different services consuming those events. It’s about decoupling. The publishing service doesn’t know who is consuming, or if anyone is consuming, the events it’s publishing.

In this example, we have a topic with three events. Each consumer works independently, processing messages from the topic.

Because events are not removed from the topic, a new consumer could start consuming the first event on the topic. Apache Kafka® maintains an offset per topic, per consumer group, and partition. I’ll get to consumer groups and partitions shortly. This allows consumers to process new events that are appended to the topic. However, this also allows existing consumers to re-process existing messages by changing the offset.

Just because a consumer processes an event from a topic does not mean that they cannot process it again or that another consumer can’t consume it. The event is not removed from the topic when it’s consumed.

Commands & Events

A lot of the trouble I see with using Apache Kafka® revolves around applying various patterns or semantics typical with queues or a message broker and trying to force it with Apache Kafka®. An example of this is Commands.

There are two kinds of messages. Commands and Events. Some will say Queries are also messages, but I disagree in the context of asynchronous messaging.

Commands are about invoking behavior. There can be many producers of a command. There is a required single consumer of a command. The consumer will be within the logical boundary that owns the definition/schema of the command.

Events are about notifying other parts of your system that something has occurred. There is only a single publisher of an event. The logical boundary that publishes an event owns the schema/definition. There may be many consumers of an event or none.

Commands and events have different semantics. They have very different purposes, and how that also pertains to coupling.

By this definition, how can you publish a command to a Apache Kafka® topic and guarantee that only a single consumer will process it? You can’t.

Queue

This is where a queue and a message broker differ.

When you send a command to a queue, there’s going to be a single consumer that will process that message.

When the consumer finishes processing the message, it will acknowledge back to the broker.

At this point, the broker will remove the message from the queue.

The message is gone. The consumer cannot consume it again, nor can any other consumer.

This article originally appeared on codeopinion.com. To read the full article, click here.

Cookies preferences

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

Necessary

Necessary
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.