Apache Kafka is a powerful tool that helps in building event-driven microservices. But before diving deeper, let’s understand the fundamental concept of an event.
What is an Event?
In Apache Kafka, an event is an indication that something has happened in the system. For example:
- When a user logs into a website, a login event is generated.
- When a new product is created, a product created event is published.
- When an order is placed, an order placed event is generated.
Events represent changes in the state of an application or system. They act as a mechanism to notify other services about changes occurring within the system.
Naming Conventions for Events
In event-driven microservices, naming events follows a specific convention:
- Event names should be in simple past tense.
- The name should follow this structure: Noun + Performed Action + “Event”.
For example:
- When a product is created:
ProductCreatedEvent
- When a product is shipped:
ProductShippedEvent
- When a product is deleted:
ProductDeletedEvent
This naming convention ensures clarity and consistency in event-driven communication.
Kafka Events vs. Kafka Messages
Many resources use the terms event and message interchangeably, but there is a distinction:
- A Kafka message is like an envelope that contains data.
- The event data is the content inside that envelope.
Kafka Message Structure
A Kafka message consists of three primary components:
- Message Payload (Value)
- This contains the actual event data.
- The data can be in various formats such as:
- Simple string
- JSON
- Other serialized formats (e.g., Avro, Protobuf)
- Even
null
if required
- Example JSON payload for a
ProductCreatedEvent
:
{ "productId": "12345", "productName": "Laptop", "price": 75000, "createdAt": "2025-02-21T10:30:00Z" }
- The default size limit of a Kafka message payload is 1 MB.
- Large data (e.g., images or videos) should be referenced via a URL instead of embedding them directly.
- Message Key
- This helps Kafka to determine partitioning.
- Can be an alphanumeric string or a custom format.
- Example: A product ID (
"12345"
) can be used as a key to ensure messages related to a product are stored in the same partition.
- Timestamp
- Can be set by the system or manually assigned.
- Useful for tracking when an event occurred.
Optional Components
- Headers: Key-value pairs used to store metadata.
- Example: An authorization token in the header for authentication.
Storing and Ordering Events in Kafka
- Events are stored in Kafka topics as byte arrays.
- Messages are serialized by the producer and deserialized by the consumer.
- Ordering of events is achieved using the message key, ensuring events with the same key land in the same partition.