Skip to content

Instantly share code, notes, and snippets.

@mtp1376
Created March 2, 2023 11:38
Show Gist options
  • Save mtp1376/478860e7acbe55fab3727db968375eaf to your computer and use it in GitHub Desktop.
Save mtp1376/478860e7acbe55fab3727db968375eaf to your computer and use it in GitHub Desktop.
This script proves that AWS SQS APIs have a cache that caches the result based on the exact input for 40 seconds
import { SQS } from '@aws-sdk/client-sqs';
import { now } from "lodash";
const sqsClient = new SQS({
credentials: {
accessKeyId: 'X',
secretAccessKey: 'Y',
},
region: 'us-east-2'
});
(async function () {
// trigger cache
const cacheStart = now();
await sqsClient.listQueues({
QueueNamePrefix: 'test-cache-'
})
// create queue and get url
const queueUrl = (await sqsClient.createQueue({
QueueName: 'test-cache-1',
Attributes: {}
})).QueueUrl;
// show that listQueues does not return the new queue
if (!!((await sqsClient.listQueues({ QueueNamePrefix: 'test-cache-' })).QueueUrls?.length))
console.log('New queue shown in the listQueues with same args - - hypothesis is probably wrong');
else
console.log('New queue not shown in the listQueues with the same arg - hypothesis is probably right')
// use a new prefix - it should be shown
if (!!((await sqsClient.listQueues({ QueueNamePrefix: 'test-cache' })).QueueUrls?.length))
console.log('New queue shown in the listQueues with new args - - hypothesis is probably right');
else
console.log('New queue not shown in the listQueues with the new arg - hypothesis is probably wrong')
// wait until the cache is removed
while (!((await sqsClient.listQueues({ QueueNamePrefix: 'test-cache-' })).QueueUrls?.length)) {
// empty block
}
console.log(`AWS API cache takes ${now() - cacheStart} to invalidate`);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment