messaging
system design
key/value pair
kafka

What's the purpose of Kafka's key/value pair-based messaging?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Kafka's key/value pair-based messaging is designed to provide flexibility, efficiency, and control over message routing, partitioning, and processing. Both the key and value in a Kafka message play specific roles in enabling Kafka's powerful capabilities.


Purpose of Kafka Key/Value Pair-Based Messaging

1. Message Partitioning

  • Key: Determines which partition a message is sent to.
    • Kafka uses a partitioner (default is hash-based) to map the key to a specific partition.
    • This allows for logical grouping of related messages.
  • Value: Contains the actual payload or data of the message.

Why It’s Useful:

  • Ensures message ordering for a particular key (e.g., all messages for a userId go to the same partition).
  • Enables parallelism, as partitions can be consumed independently.

Example:

  • Key: userId=123
  • Value: {"action": "login", "timestamp": "2024-01-01T12:34:56"}

In this case, all events for userId=123 will be routed to the same partition, maintaining their order.


2. Logical Grouping of Messages

The key enables logical partitioning, which groups related data together.

Example Use Case:

  • A payment processing system:
    • Key: transactionId
    • Value: { "amount": 100, "currency": "USD" }

By grouping all messages for the same transactionId, downstream systems can efficiently process all events related to a single transaction.


3. Custom Partitioning

You can implement a custom partitioner to override the default behavior and route messages to specific partitions based on business logic.

Example:

  • Key: region=US
  • Custom Partitioner: Routes all messages with region=US to a designated partition.

4. Message Deduplication

  • Keys can be used to deduplicate messages when combined with Kafka's log compaction feature.
  • Kafka retains only the latest message for a given key in compacted topics, ensuring data consistency.

Use Case:

  • Storing the latest state of a user or device:
    • Key: userId
    • Value: { "state": "active" }
  • Only the most recent state for each userId is kept, reducing storage overhead.

5. Stateful Processing

  • Keys are essential for stateful stream processing in tools like Kafka Streams or ksqlDB.
  • Operations like joins, aggregations, or windowing rely on keys to group related data.

Example Use Case:

  • Aggregating website visits by user:
    • Key: userId
    • Value: { "page": "home", "timestamp": "2024-01-01T12:00:00" }

Keys allow Kafka Streams to aggregate all events for the same userId and calculate metrics like total time spent on the website.


6. Integration with External Systems

  • Keys make it easier to integrate Kafka with databases or systems that require primary keys.
  • For example, when syncing data from Kafka to a database:
  • The userId key can directly map to a database row's primary key.

Summary of Key/Value Roles

AspectKeyValue
PurposeDetermines message routing to partitionsHolds the actual data or payload
Order GuaranteeEnsures ordering within a partition for the same keyNo direct impact on ordering
PartitioningUsed by partitioners to assign messages to partitionsNot used for partitioning
DeduplicationHelps deduplicate messages with log compactionN/A
Stateful ProcessingUsed to group and process related data (e.g., in Kafka Streams)Provides the content to be processed

When to Use Keys

  1. Ordering Guarantees:
    • Use keys when ordering is critical for related messages (e.g., events for a specific userId or transactionId).
  2. Message Grouping:
    • Use keys to group related messages in the same partition for easier processing.
  3. Stateful Processing:
    • Use keys for stateful operations like aggregations or joins.
  4. Log Compaction:
    • Use keys when you need to retain only the latest value for a given key (e.g., user profiles or device states).

When Keys Are Not Needed

  • If ordering or grouping isn't required, you can produce messages without a key.
  • Kafka will use a round-robin partitioning strategy, distributing messages evenly across partitions.

Example:

  • Key: null
  • Value: { "event": "page_view", "timestamp": "2024-01-01T12:00:00" }

Conclusion

Kafka’s key/value pair-based messaging provides flexibility to design efficient, scalable, and reliable systems. Use keys to:

  • Route and group messages logically.
  • Maintain ordering within partitions.
  • Enable deduplication and stateful processing.

For simple or high-throughput use cases where grouping or ordering isn’t critical, you can omit the key, and Kafka will handle the distribution for you.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.