Search code examples
springspring-bootdisruptor-pattern

Using Disruptor with Spring Boot


I have the following configuration for Reactor in my Spring Boot app:

@Configuration
public class AsyncConfig {

    @Bean
    public EventBus eventBus(){
        return EventBus.create(getDispatcher("rb-relay"));
    }

    public MultiThreadDispatcher getDispatcher(String name){
        return new WorkQueueDispatcher(name, 4, 1024, null);
    }
}

Basically what I'm doing is this: whenever an request comes to my endpoint, I relay it to other endpoints registered in my app. For that, I dispatch every incoming request to the bus:

@Service
public class URLRelayPublisher {

    @Autowired
    EventBus bus;

    @Autowired
    DestinationURLRepository repository;

    public void relay(HttpServletRequest request){
        repository.findAll()
                .forEach(destinationURL -> bus.notify("relay", Event.wrap(new RelayEvent(destinationURL, request))));
    }
}

and my consumer

@Component
@Log
public class URLRelayReceiver implements Consumer<Event<RelayEvent>> {

    @Autowired
    RestTemplate template;

    @Override
    public void accept(Event<RelayEvent> relayEvent) {
        try{
            HttpServletRequest request = relayEvent.getData().getRequest();
            DestinationURL destinationURL = relayEvent.getData().getDestinationURL();
            log.info("Relaying for "+destinationURL.getUrl());

            Map<String, String> headers = buildHeader(request);
            template.postForLocation(destinationURL.getUrl(), headers, request.getParameterMap());
        }catch (RestClientException e){
            log.info("Could not relay event: "+e.getMessage());
        }
    }

Disruptor claims to process around 6-8 Mi TPS, although when I siege the app (siege -d 2 -c 50 -t30S -H 'Content-Type: application/json' -H 'Auth-Token: qyz3kHZFuXRw8nm14uYLjLNoQ' 'http://127.0.0.1:8080/callback POST {"content": "message"}') I get something around 96 TPS. My questions are: 1) Is the configuration above enough to use Disruptor? 2) How do I make sure I'm using Disruptor right in here?


Solution

  • The disruptor can "process" 6-8 Mi TPS but all that means is it can exchange 6-8 Mi events between threads. It is not making any claims about the throughput of a spring-boot application.

    So in you're example it is passing RelayEvent between the various Consumers. Given all the other work going on in the context of a spring-boot application that will dominate the performance of the application rather than the disruptor.