Skip to content
betterstack-alchemyUnofficial

Object cache

An ObjectCache is an S3-compatible cache in front of buckets at another storage provider, such as AWS S3 or R2. It serves reads from fast local disks and passes writes on to the original buckets. S3 clients connect to its endpoints, with path-style addressing and SigV4 signing: an HTTP endpoint in each attached network, and a public HTTPS endpoint with publicAccess: true. The buckets, and the data, stay at the origin.

your appS3 client · SigV4
assetsobject cache · OP100
my-assetsbucket
mediabucket
AWS S3origin buckets
  • your appreads, writes →assets
  • assetswrites through →AWS S3
Clients talk to the cache's endpoints. Each bucket it serves is backed by a bucket at the origin, which keeps the data.

Name the origin bucket and the keys the cache uses to reach it:

alchemy.run.ts
const cache = yield* BetterStack.Cloud.ObjectCache("assets", {
type: "OP100",
buckets: [
{
originEndpoint: "https://s3.us-west-2.amazonaws.com",
originBucket: "my-assets",
originRegion: "us-west-2",
originAccessKeyId: accessKeyId,
originSecretAccessKey: secretAccessKey,
},
],
cacheTtlSeconds: 3600,
publicAccess: true,
});

The origin keys are write-only: the API stores them encrypted and never returns them, so they are compared with the previous deploy. Pass them as Redacted, here read through Config. Creating a cache waits until it is running, for up to 30 minutes. type rescales in place; cacheTtlSeconds, evictionPolicy, maxCapacityGb and the other settings update in place and are applied in the background.

buckets is sent on create only. Add buckets to an existing cache as ObjectCacheBuckets:

alchemy.run.ts
yield* BetterStack.Cloud.ObjectCacheBucket("media", {
objectProxyId: cache.id,
originEndpoint: "https://s3.us-west-2.amazonaws.com",
originBucket: "my-media",
originRegion: "us-west-2",
originAccessKeyId: accessKeyId,
originSecretAccessKey: secretAccessKey,
});

Clients address a bucket by name, which defaults to originBucket. originPrefix serves one folder of a bucket under its own name.

Attach the cache to a private network and turn off its public endpoint:

alchemy.run.ts
const network = yield* BetterStack.Cloud.Network("private", {
ipPrefix: "10.0.0.0",
ipPrefixLength: 24,
});
yield* BetterStack.Cloud.ObjectCache("internal", {
type: "OP100",
buckets: [
{
originEndpoint: "https://s3.us-west-2.amazonaws.com",
originBucket: "my-assets",
originAccessKeyId: accessKeyId,
originSecretAccessKey: secretAccessKey,
},
],
networkIds: [network.id],
publicAccess: false,
});

The object cache is read by S3 clients that sign with SigV4. It doesn’t front an HTTP origin such as a server, so it can’t serve a website’s assets to browsers. Websites serve their own assets.

Every object cache this stack creates carries an alchemy entry in its metadata, which takes one of its 10 metadata slots, so a deploy that lost track of one finds it again and alchemy nuke lists only Alchemy-created caches. Buckets have no metadata, so nothing marks them as this stack’s. See the ObjectCache reference.