Created
September 15, 2022 05:52
-
-
Save therightstuff/91f6e128c467c2c9dc68c76772f3489f to your computer and use it in GitHub Desktop.
Atomic DynamoDB increment and creation/update time with Axios/API Gateway/Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const aws = require('aws-sdk'); | |
const dynamodb = new aws.DynamoDB.DocumentClient(); | |
let corsHeaders = { | |
'Access-Control-Allow-Origin': process.env.CORS_ORIGIN, | |
'Access-Control-Allow-Credentials': true, | |
}; | |
exports.handler = async () => { | |
const promise = new Promise(async (resolve) => { | |
try { | |
let response = await dynamodb.update({ | |
TableName: process.env.TABLE_NAME, | |
Key: { "hashKeyName": "identity" }, | |
UpdateExpression: | |
`set creation_time = if_not_exists(creation_time, :update_time), | |
update_time = :update_time, | |
num_updates = if_not_exists(num_updates, :zero) + :increase`, | |
ExpressionAttributeValues: { | |
":update_time": Date.now(), | |
":increase": 1, | |
":zero": 0 | |
}, | |
ReturnValues: "ALL_NEW" | |
}).promise(); | |
resolve({ | |
"isBase64Encoded": false, | |
"statusCode": 200, | |
"headers": corsHeaders, | |
"body": JSON.stringify(response) | |
}); | |
} catch (err) { | |
console.error(err); | |
resolve({ | |
"isBase64Encoded": false, | |
"statusCode": 500, | |
"headers": corsHeaders, | |
"body": JSON.stringify(err) | |
}); | |
} | |
}); | |
return promise; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const http = require('http'); | |
http.globalAgent.maxSockets = 150; | |
let min, max; | |
let responseValues = {}; | |
let promises = []; | |
for (let i = 0; i < 500; i++) { | |
promises.push(axios({ | |
method: 'post', | |
url: 'https://a1b2c3d4e5.execute-api.us-east-1.amazonaws.com/prod/atomic-update', | |
data: {}, | |
headers: { | |
'Content-Type': 'application/json', | |
'x-api-key': 'abCDefGHijKLmnOPqrSTuvWXyz' // throttling set to 2000/200 | |
} | |
})); | |
} | |
Promise.all(promises) | |
.then(results => { | |
results.forEach(result => { | |
let counter = result?.data?.Attributes?.num_updates; | |
if (counter) { | |
responseValues[counter] = responseValues[counter] ? responseValues[counter] + 1 : 1; | |
if (!min || counter < min) min = counter; | |
if (!max || counter > max) max = counter; | |
} | |
}); | |
let unexpected = []; | |
for (key in responseValues) { | |
if (responseValues[key] !== 1) unexpected.push(key); | |
} | |
console.log(`min: ${min}, max: ${max}, unexpected: ${unexpected}`); | |
}) | |
.catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment