Apache Kafka & Stream Processing Fundamentals · Lezione

Chiavi dei messaggi e strategie di partizionamento

Scopra come Kafka usi le chiavi dei messaggi per instradare i record nelle partizioni, come ciò garantisca l'ordine e come progettare chiavi e partitioner personalizzati.

Lezione 4 di 413 passaggi

Chiavi dei messaggi e strategie di partizionamento è una lezione Apache Kafka & Stream Processing Fundamentals gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Apache Kafka & Stream Processing Fundamentals, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Apache Kafka & Stream Processing Fundamentals include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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) % numPartitions

No 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 ordered

Choosing 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.VipPartitioner

Partition 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.
Gratis per iniziare

Impara Apache Kafka & Stream Processing Fundamentals con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Chiavi dei messaggi e strategie di partizionamento» è gratuita?

Sì — il testo completo di «Chiavi dei messaggi e strategie di partizionamento» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Apache Kafka & Stream Processing Fundamentals, passa a CoddyKit PRO. Il corso Apache Kafka & Stream Processing Fundamentals include 4 lezioni in totale.

Cosa imparerò in «Chiavi dei messaggi e strategie di partizionamento»?

Scopra come Kafka usi le chiavi dei messaggi per instradare i record nelle partizioni, come ciò garantisca l'ordine e come progettare chiavi e partitioner personalizzati. Eserciti Apache Kafka & Stream Processing Fundamentals con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Apache Kafka & Stream Processing Fundamentals?

Non è richiesta alcuna esperienza precedente. Apache Kafka & Stream Processing Fundamentals su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Chiavi dei messaggi e strategie di partizionamento»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Apache Kafka & Stream Processing Fundamentals?

Sì. Ogni lezione Apache Kafka & Stream Processing Fundamentals include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Produzione di messaggi in Kafka
  2. Consumo di messaggi da Kafka
  3. Comprendere partizioni e offset
  4. Chiavi dei messaggi e strategie di partizionamento
← Torna a Apache Kafka & Stream Processing Fundamentals