Need advice about which tool to choose?Ask the StackShare community!
AWS Lambda vs Amazon SQS: What are the differences?
Introduction
In this markdown, we will discuss the key differences between AWS Lambda and Amazon SQS. Both AWS Lambda and Amazon Simple Queue Service (SQS) are services provided by Amazon Web Services (AWS) that enable developers to build scalable and efficient applications. However, they have distinct functionalities and use cases.
Scalability: AWS Lambda is a serverless computing service that automatically scales to handle a high volume of requests. It allows developers to run code without provisioning or managing servers. On the other hand, Amazon SQS is a fully managed message queuing service that provides distributed message queues for storing messages as they travel between different components of a distributed application. It enables decoupling of the sender and receiver, allowing scalability within the application itself.
Invocation: AWS Lambda functions are triggered by events such as changes to data in an Amazon S3 bucket, updates to an Amazon DynamoDB table, or an HTTP request through Amazon API Gateway. These events invoke the Lambda function, which executes the code. On the contrary, Amazon SQS allows messages to be sent and received by any component of a distributed application. It decouples the sender from the receiver and allows the receiver to process messages at its own pace.
Compute Resource: AWS Lambda functions are executed on demand, meaning that the necessary compute resources are automatically provisioned by AWS to execute the code. The function's execution environment is managed by AWS. In contrast, Amazon SQS does not execute any code itself. It simply acts as a message queue, storing messages until they are processed by a separate component or worker.
Message Persistence: In AWS Lambda, messages are not persisted by default. The function is invoked with an event, processes it, and generates a response. There is no storage of messages for future retrieval. On the other hand, Amazon SQS guarantees message persistence. Messages sent to an SQS queue are stored redundantly across multiple availability zones to provide durability. The messages can be stored in the queue for a configurable retention period.
Concurrency: AWS Lambda functions can scale horizontally to handle multiple requests concurrently. Each instance of the Lambda function can process a single event at a time in a separate container. The number of concurrent executions can be configured to meet the application's requirements. On the other hand, Amazon SQS can handle multiple concurrent workers that retrieve and process messages from the queue. The number of workers can be adjusted to achieve the desired throughput.
Billing: AWS Lambda pricing is based on the number of requests and the duration of both the execution and the memory allocated. Users pay only for the compute resources actually consumed during code execution. Amazon SQS pricing is based on the number of requests made to the service, as well as the volume of data transferred and stored in the queues. Users pay for the number of messages sent, received, or deleted, and for storage consumed by messages.
In summary, AWS Lambda is a serverless compute service that automatically scales to handle requests, while Amazon SQS is a managed message queuing service that provides distributed message queues. Lambda executes code on demand, while SQS acts as a persistent storage and decoupling mechanism. Lambda scales horizontally with concurrent executions, while SQS handles multiple worker processes. The pricing models also differ between the two services.
Hi, we are in a ZMQ set up in a push/pull pattern, and we currently start to have more traffic and cases that the service is unavailable or stuck. We want to: * Not loose messages in services outages * Safely restart service without losing messages (ZeroMQ seems to need to close the socket in the receiver before restart manually)
Do you have experience with this setup with ZeroMQ? Would you suggest RabbitMQ or Amazon SQS (we are in AWS setup) instead? Something else?
Thank you for your time
ZeroMQ is fast but you need to build build reliability yourself. There are a number of patterns described in the zeromq guide. I have used RabbitMQ before which gives lot of functionality out of the box, you can probably use the worker queues
example from the tutorial, it can also persists messages in the queue.
I haven't used Amazon SQS before. Another tool you could use is Kafka.
Both would do the trick, but there are some nuances. We work with both.
From the sound of it, your main focus is "not losing messages". In that case, I would go with RabbitMQ with a high availability policy (ha-mode=all) and a main/retry/error queue pattern.
Push messages to an exchange, which sends them to the main queue. If an error occurs, push the errored out message to the retry exchange, which forwards it to the retry queue. Give the retry queue a x-message-ttl and set the main exchange as a dead-letter-exchange. If your message has been retried several times, push it to the error exchange, where the message can remain until someone has time to look at it.
This is a very useful and resilient pattern that allows you to never lose messages. With the high availability policy, you make sure that if one of your rabbitmq nodes dies, another can take over and messages are already mirrored to it.
This is not really possible with SQS, because SQS is a lot more focused on throughput and scaling. Combined with SNS it can do interesting things like deduplication of messages and such. That said, one thing core to its design is that messages have a maximum retention time. The idea is that a message that has stayed in an SQS queue for a while serves no more purpose after a while, so it gets removed - so as to not block up any listener resources for a long time. You can also set up a DLQ here, but these similarly do not hold onto messages forever. Since you seem to depend on messages surviving at all cost, I would suggest that the scaling/throughput benefit of SQS does not outweigh the difference in approach to messages there.
I want to schedule a message. Amazon SQS provides a delay of 15 minutes, but I want it in some hours.
Example: Let's say a Message1 is consumed by a consumer A but somehow it failed inside the consumer. I would want to put it in a queue and retry after 4hrs. Can I do this in Amazon MQ? I have seen in some Amazon MQ videos saying scheduling messages can be done. But, I'm not sure how.
Mithiridi, I believe you are talking about two different things. 1. If you need to process messages with delays of more 15m or at specific times, it's not a good idea to use queues, independently of tool SQM, Rabbit or Amazon MQ. you should considerer another approach using a scheduled job. 2. For dead queues and policy retries RabbitMQ, for example, doesn't support your use case. https://medium.com/@kiennguyen88/rabbitmq-delay-retry-schedule-with-dead-letter-exchange-31fb25a440fc I'm not sure if that is possible SNS/SQS support, they have a maximum delay for delivery (maxDelayTarget) in seconds but it's not clear the number. You can check this out: https://docs.aws.amazon.com/sns/latest/dg/sns-message-delivery-retries.html
When adding a new feature to Checkly rearchitecting some older piece, I tend to pick Heroku for rolling it out. But not always, because sometimes I pick AWS Lambda . The short story:
- Developer Experience trumps everything.
- AWS Lambda is cheap. Up to a limit though. This impact not only your wallet.
- If you need geographic spread, AWS is lonely at the top.
Recently, I was doing a brainstorm at a startup here in Berlin on the future of their infrastructure. They were ready to move on from their initial, almost 100% Ec2 + Chef based setup. Everything was on the table. But we crossed out a lot quite quickly:
- Pure, uncut, self hosted Kubernetes — way too much complexity
- Managed Kubernetes in various flavors — still too much complexity
- Zeit — Maybe, but no Docker support
- Elastic Beanstalk — Maybe, bit old but does the job
- Heroku
- Lambda
It became clear a mix of PaaS and FaaS was the way to go. What a surprise! That is exactly what I use for Checkly! But when do you pick which model?
I chopped that question up into the following categories:
- Developer Experience / DX 🤓
- Ops Experience / OX 🐂 (?)
- Cost 💵
- Lock in 🔐
Read the full post linked below for all details
Pros of Amazon SQS
- Easy to use, reliable62
- Low cost40
- Simple28
- Doesn't need to maintain it14
- It is Serverless8
- Has a max message size (currently 256K)4
- Triggers Lambda3
- Easy to configure with Terraform3
- Delayed delivery upto 15 mins only3
- Delayed delivery upto 12 hours3
- JMS compliant1
- Support for retry and dead letter queue1
- D1
Pros of AWS Lambda
- No infrastructure129
- Cheap83
- Quick70
- Stateless59
- No deploy, no server, great sleep47
- AWS Lambda went down taking many sites with it12
- Event Driven Governance6
- Extensive API6
- Auto scale and cost effective6
- Easy to deploy6
- VPC Support5
- Integrated with various AWS services3
Sign up to add or upvote prosMake informed product decisions
Cons of Amazon SQS
- Has a max message size (currently 256K)2
- Proprietary2
- Difficult to configure2
- Has a maximum 15 minutes of delayed messages only1
Cons of AWS Lambda
- Cant execute ruby or go7
- Compute time limited3
- Can't execute PHP w/o significant effort1