SNS to Lambda or SNS to SQS to Lambda, what are the trade-offs?
Tags: aws, lambda, serverless
Published: 2023-07-12 23:08:40

---
title: SNS to Lambda or SNS to SQS to Lambda, what are the trade-offs?
published: true
date: 2023-07-11 21:10:24 UTC
tags: AWS,Lambda,Serverless
canonical_url: https://theburningmonk.com/2023/07/sns-to-lambda-or-sns-to-sqs-to-lambda-what-are-the-trade-offs/
---

I had a really good question from one of my students at the [Production-Ready Serverless workshop](https://productionreadyserverless.com/) the other day:

> “I’m reacting to S3 events received via an SNS topic and debating the merits of having the Lambda triggered directly by the SNS or having an SQS queue. I can see the advantage of the queue for enhanced visibility into the processing and “buffering” events for the Lambda but is there a reason not to use a queue in this situation?”

It used to be that you’d always use SNS with SQS in case there was a problem with delivery to the target, so you don’t lose data. But now that SNS supports Dead-Letter Queues (DLQs) natively, you don’t need to do it for resilience anymore. There are still reasons to use SQS here, namely for cost efficiency and performance.

The main consideration here is that SQS is a batched event source for Lambda so you can process the same number of messages with fewer invocations. If the processing logic is IO-heavy (e.g. waiting for API responses) then you can even process messages in parallel and make more efficient use of the CPU cycles. This often leads to better throughput and lower Lambda costs when processing a large number of messages.

However, this cost-saving needs to be weighed against the additional cost for SQS:

- for publishing messages to it (via SNS), and
- for the polling requests (that the Lambda service performs on your behalf via the event source mapping).

This trade-off depends on your workload and how long it takes to process each message.

The main drawback of using SQS is complexity. Because now you’d need to:  
1) Have a try-catch around each message so you can capture the failed messages and avoid failing the entire batch.  
2) Implement partial batch response when there are partial failures (see official documentation [here](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting)) by configuring the event source mapping and using the correct response format.  
3) Configure a DLQ and set up alerts so you know when there are failed messages that require manual intervention. For example, to investigate why the messages failed and if possible, replay them by using the new [StartMessageMoveTask](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_StartMessageMoveTask.html) API to send the messages back to the main queue.

It’s worth noting that point 3 is still necessary even if you don’t use SQS. But with SNS and Lambda, you can use Lambda Destination instead, which is a better solution than DLQs. Because the failure destination captures the error as well as the failed event, whereas the SQS DLQ would only capture the failed event. It then falls on you to figure out what caused the failure and if the failed message can be retried. You need to find the relevant log messages to figure out what the original error was. To do that, you need to have good observability in place so you can find the error logs related to a failed SQS message. This can be time-consuming if there is a large number of failed messages in the DLQ.

In general, I like to start with the simplest solution, and if it ticks all my boxes then I stop there. There should be a good reason to add a moving part to your architecture. In this particular case, if you’re processing lots of events and the Lambda cost starts to add up, then it’s a good reason to add SQS into the mix so you can process messages in batches.

Similarly, we can argue that you don’t even need SNS in the original question because S3 is capable of invoking Lambda functions. But if you need to fan out the S3 notifications to multiple targets then that’s a good reason to add SNS (or EventBridge) into the mix.

Oh, another thing that’s worth pointing out here is that for asynchronous invocation, the Lambda service already employs an internal SQS queue (see below).

![](https://theburningmonk.com/wp-content/uploads/2023/07/img_64adbe7c2c1a6.png)

(source: [https://www.youtube.com/watch?v=0\_jfH6qijVY](https://www.youtube.com/watch?v=0_jfH6qijVY))

### Counter Arguments

A common argument for using SQS is that you get better visibility into the state of the queue – e.g. how many messages are in the backlog.

While there is no direct metric that tells you the size of the internal queue for your function, you can estimate it with the available metrics. For example, you can use the delta between SNS’s _NumberOfMessagesPublished_ metric and Lambda’s _Invocations_ metric to estimate the no. of messages in the backlog. Alternatively, you can also use Lambda’s _AsyncEventsReceived_ metric instead of the SNS metric.

You can also use Lambda’s _AsyncEventAge_ metric to get a sense of how far behind you are. If a function is throttled due to its reserved concurrency setting and is not able to keep up with the flow of incoming messages, then the _AsyncEventAge_ metric would continue to climb. Similarly, if a function fails repeatedly and the async messages have to be retried, then this metric would also spike. You can create an alert against this metric to notify you that your system is falling behind and you need to revisit its reserved concurrency setting.

Another common argument is that SQS gives you better concurrency control with the _MaximumConcurrency_ setting on the event source mapping.

This is also a flawed argument, but there is a small caveat to it… Let me explain.

Lambda has built-in concurrency control with its reserved concurrency setting, which acts as the “maximum concurrency” for the function.

However, the pollers would attempt to invoke the function regardless. These attempts would be throttled and they will be reflected in the function’s _Throttles_ metric. Luckily, even as these attempts to invoke the function are throttled, they do not count as “failed invocations” and the messages are not directed to the DLQ or Lambda Destination until they have been processed by the function and failed three times.

![](https://theburningmonk.com/wp-content/uploads/2023/07/img_64add6b166aa1.png)

So what does this mean for you?

It means you have an effective concurrency control mechanism when using SNS with Lambda directly, using Lambda’s reserved concurrency setting. However, this can result in throttling happening in the background and you kinda have to turn a blind eye to it and ignore these throttling.

With SQS and Lambda, you can’t rely on Lambda’s reserved concurrency setting because it would lead to the SQS over-polling problem (see [this post](https://zaccharles.medium.com/lambda-concurrency-limits-and-sqs-triggers-dont-mix-well-sometimes-eb23d90122e0) by Zac Charles). Instead, you have to use the _MaximumConcurrency_ setting on the SQS event source mapping.

And it means the argument that SQS offers better concurrency control is incorrect, so long as you ignore the throttlings on the Lambda function.

The post [SNS to Lambda or SNS to SQS to Lambda, what are the trade-offs?](https://theburningmonk.com/2023/07/sns-to-lambda-or-sns-to-sqs-to-lambda-what-are-the-trade-offs/) appeared first on [theburningmonk.com](https://theburningmonk.com).