Crates.io | sqs-service-helper |
lib.rs | sqs-service-helper |
version | 0.1.5 |
source | src |
created_at | 2017-07-23 20:54:46.461914 |
updated_at | 2017-07-24 17:42:25.45289 |
description | A library to facilitate SQS based consumer services |
homepage | |
repository | https://github.com/insanitybit/sqs-service-helper |
max_upload_size | |
id | 24741 |
size | 129,813 |
A library for making SQS consumer based services trivial to write.
Automatic visibility timeout handling
Automatic consumer throttling/ scaling
Automatic use of AWS Bulk APIs
Parallel message processing
When an SQS Message is received from its queue it remains invisible for a set amount of time - either defined by the SQS Queue or the message. The default is 30 seconds.
When that timeout expires the message reappears on the queue with a new receipt.
If a message takes longer to process than its visibility timeout then, without extra measures, the message will likely be processed many times.
In order to avoid this issue the framework will automatically start background services to update the timeout. The goal is to keep the timeout as low as possible so that if there is an error during process it will appear quickly onto the queue, but also to ensure that regardless of the time necessary to process the message it will remain invisible.
The current algorithm goes something like this:
Throughout this process the time it takes for a message to reach the extender from the consumer is tracked and sent to the Throttler. The throttler uses this time to determine the maximum number of inflight messages the service can handle while processing messages quickly enough to maintain their invisibility state.
It is notable that in the event of network failures where message visibility updates or deletes fail for the duration of a timeout that multiple message processing will occur. This is simply a limitation of the at-least-once model of processing that SQS has chosen.
As previously mentioned the service tracks how long it takes for a message's receipt to go from the consumer to the Visibility Extender.
This is the service's bottleneck.
Based on the median time this takes the service will scale consumers up or down, as well as implement a throttle - a set amount of time each consumer needs to wait between API calls (this takes the call's latency into account, so as to keep this wait period to a minimum).
A specialied StreamingMedian structure is used to efficiently calculate the median for the last 63 messages.
This throttling/ scaling should mean that you're always processing as many messages as the framework can handle, but not more.
SQS supports using bulk APIs to reduce IO and costs. This can allow for a 10x cost reduction for each bulk API utilized.
Consumers will, by default, long poll for 20 seconds and grab at most 10 messages.
This library will automatically buffer delete and visibility update requests, increasing the frequency of bulk API usage.
Once you have provided your MessageHandler to the framework it will spread the processor across threads (currently defined by the user).
The processors use a work stealing algorithm over an MPMC queue.