Perfomance tuning initial load from large context

Hi Maarten,

when using commandGateway.send(), you’re basically sending commands without any backpressure. At the moment, AxonServer will accept all commands and try to send them downstream. It is likely that the downstream process cannot handle them at the speed they are delivered, causing the queue to fill up in AxonServer.

One thing you could do is use a Semaphore to limit the number of commands in transit. It should be faster than just sendAndWait, because multiple commands can be executed in parallel on the receiving side, but it should prevent all messages to be queued.

The approach would be:
// initialize the semaphore using a “reasonable” value. A few thousand shouldn’t be a problem.
Semaphore semaphore = new Semaphore(5000);

// in the event processing loop:
semaphore.acquire();
CompletableFuture<?> result = commandGateway.send(command);
result.whenComplete((result, exception) -> semaphore.release());

In upcoming releases, we are planning to improve the backpressure mechanisms for clients sending commands (and queries, for that matter), so that sending slows down when consumers are saturated.

Hope this helps.
Cheers,