0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · Lezione

Listener del ribilanciamento e appartenenza statica

Scopra come i ribilanciamenti dei gruppi di consumer interrompano l'elaborazione e come i listener di ribilanciamento e l'appartenenza statica riducano al minimo downtime e perdita di stato.

Listener del ribilanciamento e appartenenza statica è una lezione Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 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 Advanced Spring Boot 4: Event-Driven Architecture (Kafka), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Advanced Spring Boot 4: Event-Driven Architecture (Kafka) include 4 lezioni in totale.

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

What Triggers a Rebalance

A rebalance reassigns partitions among consumers in a group. It happens when a consumer joins, leaves, crashes, or its session times out.

During a rebalance, processing pauses across the group.

The Cost of Rebalancing

Frequent rebalances hurt throughput: partitions are revoked and reassigned, in-flight work may be reprocessed, and local caches are invalidated.

Advanced consumers actively minimize and react to rebalances.

Rebalance Listener Hooks

A ConsumerRebalanceListener gives you two callbacks:

  • onPartitionsRevoked — called before partitions are taken away.
  • onPartitionsAssigned — called after new partitions are granted.

Committing on Revoke

Use onPartitionsRevoked to commit offsets for work completed so far, avoiding duplicate processing after reassignment.

public void onPartitionsRevoked(Collection<TopicPartition> parts) {
    consumer.commitSync(currentOffsets);
}

Wiring It in Spring

In Spring Kafka, implement ConsumerAwareRebalanceListener and set it on the container properties.

factory.getContainerProperties()
    .setConsumerRebalanceListener(myRebalanceListener);

Static Membership

Static membership assigns each consumer a stable group.instance.id. On a brief restart, the broker keeps its partitions instead of triggering a rebalance.

spring:
  kafka:
    consumer:
      properties:
        group.instance.id: consumer-pod-1

Session and Heartbeat Timeouts

Static membership relies on session.timeout.ms. If a consumer rejoins within the session window using the same instance id, no rebalance occurs.

session.timeout.ms: 45000
heartbeat.interval.ms: 3000

Cooperative Rebalancing

The CooperativeStickyAssignor reassigns only the partitions that must move, instead of revoking everything (stop-the-world). This dramatically reduces rebalance impact.

partition.assignment.strategy: org.apache.kafka.clients.consumer.CooperativeStickyAssignor

Avoiding Poll Timeouts

If processing a batch exceeds max.poll.interval.ms, the broker assumes the consumer is dead and rebalances. Keep per-poll work bounded or raise the interval.

Combining Strategies

For resilient consumers, combine static membership (avoid restart churn), cooperative rebalancing (smaller moves), and a rebalance listener (clean offset commits).

Putting It Together

Rebalances are unavoidable but controllable. Listeners let you commit cleanly, static membership avoids needless rebalances on restart, and cooperative assignment limits disruption.

Quick Check

Test your understanding of rebalancing.

Recap

You learned advanced rebalance handling.

  • Rebalances pause processing and can reprocess work.
  • Rebalance listeners let you commit offsets on revoke.
  • Static membership avoids rebalances on brief restarts.
  • Cooperative assignment reduces stop-the-world disruption.

Domande Frequenti

La lezione «Listener del ribilanciamento e appartenenza statica» è gratuita?

Sì — il testo completo di «Listener del ribilanciamento e appartenenza statica» è 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 Advanced Spring Boot 4: Event-Driven Architecture (Kafka), passa a CoddyKit PRO. Il corso Advanced Spring Boot 4: Event-Driven Architecture (Kafka) include 4 lezioni in totale.

Cosa imparerò in «Listener del ribilanciamento e appartenenza statica»?

Scopra come i ribilanciamenti dei gruppi di consumer interrompano l'elaborazione e come i listener di ribilanciamento e l'appartenenza statica riducano al minimo downtime e perdita di stato. Eserciti Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 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 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?

Non è richiesta alcuna esperienza precedente. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 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 «Listener del ribilanciamento e appartenenza statica»?

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 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?

Sì. Ogni lezione Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 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. Commit manuale degli offset
  2. Sospensione e ripresa dei consumer
  3. Concorrenza e gestione dei thread
  4. Listener del ribilanciamento e appartenenza statica
← Torna a Advanced Spring Boot 4: Event-Driven Architecture (Kafka)