question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Avoiding a rebalance if you need to wait longer than the heartbeat

See original GitHub issue

Hi Folks, I have a problem that doesn’t happen with spring-kafka and I would like to know if there is a right way to do with spring-cloud-stream-kafka.

The scenario: I have business logic to wait for some specific time duration to process the next message. The problem: Using the approach bellow, if the time is greater than 10sec, the kafka start rebalance given the heartbeat process.

    @Override
    public void run(ApplicationArguments args) {
        new Thread(() -> doWork(errors, events)).start();
    }

    private void doWork(PollableMessageSource source, MessageChannel output) {
        while(true) {
            try {
                source.poll(message -> errorProcessor.process(message, output),
                        new ParameterizedTypeReference<Event>() {});
                
                Thread.sleep(sleepTime);  ///// If this time is greater than 10 sec, the
            } catch (InterruptedException e) {
                log.error("Fail to sleep thread: {}", e.getMessage());
            }
        }
    }

My founds: Looks like given I’m waiting with Thread.sleep() the main thread is locked and this is why the heartbeat time out and then Kafka restart to rebalance. Make senses?

With this, I have tested the spring-kafka directly and with it works fine the code below:

    @KafkaListener(topics = "topic", groupId = "group1")
    public void consume(String message) {
        logger.info(String.format("#### -> Consumed message-> %s", message));
        try {
            Thread.sleep(60*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        logger.info("#### -> end waiting for...");
    }

I got this idea from this Github issue from spring-kafka and on the same page I could see the there was an implementation to do the heartbeat to Kafka in the background, so this is why I imagine works for that approach.

Do you know if there is somehow to do this same idea using spring-cloud-stream? or if something that is needed to implement it?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
sobychackocommented, Apr 3, 2020

Closing this due to no recent activity.

1reaction
garyrussellcommented, Jun 3, 2019

Even stranger; I can’t reproduce it at all now, with or without logging 😖

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing Kafka consumers - Strimzi
So rebalancing can have a clear impact on the performance of your cluster group. As mentioned, increasing the number of heartbeat checks reduces ......
Read more >
Use Capacity Rebalancing to handle Amazon EC2 Spot ...
When launching a new instance, Amazon EC2 Auto Scaling waits until the new instance passes its health check before it proceeds with terminating...
Read more >
Kafka Consumer Important Settings: Poll & Internal Threads ...
If more than session.timeout.ms passes without the consumer sending a heartbeat to the group coordinator, it is considered dead and the group coordinator ......
Read more >
Chapter 4. Kafka Consumers: Reading Data from Kafka
It will also trigger a rebalance immediately rather than wait for the group coordinator to discover that the consumer stopped sending heartbeats and...
Read more >
What does "Rebalancing" mean in Apache Kafka context?
If a consumer stop sending heartbeat for long time and its session will time out (controlled by session.timeout.ms) then group coordinator will ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found