Large Messages
The large message utility handles SQS and SNS messages which have had their payloads offloaded to S3 if they are larger than the maximum allowed size (256 KB).
Notice
The large message utility (available in the powertools-sqs
module for versions v1.16.1 and earlier) is now deprecated
and replaced by the powertools-large-messages
described in this page.
You can still get the documentation here
and the migration guide here.
Features¶
- Automatically retrieve the content of S3 objects when SQS or SNS messages have been offloaded to S3.
- Automatically delete the S3 Objects after processing succeeds.
- Compatible with the batch module (with SQS).
Background¶
stateDiagram-v2
direction LR
Function : Lambda Function
state Application {
direction TB
sendMsg: sendMessage(QueueUrl, MessageBody)
extendLib: extended-client-lib
[*] --> sendMsg
sendMsg --> extendLib
state extendLib {
state if_big <<choice>>
bigMsg: MessageBody > 256KB ?
putObject: putObject(S3Bucket, S3Key, Body)
updateMsg: Update MessageBody<br>with a pointer to S3<br>and add a message attribute
bigMsg --> if_big
if_big --> [*]: size(body) <= 256kb
if_big --> putObject: size(body) > 256kb
putObject --> updateMsg
updateMsg --> [*]
}
}
state Function {
direction TB
iterateMsgs: Iterate over messages
ptLargeMsg: powertools-large-messages
[*] --> Handler
Handler --> iterateMsgs
iterateMsgs --> ptLargeMsg
state ptLargeMsg {
state if_pointer <<choice>>
pointer: Message attribute <br>for large message ?
normalMsg: Small message,<br>body left unchanged
getObject: getObject(S3Pointer)
deleteObject: deleteObject(S3Pointer)
updateBody: Update message body<br>with content from S3 object<br>and remove message attribute
updateMD5: Update MD5 of the body<br>and attributes (SQS only)
yourcode: <b>YOUR CODE HERE!</b>
pointer --> if_pointer
if_pointer --> normalMsg : False
normalMsg --> [*]
if_pointer --> getObject : True
getObject --> updateBody
updateBody --> updateMD5
updateMD5 --> yourcode
yourcode --> deleteObject
deleteObject --> [*]
}
}
[*] --> Application
Application --> Function : Lambda Invocation
Function --> [*]
SQS and SNS message payload is limited to 256KB. If you wish to send messages with a larger payload, you can leverage the amazon-sqs-java-extended-client-lib or amazon-sns-java-extended-client-lib which offload the message to Amazon S3. See documentation (SQS / SNS)
When offloaded to S3, the message contains a specific message attribute and the payload only contains a pointer to the S3 object (bucket and object key).
This utility automatically retrieves messages which have been offloaded to S3 using the extended client libraries. Once a message's payload has been processed successfully, the utility deletes the payload from S3.
This utility is compatible with versions 1.1.0+ of amazon-sqs-java-extended-client-lib and 1.0.0+ of amazon-sns-java-extended-client-lib.
Install¶
Depending on your version of Java (either Java 1.8 or 11+), the configuration slightly changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Permissions¶
As the utility interacts with Amazon S3, the lambda function must have the following permissions on the S3 bucket used for the large messages offloading:
s3:GetObject
s3:DeleteObject
Annotation¶
The annotation @LargeMessage
can be used on any method where the first parameter is one of:
SQSEvent.SQSMessage
SNSEvent.SNSRecord
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
When the Lambda function is invoked with a SQS or SNS event, the utility first checks if the content was offloaded to S3. In the case of a large message, there is a message attribute specifying the size of the offloaded message and the message contains a pointer to the S3 object.
If this is the case, the utility will retrieve the object from S3 using the getObject(bucket, key)
API,
and place the content of the object in the message payload. You can then directly use the content of the message.
If there was an error during the S3 download, the function will fail with a LargeMessageProcessingException
.
After your code is invoked and returns without error, the object is deleted from S3
using the deleteObject(bucket, key)
API. You can disable the deletion of S3 objects with the following configuration:
1 2 3 4 |
|
Use together with batch module
This utility works perfectly together with the batch module (powertools-batch
), especially for SQS:
Combining batch and large message modules | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Use together with idempotency module
This utility also works together with the idempotency module (powertools-idempotency
).
You can add both the @LargeMessage
and @Idempotent
annotations, in any order, to the same method.
The @Idempotent
takes precedence over the @LargeMessage
annotation.
It means Idempotency module will use the initial raw message (containing the S3 pointer) and not the large message.
Combining idempotency and large message modules | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Customizing S3 client configuration¶
To interact with S3, the utility creates a default S3 Client :
1 2 3 4 |
|
If you need to customize this S3Client
, you can leverage the LargeMessageConfig
singleton:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Migration from the SQS Large Message utility¶
- Replace the dependency in maven / gradle:
powertools-sqs
==>powertools-large-messages
- Replace the annotation:
@SqsLargeMessage
==>@LargeMessage
(the new module handles both SQS and SNS) - Move the annotation away from the Lambda
handleRequest
method and put it on a method withSQSEvent.SQSMessage
orSNSEvent.SNSRecord
as first parameter. - The annotation now handles a single message, contrary to the previous version that was handling the complete batch. It gives more control, especially when dealing with partial failures with SQS (see the batch module).
- The new module only provides an annotation, an equivalent to the
SqsUtils
class is not available anymore in this new version.
Finally, if you are still using the powertools-sqs
library for batch processing, consider moving to powertools-batch
at the same time to remove the dependency on this library completely; it has been deprecated and will be removed in v2.