I have a queue I need to specifically process 20 messages per a second. Is it possible to configure this with sqs?
In order to reach exactly 20 messages per second, you will have to spend a lot of effort coordinating your consumer to do such a thing. There is no way to limit consumption on SQS. If you are using lambda, you can set your concurrency limit to closely match your goal of 20m/s. Ex. If each call batches 10 messages, and takes on average 30 seconds to complete the request, you would want concurrency to be around 7 (6.67 rounded up to 7). Calculated by c = ceil(20 / (30/10)) where c is the concurrency. I also assumed ceiling the number because falling behind is probably worse than over-consuming.
Completely out of curiosity, why do you need to limit consumption on the message level vs the consumer level?
You’re asking the correct question, I think he is maybe a bit confused and should be looking into simply throttling the consumption of messages.
You have to do this on the consumer side. You can coordinate a rate using the Genetic cell rate algorithm which may have an implementation in your language of choice (I’ve used this one with Ruby before). This algorithm allows you to determine an instantaneous rate without keeping a full list of events that have happened over a given period. If you’re using a featureful consumer library, look for a place where you can add middleware that checks this before pulling a new message off the queue.
Your answer is completely valid. It’s basically down to where do you want to implement the rate limiter, it’s either on the infrastructure level (using already existing feature inside the cloud) or on the compute level. If it’s on the compute layer, most probably your code stands after a load balancer, meaning that the state must be synced between your compute instances which might be more pricy
Seeing as this comes up as one of the first hits on google, I can share an approach I have used in the past.
I am surprised that no one has mentioned using the Leaky Bucket Algorithm for this. It works for queueing as well.
The leaky bucket algorithm is based on the idea of a bucket that leaks water at a constant rate. Each request to the API is represented by a drop of water that enters the bucket. The bucket has a maximum capacity, which limits the burstiness of the requests. When the bucket is full, any incoming request is rejected or queued until there is space in the bucket. The requests are processed and sent to the API at a fixed rate, equal to the leakage rate of the bucket. The leaky bucket algorithm ensures that the requests are evenly distributed over time, regardless of how fast they arrive.
Let’s say you have two queues. We’ll name this “data” and the other “clock”.
A writer writes tokens into the clock queue at the desired consumption rate (say, 10 per second) and these messages have a TTL of say, one second. It also checks how deep the queue is before writing additional messages.
Worker jobs only poll the data queue for a new job after they can read a message from the clock queue successfully.
There are some sync issues here (like, what happens if TTL isn’t properly supported by the queue or if too many clock tokens back up, but you can probably solve that with additional readers or different queue management techniques.
My SES has limit of processing 100 messages/sec, with your suggestion I might be able to control it.