Building a Simple Cache Service using AWS S3

3 min readSep 30, 2022

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
Key: `${key}.json`
return JSON.parse(Body.toString());
catch(e) {
return defaultValue;
};const set = (key, value) =>
Key: `${key}.json`,
Body: JSON.stringify(value)
.promise();module.exports = {

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');

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
  • Delete prefix-containing cache keys
  • Tag cache keys to enable deletion or retrieval of all keys with a certain tag.

About CodeStax.Ai

At CodeStax.Ai, we stand at the nexus of innovation and enterprise solutions, offering technology partnerships that empower businesses to drive efficiency, innovation, and growth, harnessing the transformative power of no-code platforms and advanced AI integrations.

But the real magic? It’s our tech tribe behind the scenes. If you’ve got a knack for innovation and a passion for redefining the norm, we’ve got the perfect tech playground for you. CodeStax.Ai offers more than a job — it’s a journey into the very heart of what’s next. Join us, and be part of the revolution that’s redefining the enterprise tech landscape.




Tech tales from our powerhouse Software Engineering team!