Apache Kafka allows services to communicate by publishing and consuming events. But building a reliable event-driven system involves more than sending messages between services.
One of the most important decisions is how you design the events themselves.
A well-designed event clearly describes something that happened, contains the information consumers need, and remains useful as your system evolves. A poorly designed event can create tight coupling between services, introduce data inconsistencies, and make future changes difficult.
In this chapter, we'll look at the principles of designing good Kafka events, using an e-commerce order processing system as our running example.
An event is a record of something that has already happened in a system.
For example, when a customer places an order, the Order Service might publish an event called OrderPlaced.
Other services can consume this event and respond independently:
The diagram below shows one OrderPlaced event reaching all four consumers.
The Order Service doesn't need to know which services consume the event or what they do with it.
This separation is one of the main benefits of event-driven architecture.
However, it only works well when events have clear meanings and stable contracts.
The first principle is to design events around meaningful business facts rather than internal implementation details.
Consider these event names:
These names describe operations or instructions. They don't clearly communicate what has already happened.
Compare them with:
These describe completed business actions.
For example, PaymentAuthorized tells consumers that a payment was successfully authorized. They can use that information without knowing how the Payment Service performed the authorization.
A good event answers a simple question: What happened?
Event names should generally use past-tense verbs to communicate completed actions.
It's also important to define exactly what an event means.
Does OrderPlaced mean the customer clicked the checkout button, or that the Order Service successfully validated and saved the order?
Those are different events.
For our system, let's define OrderPlaced as:
An order has been validated, accepted, and successfully persisted by the Order Service.
This definition gives every consumer the same understanding of the event.
Once an event has been published, it should represent an immutable historical fact.
Suppose an order is placed with a total of $100.
The Order Service publishes:
Later, the customer applies a discount that reduces the total to $90.
We shouldn't modify the original OrderPlaced event. Instead, we publish another event:
Both events represent facts that happened at different points in time.
This approach allows consumers to understand how the system's state changed over time.
It also makes event replay useful. A consumer can process historical events to reconstruct its own state, provided the event stream contains all the relevant changes and the consumer handles ordering and duplicates correctly.
The key idea is that events describe changes; they aren't mutable database records.
A good event should contain enough information for consumers to understand and process it.
Let's design a more complete OrderPlaced event.
Let's understand the important fields.
eventId uniquely identifies a logical event.
This becomes particularly useful because Kafka consumers may receive the same event more than once.
For example, a consumer might process an event successfully but crash before committing its offset. When it restarts, Kafka may deliver the event again.
A consumer can use eventId to recognize previously processed events and avoid repeating side effects.
If the producer republishes the same logical event after a retry, it should preserve the original event ID rather than generating a new one. A fresh ID makes the duplicate look like a different event, so consumers can no longer detect it.
eventType identifies what happened.
Consumers can use it to determine how to interpret the event.
For example, OrderPlaced and OrderCancelled may require different processing logic.
eventVersion identifies the version of the event contract.
As systems evolve, you might add fields, change representations, or introduce new event types.
Version information can help consumers understand which contract an event follows.
However, version numbers don't replace proper schema compatibility rules.
occurredAt records when the business event happened.
This is different from when Kafka received the record or when a consumer processed it.
For example:
These timestamps can differ because of processing delays, network issues, and retries.
Keeping the original event time is particularly useful for analytics and event-time stream processing.
Use a consistent timestamp format, preferably UTC.
The remaining fields contain information about the order.
Notice that totalAmountMinor stores the amount in the currency's minor units, avoiding floating-point rounding problems. For USD, 9999 represents $99.99.
The exact structure should be documented so every consumer interprets the values consistently.
One common design question is how much information an event should contain.
Consider an OrderPlaced event with only an order ID:
This is simple, but consumers might need to call the Order Service to retrieve additional information.
If several consumers make these requests, we introduce extra network calls, latency, and dependencies on the Order Service's availability.
Alternatively, we could include the entire order object with customer details, billing addresses, shipping information, payment details, and other internal fields.
That creates a different problem.
Events become larger, expose unnecessary information, and tightly couple consumers to the producer's internal data model.
A better approach is to include the information consumers reasonably need to react to the event, without copying the entire database record.
For example, an OrderPlaced event might include order identifiers, product IDs, quantities, total amount, and currency.
It probably shouldn't contain unrelated internal fields such as administrative notes or database-specific metadata.
There isn't a universal rule for the ideal payload size.
The right decision depends on consumer requirements, event semantics, data sensitivity, and how frequently the information changes.
Every Kafka record can have a key.
The key is important because it influences which partition receives the record.
For our OrderPlaced event, a natural choice is:
If all events for the same order use orderId as their key, Kafka's key-based partitioning can place them in the same partition, assuming a consistent partitioning strategy and partition count.
This matters because Kafka guarantees record ordering within a partition, not across an entire topic.
For example:
A consumer reading that partition sees these records in their stored order.
However, using a key doesn't automatically guarantee correct business ordering. Producers must publish events in the intended order, and changes to partition counts or routing strategies can affect key placement.
The right key depends on the entity whose ordering and processing locality matter.
For order lifecycle events, orderId is usually a good choice.
A common mistake is designing an event specifically for one downstream service.
For example:
This event tells individual consumers what to do rather than describing the business fact.
If a new service is added, the producer may need to change its event structure.
That creates unnecessary coupling.
Instead, the Order Service should publish the fact that an order was placed.
Each consumer independently decides how to react.
The Notification Service may send an email, the Inventory Service may reserve products, and the Analytics Service may update its metrics.
Events should describe what happened, not dictate how every consumer should respond.
Some systems also use commands to request specific actions. Commands are useful, but they serve a different purpose from events.
Kafka is often used in systems where reliability matters.
That means event design should account for retries, duplicate delivery, and partial failures.
Consider this sequence:
OrderPlaced to Kafka.The database now contains the order, but downstream services never receive the notification.
The opposite problem can also occur: an event might be published even though the corresponding database transaction ultimately fails.
A common solution is the Transactional Outbox Pattern.
Instead of updating the database and publishing directly to Kafka as two independent operations, the service writes the business change and an outbox record in the same database transaction.
A separate process then reliably publishes the outbox record to Kafka.
The outbox flow looks like this.
This solves the problem of atomically recording the business change and the intent to publish, although duplicate publication is still possible.
The outbox guarantees that the business change and the outbox record are saved together, but the relay may publish the same record more than once. Stable event IDs and idempotent consumer processing remain important.
Events often remain in Kafka long after they were originally produced.
That means newer consumers may encounter older event formats, while older consumers may continue running after producers have been updated.
For example, suppose we want to add a discountAmountMinor field to OrderPlaced.
A generally safer change is to introduce it as an optional field or provide an appropriate default, depending on the serialization format and schema compatibility rules.
Consumers that don't recognize the field can continue processing the information they understand.
In contrast, renaming or removing a required field can break existing consumers.
Good event contracts should make future evolution possible without requiring every service to deploy simultaneously.
This becomes especially important in large organizations where producer and consumer teams release independently.
Schema Registry, compatibility modes, and contract versioning help manage these changes systematically.
Kafka events can be stored for extended periods and consumed by multiple applications.
Therefore, avoid publishing sensitive information unless it is genuinely necessary.
For example, an order event typically needs a customerId, not a customer's password, full payment card details, or other sensitive personal information.
Also consider what happens if a user requests data deletion. Once personal information is copied into multiple Kafka topics and downstream systems, removing it becomes more complicated.
Another practical consideration is event size.
Large payloads increase network usage, storage requirements, and processing overhead.
Rather than embedding large images, documents, or files inside Kafka records, store them in object storage and include references in the event.
For example:
Consumers can retrieve the document when needed, subject to appropriate access controls.
When designing a new Kafka event, ask yourself:
You don't need to include every possible field or anticipate every future consumer.
The goal is to create a clear, stable contract that captures the business event accurately and allows services to evolve independently.
Good Kafka event design starts with understanding the business, not the messaging infrastructure.
An event should communicate a meaningful fact, carry useful information, and remain reliable as the system grows.
Pay particular attention to event naming, immutability, identifiers, timestamps, payload structure, keys, and schema evolution.
These decisions directly affect how easily services can communicate, recover from failures, and evolve over time.
10 quizzes