0
0
Kafkadevops~5 mins

Event choreography vs orchestration in Kafka - CLI Comparison

Choose your learning style9 modes available
Introduction
When multiple services need to work together, they can either coordinate themselves by listening and reacting to events, or be controlled by a central manager that tells each service what to do. Event choreography lets services talk directly by sending events, while orchestration uses a central controller to manage the flow.
When you want services to be loosely connected and react independently to events without a central controller.
When you need a central system to control the order and logic of service interactions clearly.
When building a microservices system where services publish and subscribe to Kafka topics to communicate.
When you want to avoid a single point of failure by letting services coordinate themselves.
When you want clear visibility and control over the workflow by using a central orchestrator.
Commands
Create a Kafka topic named 'order-created' where services will publish events when an order is created.
Terminal
kafka-topics --create --topic order-created --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
Expected OutputExpected
Created topic order-created.
--topic - Name of the Kafka topic to create
--partitions - Number of partitions for the topic
--replication-factor - Number of copies of the topic for fault tolerance
Start a producer to send an event message to the 'order-created' topic, simulating an order creation event.
Terminal
kafka-console-producer --topic order-created --bootstrap-server localhost:9092
Expected OutputExpected
No output (command runs silently)
--topic - Topic to send messages to
--bootstrap-server - Kafka server address
Consume the first message from the 'order-created' topic to verify the event was published.
Terminal
kafka-console-consumer --topic order-created --bootstrap-server localhost:9092 --from-beginning --max-messages 1
Expected OutputExpected
{"orderId": "12345", "status": "created"}
--from-beginning - Read messages from the start of the topic
--max-messages - Stop after reading specified number of messages
Create a Kafka topic 'order-processor' to simulate an orchestrator sending commands to process orders.
Terminal
kafka-topics --create --topic order-processor --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
Expected OutputExpected
Created topic order-processor.
--topic - Name of the Kafka topic to create
--partitions - Number of partitions for the topic
--replication-factor - Number of copies of the topic for fault tolerance
Send a command message to the 'order-processor' topic to simulate orchestration controlling the order processing.
Terminal
kafka-console-producer --topic order-processor --bootstrap-server localhost:9092
Expected OutputExpected
No output (command runs silently)
--topic - Topic to send messages to
--bootstrap-server - Kafka server address
Key Concept

If you remember nothing else from this pattern, remember: choreography lets services react to events independently, while orchestration uses a central controller to direct service actions.

Common Mistakes
Trying to use orchestration without a central controller service.
Orchestration requires a central system to send commands; without it, services cannot be coordinated properly.
Set up a dedicated orchestrator service that listens and sends commands to other services via Kafka topics.
Publishing events without consumers listening in choreography.
Events will be lost or ignored if no service subscribes to the Kafka topics, breaking the workflow.
Ensure all relevant services subscribe to the correct Kafka topics to react to events.
Summary
Create Kafka topics to represent events or commands for services to communicate.
Use producers to send messages representing events or orchestration commands.
Use consumers to listen and react to events in choreography or follow commands in orchestration.