Search code examples
disruptor-pattern

How a consumer posts back to the same Disruptor ring buffer


In a multiple producer setup, there is one producer thread and one consumer thread. Can the consumer post new events back to the same ring buffer? I assume it breaks when the buffer is full and the consumer thread will never get a free slot while it is working on the current event. In other words, dead lock happens.

What is the best way to do this? Do I have to introduce a sort of proxy thread who receives events from the consumer and post them to the ring buffer like normal producers?

supplement - why is it useful? Say the consumer thread is processing stock market data event and it needs to send an order to a market simulator(a class), and the market simulator should send an order execution event to the same ring buffer, ideally.


Solution

  • Providing a separate answer in light of additional detail in question (I still believe my original answer to be valid).

    In my head/experience you're trying to do to much in one go as your conflating the consumer of the ring buffer and the publisher which are usually separate concerns. I would generally expect the response events from your simulator to be delivered to the ring buffer in the same way as the original events its processing.

    Having said that what you're asking for is actually possible.

    You do however have to do a bit more of the work your self and write a custom EventProcessor which would allow you to mark the incoming message as processed (thus freeing the slot) before trying to publish an event back.