Information Flow in Kafka Event‐Driven Application - ajay3003/doc GitHub Wiki
Information Flow in Kafka Event-Driven Application
1. Configuration Setup
- Source: The
appsettings.jsonfile contains the configuration details, such as the Kafka broker (BootstrapServers) and the topic to use (Topic). - Flow: At startup, the application reads these settings using
IConfigurationand injects them into the application via Dependency Injection (DI). - Target: The
KafkaSettingsmodel, which is shared across producer and consumer services.
2. Message Production (Producer Service)
- Trigger: The
Workerbackground service periodically generates a message to send. - Flow:
- The producer service (
KafkaProducerService) uses Kafka settings likeBootstrapServersandTopicto connect to Kafka. - The service creates a Kafka message containing the message payload (e.g.,
"Message at {DateTime.Now}"). - It sends this message to Kafka using the
Producemethod of the Kafka producer client.
- The producer service (
- Target: Kafka stores the message in the specified topic (e.g.,
test-topic).
3. Kafka Broker
- Role: Kafka acts as the central message bus. It stores messages in the specified topic, ensuring they are available for consumption by other components.
- Flow:
- Kafka maintains the message in the topic until a consumer acknowledges it or it expires (based on Kafka’s configuration).
4. Message Consumption (Consumer Service)
- Trigger: The
Workerbackground service runs the consumer in a separate task to listen for incoming messages. - Flow:
- The consumer service (
KafkaConsumerService) uses Kafka settings to subscribe to the topic. - It continuously polls Kafka for new messages.
- When a new message arrives, the consumer service processes it (e.g., logs it to the console or triggers further processing).
- The consumer service (
- Output: The received message is processed and displayed/logged.
Information Flow Summary
- Input Configuration: Settings like Kafka broker address and topic name are read from
appsettings.jsonand injected into the producer and consumer. - Producer Action: The producer generates and sends a message to Kafka.
- Kafka Action: Kafka receives the message and stores it in the topic.
- Consumer Action: The consumer retrieves the message from Kafka and processes it.
Example Flow
-
Producer:
- Sends a message:
"Message at 2024-12-19 10:15:00" - Kafka stores this message in the
test-topic.
- Sends a message:
-
Kafka Broker:
- Receives and queues the message in the
test-topic.
- Receives and queues the message in the
-
Consumer:
- Retrieves the message:
"Message at 2024-12-19 10:15:00". - Logs the message:
Message received: Message at 2024-12-19 10:15:00.
- Retrieves the message:
Diagram of Information Flow
[Configuration (appsettings.json)]
|
v
[KafkaProducerService] --> [Kafka (Broker/Topic)] --> [KafkaConsumerService]
| |
v v
[Generate Message] [Process/Log Message]
What is happening "under the hood"?
- Producer: Creates a connection to Kafka using the
BootstrapServersand serializes the message. - Kafka: Manages the topic as a queue and stores the serialized messages until consumed.
- Consumer: Connects to Kafka using the same broker address and subscribes to the topic, deserializing and processing messages as they arrive.
This flow ensures a loosely coupled, scalable, and fault-tolerant communication system.