- Newest
- Most votes
- Most comments
When handling rate limit or quota exceeded errors with the SNS PublishBatch API in AWS SDK for Java v2, you're correct that these come through as generic SnsException instances rather than specific exception types.
The recommended approach for handling these errors without relying on error message parsing is to examine the error details within the SnsException. Specifically, you can check the error code and HTTP status code from the AWS error details:
try { PublishBatchResponse response = snsClient.publishBatch(request); // Process successful messages response.successful().forEach(success -> { // Handle successful messages }); // Check for failed messages within the batch response.failed().forEach(failure -> { // Handle individual message failures }); } catch (SnsException e) { // Check for throttling/rate limit errors if (e.awsErrorDetails().sdkHttpResponse().statusCode() == 429 || "Throttling".equals(e.awsErrorDetails().errorCode())) { // Handle throttling with exponential backoff } else if (e.awsErrorDetails().errorCode().contains("Limit") || e.awsErrorDetails().errorMessage().contains("Rate exceeded")) { // Handle quota/limit exceeded errors } else { // Handle other types of errors } }
Amazon SNS considers all 5XX errors and 429 (too many requests) errors as retryable. For these errors, implementing exponential backoff with jitter is appropriate. When you encounter throttling errors, the SDK internally categorizes them as SdkErrorType.THROTTLING.
For batch operations, it's also important to check the response object for partial failures. The PublishBatch operation can result in a combination of successful and unsuccessful actions, so you should check for batch errors even when the call returns an HTTP status code of 200.
Remember that Amazon SNS makes a total of 50 attempts to deliver a message before discarding it. If you need to retain messages that can't be delivered after all retries, consider configuring a dead-letter queue (DLQ) for your subscription.
Sources
SnsClient (AWS SDK for Java - 2.21.28)
SdkErrorType (AWS SDK for Java - 2.21.29)
Amazon SNS message batching - Amazon Simple Notification Service
Amazon SNS message delivery retries - Amazon Simple Notification Service
answered a year ago
Keyword Limit also exists in Throttling error codes, also Rate exceeded is checking the error message which I want to avoid as the error message is not future-proof.
else if (e.awsErrorDetails().errorCode().contains("Limit") || e.awsErrorDetails().errorMessage().contains("Rate exceeded"))
THROTTLING_ERROR_CODES.add("Throttling");
THROTTLING_ERROR_CODES.add("ThrottlingException");
THROTTLING_ERROR_CODES.add("ThrottledException");
THROTTLING_ERROR_CODES.add("ProvisionedThroughputExceededException");
THROTTLING_ERROR_CODES.add("SlowDown");
THROTTLING_ERROR_CODES.add("TooManyRequestsException");
THROTTLING_ERROR_CODES.add("RequestLimitExceeded");
THROTTLING_ERROR_CODES.add("BandwidthLimitExceeded");
THROTTLING_ERROR_CODES.add("RequestThrottled");
THROTTLING_ERROR_CODES.add("RequestThrottledException");
THROTTLING_ERROR_CODES.add("EC2ThrottledException");
THROTTLING_ERROR_CODES.add("PriorRequestNotComplete");
answered a year ago
Relevant content
asked 2 years ago
