1
votes

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?

1
I'm not super familiar with the C# side of things but is this method not available to you? It may take some number of milliseconds but it's synchronous in nature.stdunbar
Is it possible to send SQS messages without waiting for the response? Whenever you execute a web service call, you should know the result (success, exception) and handle that result appropriately. Why would you want to just ignore the result?vasek
stdunbar, I was surprised to see that SendMessages is supported in the later versions of the SDK. I was mistaken. I found this page (docs.aws.amazon.com/sdk-for-net/v3/developer-guide/…) which states that the SDK only supports async versions for .Net Core. If I switch to the full .Net framework, I can get the non-async versions. If I use the async version, can I just fire and forget?mmuse
vasek, I agree we should normally wait for the response. But I'm implementing a fan out pattern and the process I'm modeling can accept a few lost results. My experience with SQS to date is that it's pretty robust and doesn't drop very often. And waiting for the response is slowing down the fan out and hence the results. My users would rather fast results that may not be 100% complete than 100% complete results and the delay.mmuse

1 Answers

1
votes

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.