/
Competing Consumers Pattern
Competing Consumers Pattern
Problem
App running in the cloud expecting to handle a large number of requests :
not handle synchronously, rather handle them asynchronously.
At peak hours, using a single instance can cause the instance to become flooded with requests.
Solution
Using multiple instances to make the workload to be load balanced across consumers to prevent an instance to become the bottleneck.
Need a message queue to distribute work to instances of a service and to avoid timeout.
Diagram from Micosoft Documentation
Competing Consumers In Practice
Example of Code in C#
string connectionString = "<connection_string>";
string queueName = "<queue_name>";
// since ServiceBusClient implements IAsyncDisposable we create it with "await using"
await using var client = new ServiceBusClient(connectionString);
// create the sender
ServiceBusSender sender = client.CreateSender(queueName);
// create a message that we can send. UTF-8 encoding is used when providing a string.
ServiceBusMessage message = new ServiceBusMessage("Hello world!");
// send the message
await sender.SendMessageAsync(message);
// create a receiver that we can use to receive the message
ServiceBusReceiver receiver = client.CreateReceiver(queueName);
// the received message is a different type as it contains some service set properties
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();
// get the message body as a string
string body = receivedMessage.Body.ToString();
Console.WriteLine(body);
, multiple selections available,