Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Event-Driven Architectures and Message Brokers

Event-Driven Architectures (EDAs) revolve around events:

An EDA includes channels, operations, and components:

For example, after receiving a request from a frontend, a user management microservice might publish an event to a userSignedUp topic including a user ID and email address. Another microservice subscribed to this topic might process this event by applying discount credits to the new user account for having signed up on a promotional day.

A topic can have any number of producers or consumers (including zero). For example, a zero-consumer EDA can be used solely for storing events.

A component can both publish and subscribe to the same topic.

Sending and waiting for an event is non-blocking. A producer does not wait for an event it has sent to be received or processed by a consumer, and a consumer can do other things while waiting to receive an event.

In an EDA, events are responded to immediately, which removes the need for polling and thus reduces both wait times and unnecessary computation. In an extreme example, if you did not use an EDA and had enough services polling the next one in a chain, their polling rates would accumulate into a longer wait time; an EDA avoids this.

Kafka

Apache Kafka supports subscribing to, publishing, storing, real-time processing, and retroactive processing of events.

It follows a client-server architecture.

It is a distributed, scalable, elastic, fault-tolerant, and secure system consisting of a cluster of one or more servers:

Following the Kafka architecture, clients subscribe to, publish, and process events. REST APIs are available for this, as well as client libraries for languages including Go, Python, and more.

Event storage

APIs

Kafka Quickstart

Download Kafka:

wget https://dlcdn.apache.org/kafka/4.1.1/kafka_2.13-4.1.1.tgz

Extract and navigate within the downloaded zip folder:

tar -xzf kafka_2.13-4.1.1.tgz
cd kafka_2.13-4.1.1

Download the Kafka Docker image and run as a container:

docker pull apache/kafka:4.1.1
docker run -p 9092:9092 apache/kafka:4.1.1

Create a quickstart-events topic:

bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092

Get information about the topic:

bin/kafka-topics.sh --describe --topic quickstart-events --bootstrap-server localhost:9092

Write events to the topic:

bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092

Read events from the topic:

bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server localhost:9092

Configure Kafka Connect to look in connect-file-4.1.1.jar for connector classes:

echo "plugin.path=libs/connect-file-4.1.1.jar" >> config/connect-standalone.properties

Create two lines of test data in test.txt:

echo -e "foo
bar" > test.txt

Start a standalone version of Kafka Connect, configured via the config/ connect-standalone.properties file and with two connectors (a FileStreamSource connector and a FileStreamSink connector):

bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties

The FileStreamSource and FileStreamSink connectors are configured by the config/connect-file-[source/sink].properties files to read test.txt into the connect-test topic and write from that topic into test.sink.txt, respectively.

Verify that FileStreamSource successfully read events from test.txt:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic connect-test --from-beginning

Verify that FileStreamSink successfully wrote the events to test.sink.txt:

more test.sink.txt

Clean up:

rm -rf /tmp/kafka-logs /tmp/kraft-combined-logs