From the Amazon SQS FAQ page:
Amazon SQS does not guarantee FIFO access to messages in Amazon SQS queues, mainly because of the distributed nature of the Amazon SQS. If you require specific message ordering, you should design your application to handle it.
My question is - How does one design the application that way?
You can set a message sequence counter while sending message. At receiving end, you can keep processing messages if sequence is right. In case an out of sequence message comes, wait till the right message comes (till then store messages in a list sorted by seq no.) and then process right sequence message and others which came in between.
Not exactly FIFO, but you can do something like this:
Creation of queues:
- Create N queues (say q1, q2, ..., qN)
- Have an upper limit in each queue (M number of messages per queue)
Inserting item in the queues:
- All jobs writing to the SQS will start from q1
- If q1 has M messages then move on to q2 and so on (after qN move to q1)
Picking item from the queues:
- All job reading from SQS will start picking item from q1
- If q1 is empty then move on to q2 and so on (after qN move to q1)
You need to make sure that draining one queue (reading out M messages) should take sufficiently lesser time than filling all the queues (pushing NxM messages). Which will ensure that batch of M messages will take priority over rest of the messages inserted in the queues. (First M messages In First M messages Out)
From the Amazon SQS FAQ page:
Amazon SQS does not guarantee FIFO access to messages in Amazon SQS queues, mainly because of the distributed nature of the Amazon SQS. If you require specific message ordering, you should design your application to handle it.
My question is - How does one design the application that way?
You can set a message sequence counter while sending message. At receiving end, you can keep processing messages if sequence is right. In case an out of sequence message comes, wait till the right message comes (till then store messages in a list sorted by seq no.) and then process right sequence message and others which came in between.
Not exactly FIFO, but you can do something like this:
Creation of queues:
- Create N queues (say q1, q2, ..., qN)
- Have an upper limit in each queue (M number of messages per queue)
Inserting item in the queues:
- All jobs writing to the SQS will start from q1
- If q1 has M messages then move on to q2 and so on (after qN move to q1)
Picking item from the queues:
- All job reading from SQS will start picking item from q1
- If q1 is empty then move on to q2 and so on (after qN move to q1)
You need to make sure that draining one queue (reading out M messages) should take sufficiently lesser time than filling all the queues (pushing NxM messages). Which will ensure that batch of M messages will take priority over rest of the messages inserted in the queues. (First M messages In First M messages Out)
0 commentaires:
Enregistrer un commentaire