chilts/mongodb-queue

message order after visibility timeout

Closed this issue · 4 comments

Hi, thanks for the smart library.

After reading the code, I assume that the every time the message go back to the message queue(after visibility timeout), it remain the same _id as before, so the order of them is still according to the id and not changed? Right?

Hi @hanwencheng,

Yes, the id will not change.

However, the ordering of the messages is not by id but by it's visibility (the 'visible' field). When a message is retrieved this field is updated to be now plus it's visibility window.

However, it does mean that if a message goes back on the stack, it will be visible immediately, but this doesn't mean it'll be next in the list, but it'll be somewhere near the start.

Your code should never depend on delivery of any particular message in any particular order. Even though there are some rules to this, it's better if your code stays independent of it and just takes each message as it comes. :) I'm sure you're doing this already, but am just clarifying.

Thanks for your question.

Andy

Oh wait, I mis-read my code! Silly me.

Yes, it is ordered by _id. :)

Thanks, Andy

Thanks for the clarification, that is a important point for me because the message processing progress need to be order by their receiving time, then everything will be fine.

Hanwen

Yes, but you shouldn't actually rely on that. If you need to rely on the ordering then you might need to look for another solution.

For example, if you have three messages and you process multiple messages at a time, then you may well fail with message number 1, then process 2 and 3 before 1 timesout and gets put back on the queue.

Anyway, just so you know. :)