メッセージキーとパーティショニング戦略
Kafkaがメッセージキーでレコードをパーティションへ振り分ける方法、順序性が保証される仕組み、キーとカスタムパーティショナーの設計方法を学びます。
「メッセージキーとパーティショニング戦略」はCoddyKit上の無料Apache Kafka & Stream Processing Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはApache Kafka & Stream Processing Fundamentals学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Apache Kafka & Stream Processing Fundamentalsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Role of the Key
Every Kafka record can carry an optional key. The key is not just data — it determines which partition the record is written to.
Default Partitioner
When a key is present, the default partitioner hashes it and maps the hash to a partition. The same key always lands in the same partition (for a fixed partition count).
partition = hash(key) % numPartitionsNo Key Behavior
If you send records without a key, the producer distributes them across partitions (sticky batching in modern clients) for balanced load — but ordering across partitions is not guaranteed.
Ordering Guarantee
Kafka guarantees ordering only within a partition. By keying related records (e.g. all events for one order id), you guarantee they are processed in order.
send("orders", orderId, event); // all events for an order stay orderedChoosing a Good Key
A good key reflects your ordering and grouping needs.
- User events keyed by userId.
- Order events keyed by orderId.
- Avoid keys with too few distinct values (causes hot partitions).
Hot Partitions
If one key value dominates traffic, its partition becomes a hotspot while others sit idle. Watch key cardinality and distribution to keep load balanced.
Keying in Spring Boot
Pass the key as the second argument to send; Spring forwards it to the partitioner.
kafkaTemplate.send("orders", order.getId(), payload);Custom Partitioner
For special routing logic, implement the Partitioner interface and override partition().
public int partition(String topic, Object key, byte[] keyBytes,
Object value, byte[] valueBytes, Cluster cluster) {
return isVip(key) ? 0 : 1 + (Math.abs(key.hashCode()) % (n - 1));
}Registering the Partitioner
Tell the producer to use your class via configuration.
spring:
kafka:
producer:
properties:
partitioner.class: com.example.VipPartitionerPartition Count and Keys
Changing the partition count changes hash % numPartitions, so existing keys may move to new partitions, breaking ordering. Choose partition count carefully up front.
Putting It Together
Keys are the link between data and partitions. They give you per-key ordering, drive load distribution, and can be customized with a partitioner for advanced routing.
Quick Check
Test your understanding of keys and partitioning.
Recap
You learned message keys and partitioning.
- The key determines the target partition via hashing.
- Same key, same partition, ordered processing.
- Low-cardinality keys cause hot partitions.
- A custom Partitioner enables advanced routing.
AI チューターと学ぶ Apache Kafka & Stream Processing Fundamentals — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「メッセージキーとパーティショニング戦略」レッスンは無料ですか?
はい。「メッセージキーとパーティショニング戦略」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Apache Kafka & Stream Processing Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Apache Kafka & Stream Processing Fundamentalsコースには全4レッスンが含まれています。
「メッセージキーとパーティショニング戦略」で何を学びますか?
Kafkaがメッセージキーでレコードをパーティションへ振り分ける方法、順序性が保証される仕組み、キーとカスタムパーティショナーの設計方法を学びます。 ブラウザで直接実行するハンズオンコードでApache Kafka & Stream Processing Fundamentalsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Apache Kafka & Stream Processing Fundamentalsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのApache Kafka & Stream Processing Fundamentalsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「メッセージキーとパーティショニング戦略」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このApache Kafka & Stream Processing Fundamentalsレッスンでコードを書いて実行できますか?
はい。すべてのApache Kafka & Stream Processing Fundamentalsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Kafkaへのメッセージ送信
- Kafkaからのメッセージ取得
- パーティションとオフセットの理解
- メッセージキーとパーティショニング戦略