Skip to content

Significant Performance Degradation When Using EnablePublishParallelSend #1752

@KrawMire

Description

@KrawMire

Hello! First of all, thank you for this package :)

I am using CAP with Kafka as transport and MongoDB for storage. I have noticed the significant performance degradation when EnablePublishParallelSend is enabled. Unfortunately, I can't provide detailed metrics about it, but I suspect, that if there are really lots of messages, then this approach is not optimal (IDispatcher.Default file):

if (_enableParallelSend)
{
    var tasks = new List<Task>();
    var batchSize = _pChannelSize / 50;
    for (var i = 0; i < batchSize && _publishedChannel.Reader.TryRead(out var message); I++)
    {
        var item = message;
        tasks.Add(Task.Run(async () =>
        {
            try
            {
                var result = await _sender.SendAsync(item).ConfigureAwait(false);
                if (!result.Succeeded) _logger.MessagePublishException(item.Origin.GetId(), result.ToString(), result.Exception);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"An exception occurred when sending a message to the transport. Id:{message.DbId}");
            }
        }));
    }

    await Task.WhenAll(tasks);
}

For example, if we have at least 200 RPS and each request produces a message to the broker, then we will create a new Task for each of this operation. I suppose it can really degrade performance.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions