How to configure DynamoDB retryLimit and retryDelay in Node js API?
amazon-dynamodb, amazon-web-services, javascript, node.js
Solution
The config is pretty limited, and the only retry parameter you can set on it is `maxRetries`.
maxRetries (Integer) — the maximum amount of retries to attempt with a request. See AWS.DynamoDB.maxRetries for more information.
You should set the maxRetries to a value that is appropriate to your use case.
aws.config.maxRetries = 20;
The `retryDelays` private API uses internally the `maxRetries` config setting, so setting that parameter globally like in my code above should work. The `retryLimit` is completely useless, and forget about it.
The number of retries can be set through configuration, but seems that there is not an elegant way to set the retry delay/backoff strategy etc.
The only way to manipulate those is to listen to the `retry` event, and manipulate the retry delay (and related behavior) in a event handler callback:
aws.events.on('retry', function(resp) {
// Enable or disable retries completely.
// disabling is equivalent to setting maxRetries to 0.
if (resp.error) resp.error.retryable = true;
// retry all requests with a 2sec delay (if they are retryable)
if (resp.error) resp.error.retryDelay = 2000;
});
Be aware that there is an exponential backoff strategy that runs internally, so the retryDelay is not literally 2s for subsequent retries. If you look at the internal service.js file you will see how the function looks:
retryDelays: function retryDelays() {
var retryCount = this.numRetries();
var delays = [];
for (var i = 0; i < retryCount; ++i) {
delays[i] = Math.pow(2, i) * 30;
}
return delays;
}
I don't think it's a good idea to modify internal API's, but you could do it by modifying the prototype of the Service class:
aws.Service.prototype.retryDelays = function(){ // Do some }
However, this will affect all services, and after looking in depth at this stuff, it is obvious their API wasn't built to cover your use-case in an elegant way, through configuration.
Problem
I have pretty high traffic peaks, thus I'd like to overwrite the dynamodb retry limit and retry policy. Somehow I'm not able to find the right config property to overwrite the retry limit and function. my code so far ``` var aws = require( 'aws-sdk'); var table = new aws.DynamoDB({params: {TableName: 'MyTable'}}); aws.config.update({accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_KEY}); aws.config.region = 'eu-central-1'; ``` I found the following amazon variables and code snippets, however I'm not sure how to wire this up with the config? ``` retryLimit: 15, retryDelays: function retryDelays() { var retryCount = this.numRetries(); var delays = []; for (var i = 0; i < retryCount; ++i) { if (i === 0) { delays.push(0); } else { delays.push(60*1000 *i); // Retry every minute instead // Amazon Defaultdelays.push(50 * Math.pow(2, i - 1)); } } return delays; } ```