mmuse
mmuse

Reputation: 125

AWS SQS without Await

I have a .Net Lambda function which sends messages to an SQS queue.

I’d like to be able to “fire and forget” those messages ... i.e. I don't want to "await" a response. However, AWS’ SQS client only provides Async methods (e.g. SendMessageAsync). And if I don’t “await” this call, then the messages never arrive in the queue.

I’ve not been able to find much on this topic. I see plenty of SQS examples online which use “SendMessage” but my assumption is that code was written using version 2.5 (or earlier) of the AWS SDK (several actually await SendMessage). The newer versions don’t have the non-Async versions of the methods.

I did find a similar StackOverflow question (QqsClient.SendMessageAsync without await doesn't work) but its asking how to await the sending of multiple messages whereas I’m looking to avoid awaiting at all.

This code will send a message:

using Amazon.SQS;

public class SQSHelper
{
    private AmazonSQSClient client = new AmazonSQSClient(Amazon.RegionEndpoint.USEast2);

    public async Task<string> SendMessage(string queue, string message)
    {
        var response = await client.SendMessageAsync(queue, message);
    }
}

But this code will not

using Amazon.SQS;

public class SQSHelper
{
    private AmazonSQSClient client = new AmazonSQSClient(Amazon.RegionEndpoint.USEast2);

    public void SendMessage(string queue, string message)
    {
        client.SendMessageAsync(queue, message);
    }
}

Is it possible to send SQS messages without waiting for the response?

Upvotes: 2

Views: 2078

Answers (1)

Josh
Josh

Reputation: 2338

You are not awaiting just the response, you are also awaiting the sending of the message. Since its also a lambda function, once the lambda handler returns your process ends which closes the HTTP connection to SQS before if finishes sending the message. This would work locally since your local process would still be executing but on a lambda function it would be hit or miss with most of the time the message being lost.

If the response time is an issue, you can zip your payload which would reduce the amount of time it takes to transmit to SQS and get a response back.

Upvotes: 2

Related Questions