Building a Simple Cache Service using AWS S3

S3 is excellent for storing files, but it also has many other uses. I adore utilizing S3 as a straightforward caching system for any stateless services that require saving some transient data to maintain state.

In the past, you would have used in-memory caching solutions like Redis for this, and Redis still has a role because it will almost always be quicker than fetching data from S3. However, S3 is a cheap, low-effort, and straightforward to install option if millisecond speed is not a problem.

const { S3 } = require('aws-sdk');
const s3 = new S3({ region: 'ap-southeast-1' });
const { CACHE_BUCKET } = process.env;
const get = async(key, defaultValue = null) => {
try {
const { Body } = await s3
.getObject({
Bucket: CACHE_BUCKET,
Key: `${key}.json`
})
.promise();
return JSON.parse(Body.toString());
}
catch(e) {
return defaultValue;
}
};const set = (key, value) =>
s3
.putObject({
Bucket: CACHE_BUCKET,
Key: `${key}.json`,
Body: JSON.stringify(value)
})
.promise();module.exports = {
get,
set
};

Use them as follows (be sure to set the environment variable CACHE BUCKET):

const cache = require('./cache.js');
// ...
// ...
await cache.set('my-cache', { message: 'S3 as simple cache', boolValue: true });
const value = await cache.get('my-cache');
console.log(value);

The values in the code snippet above are being saved as JSON objects. Of course, you are free to store any kind of material you want (binary, plain text, etc.) as long as it serves your needs.

S3 Lifecycle configurations, work well when used in conjunction with S3 as a cache. This will enable you to have cache resources deleted automatically after a predetermined amount of time.

To limit the items in the bucket to which this auto-deletion rule is applied to, you may attach tags and filter by them, or you can add a Prefix or TagFilters condition.

You may wish to increase the size of our little cache if you frequently use this form of data caching.js helper that includes a few more methods. Due to S3’s flexibility, it should be rather simple to implement the following functions:

  • Removing cache keys

About KBX Digital

At KBX Digital we use server-less technology to auto scale micro-services to serve millions of customers.

If you want to join our team please apply in the link provided below.

Link to Apply

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store