Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding certain messages in SQS

I know SQS ain't build for that, but I'm curious is it possible to find messages in a queue that meet some criteria?

I can pull messages in a loop, search the message bodies for some pattern (without even deserializing them), and filter the messages I needed. But then it is possible to end up with an infinite loop - the first messages I read will be back to the queue by the time when I reach the end of the queue...

Extending visibility of the messages possible, but how do I know exactly how long it will take to scan the entire queue, and for how long should I extend the visibility? What if I have literally ten thousand messages in there?

Is there any workaround here? I need to scan the queue for some messages, and delete those...

like image 998
iLemming Avatar asked Sep 27 '12 22:09

iLemming


People also ask

Where can I find SQS messages?

Amazon SQS begins to poll servers to find messages in the queue. The progress bar on the right side of the Receive messages section displays the polling duration. The Messages section displays a list of the received messages. For each message, the list displays the message ID, sent date, size, and receive count.

Can we filter messages in SQS?

Now that all SNS and SQS resources have been created, you are ready to set filter policies to your SNS subscriptions. A filter policy is simple JSON document, set as an attribute of the SNS subscription, which defines the type of notification the subscriber is interested in.

Can you query an SQS queue?

It is not possible to 'search' messages in an Amazon SQS queue. A ReceiveMessage() call will retrieve one or more messages, but there is no ability to control which messages will be returned.


2 Answers

Short answer: no.

Queues are designed for things like tasks. A machine grabs a new task (i.e., message) from the queue, executes the task, then deletes the task.

If you're trying to search the messages to filter them, I can't help but wonder if you're using the wrong tool for the job…

like image 66
Ryan Parman Avatar answered Sep 28 '22 05:09

Ryan Parman


I do not think the short or long answers are "No".

Here are two counterpoint solutions that are "Yes".

  1. Traversing the Queue, maintaining a visited list
  2. Using Enterprise Integration Patterns (message routing) to split your messages into downstreams based on criteria

1. Traversing the Queue, maintaining a visited list

Consider the case of a queue with N messages, with no messages being added or deleted. Without additional information (e.g. if you knew how many messages should match your criteria), you need to traverse all N messages.

The key point here is knowing when you've traversed all N messages. There are some issues here.

  1. To know exactly, you'd have to track messages as they are added to the queue
  2. To know approximately, you can get the ApproximateNumberOfMessages attribute of the queue
  3. Or you could receive messages in a loop, maintaining a visited list, and assume that you will eventually sample and exhaust messages from each server your queue is sharded over

To maintain the visited list, as you receive messages and evaluate your match criteria, you could store the message_id of all visited messages.

Message ID's are nearly unique. See this thread

https://forums.aws.amazon.com/message.jspa?messageID=76119

If you went with (3), you wouldn't be certain how many iterations would be required to exhaust the queue. However, if you performed this indefinitely, you'd be guaranteed to exhuast the queue so long as the weighted random distribution over the SQS shard servers gives them all non-zero probability.

2. Using Enterprise Integration Patterns (message routing) to split your messages into downstreams based on criteria

If you have control over your messaging architecture you could use a Message Router as a "front-end" message processor that dispatches messages to various recipients based on criteria.

And specifically you'd use a Content-Based Router:

http://www.enterpriseintegrationpatterns.com/patterns/messaging/ContentBasedRouter.html

like image 27
mcg256 Avatar answered Sep 28 '22 05:09

mcg256