25

http://docs.aws.amazon.com/general/latest/gr/api-retries.html

This document mentions that "each AWS SDK implements automatic retry logic and AWS SDK for Java automatically retries requests."

What is the default mechanism for Java AWS SDK, if i don't specify any retry config? I have been using the Java AWS SDK and get a straightforward service exception if something fails on AWS service side. I have never experienced any "automatic" retry mechanism. Can someone explain what this retry mechanism is?

laughedelic
  • 6,230
  • 1
  • 32
  • 41
WillMcavoy
  • 1,795
  • 4
  • 22
  • 34
  • 2
    There is no default mechanism of Java SDK. The AWS SDK code written by Amazon guys in Java have included retry of a client request by default. This means, if a client request fails to connect to server because of some error like, Server Busy, Throttled, etc., it will retry to establish the connection. The number of tries can be configured using `ClientConfiguration` class. – Abhineet Oct 19 '16 at 06:39
  • 1
    If you are using java sdk, automatic retry doesnt apply. The automatic reply applies if you use aws sdk. – notionquest Oct 19 '16 at 07:07

1 Answers1

33

The same documentation page says:

The AWS SDK for Java automatically retries requests, and you can configure the retry settings using the ClientConfiguration class.

You should check the official documentation for ClientConfiguration, it has plenty of methods to tune its behaviour regarding retry logic. Here are the most important:

  • withMaxErrorRetry Sets the maximum number of retry attempts for failed retryable requests (ex: 5xx error responses from services)
  • withRequestTimeout Sets the amount of time to wait (in milliseconds) for the request to complete before giving up and timing out [...]
  • withThrottledRetries Retry throttling is a feature which intelligently throttles retry attempts when a large percentage of requests are failing and retries are unsuccessful [...]
  • withRetryPolicy This is the most interesting, it allows you to choose RetryPolicy and change:
    • BackoffStrategy The hook for providing custom back-off strategy to control the sleep time between retries
    • RetryCondition The hook for providing custom condition on whether a failed request should be retried
    • maxErrorRetry
    • honorMaxErrorRetryInClientConfig (whether to respect the configuration setting mentioned above)

Also note that if you haven't noticed the automatic retry mechanism, it could be due to the client-side errors. These settings are only for retrying requests in case of server (5xx) or throttling errors:

client errors (4xx) indicate that you need to revise the request to correct the problem before trying again

If you claim that it were "service side" fails, you should provide some code to reproduce the situation and analyze what is actually happening.


Now about defaults:

What is the default mechanism for Java SDK, if i don't specify any retry config?

You can lookup the default values of the ClientConfiguration constant fields. But note, that it may differ depending on the service you use (in particular DynamoDB is a special case). Check also PredefinedClientConfigurations and PredefinedRetryPolicies classes.

laughedelic
  • 6,230
  • 1
  • 32
  • 41
  • I am new to AWS, and my requirement is to do copy between different bucket and if there is any exception occurs while copying or reading objects method should retry. I am using spring boot, java, aws sdk 1.11.x. can you please provide any example on it. – sk.bng88 Dec 17 '19 at 06:49
  • I'm sorry, but I think if you have some specifics not covered in this general answer, you should post it as a question (mentioning this one), so it can be answered separately. – laughedelic Dec 18 '19 at 17:09
  • The docs mention "client errors (4xx) indicate that you need to revise the request to correct the problem before trying again". Worth noting that this seems potentially false. I am using the AWS Java SDK Cloudwatch client (AmazonCloudWatch) 1.12 and if you hit it too hard you get a 400 AmazonServiceException with "Error Code: Throttling". This is an AWS-side throttling exception - which should be a 5xx error, not a 400 error. Unfortunately, one must build their own retry logic for these errors. – Zachary Steudel Nov 07 '22 at 22:54