How does SQS know to wait for message 1 when message 2 arrives first for a group id?

i’ve spent way too much time with chatgpt trying to clarify this but dont get it. I might be being a bit slow here.

  • i produce an purchase message with customer 444 as the group id, then
  • i produce an invoice message with customer 444 as the group id

and the invoice comes before the purchase, how does sqs know to wait for the purchase message? chatgpt just keeps saying “oh sqs just knows the purchase is supposed to come first”

Because it was first in. Really no more complex than that

SQS isn’t supposed to guarantee ordering, that’s up to the app side. It guarantees queueing.

It doesn’t. The message group Id puts the messages into different lanes, and then it processes the messages within each lane in the order they arrived, so if you send the invoice message first, the invoice will be processed first.

They are using message group Id which is only a thing for fifo queues, which do guarantee ordering.

ok, this is how i understood it. maybe i’m misunderstanding how chatgpt is trying to explain it or something. thanks

so if a system publishes 2 events to event bridge in order

then they get mis ordered to EB to SQS because EB is pubsub then they will just be out of order at SQS

If my coworker asked that, my response would be something like: instead of asking the chat you could spend that time reading the docs. Also why wait? You can process purchase and invoice separately, as it pours from the sqs in whatever order

You can find a complete explanation in the documentation for SQS FIFO queues here: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-fifo-queues.html

There might be demands within the receiving ERP that the purchase order needs to be created before the invoice is created, but that should be solved in another way than fifo-queues.

Yes, the fifo SQS only cares about when the message is added to the queue, not what happened in some other system before.

Even better, have the chat generate some test code and actually test the documented behavior for you. This isn’t some complicated edge case and the LLMs + their corresponding wrappers just aren’t that good at even a moderate understanding of how the major CSPs work as it stands today.

I would rephrase “if you send the invoice message first, the invoice will be processed first” to “if the invoice message is received by SQS first then the invoice will be processed first.”

i read the documentation and then ran through some examples w/ chat and it gave me some confusing answers. this is why i’m here. as was mentioned in another comment there are various reasons in different types of events where order matters. also why we’re looking at FIFO

I cannot tell you how exactly it’s works internally in SQS but you can read how order guarantees are done in Kafka or ActiveMQ.

Usually a metadata service/logic is involved which tracks the order of messages and its consumption. And with distributed system same data is replicated using different algorithms.

that’s not true anymore, you can use message group ids with standard to get “fair queues”: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagegroupid-property.html

are you using FIFO or standard? a lot of answers in this thread neglect the new feature Fair Queues:

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-fair-queues.html

Yeah fifo is cool, but still risky. What if a message doesn’t reach the queue, or in any way the order is wrong? What about retries, dlq? Are orders and invoices on the same queue? Smells like state. Maybe split them into two. Emit order, process order, emit invoice, process invoice. Dunno, a lot of possibilities how to solve this on aws for different infras. Lambdas, dynamo, events, what are you doing step functions, sns… The aws architecture blog might help a bit.

ChatGPT often gets nuances like this wrong. It’s not well understood / explained.

all of these are good things to think about, i was specifically trying to make sure i understand the ordering rules.