简体   繁体   中英

reactor kafka receive(): Performace gain with publishOn()?

Small question regarding reactor kafka consumer please.

in many tutorials found online, we can see two different constructs for a reactive kafka consumer.

example 1:

public Flux<String> myConsumer1() {
        return kafkaReceiver.receive()
                .map(oneMessage-> doLogicThisIsTestedNonBlockingAllTheWay(oneMessage))
                .doOnNext(fakeConsumerDTO -> System.out.println("successfully consumed {}={}" + fakeConsumerDTO))
                .doOnError(throwable -> System.out.println("something bad happened while consuming : {}" + throwable.getMessage()));
    }

example 2:

public Flux<String> myConsumer2() {
        Scheduler readerScheduler = Schedulers.newBoundedElastic(60, 60, "readerThreads");
        return kafkaReceiver.receive()
                .publishOn(readerScheduler) // or this one .publishOn(Schedulers.boundedElastic())
                .map(oneMessage-> doLogicThisIsTestedNonBlockingAllTheWay(oneMessage))
                .doOnNext(fakeConsumerDTO -> System.out.println("successfully consumed {}={}" + fakeConsumerDTO))
                .doOnError(throwable -> System.out.println("something bad happened while consuming : {}" + throwable.getMessage()));
    }

The main difference between example 1 and example 2 is that the actual processing / handling / perform logic on the message is either directly executed on the map method (example 1) or on the reactor Scheduler (example 2).

Suppose the processing / handling / perform logic on the message method has been proven non blocking (blockhound tested etc).

I am having a hard time understanding the difference between the two.

And most of all, I would like to ask if there is any performance gain from one over the other.

To emphasize, this is not an opinion based or style question.

This question is asking about a possible performance difference / performance gain between two solutions.

Thank you

I do not think there is much difference between those 2 examples, but the second example seems more flexible since you use Scheduler.

Using a Scheduler can also help with mitigating any issues with blocking code, as it allows you to specify a separate thread pool for handling the processing of the messages. This can help prevent the blocking of the main thread and improve overall performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM