⚠️ RebaseJS is under active development and not yet production-ready. APIs may change before v1.0. Feel free to explore, contribute, or star the repo.
Skip to content

RebaseJS includes a built-in memory cache by default, which is perfect for single-server deployments. However, as your application scales horizontally across multiple servers, you need a shared cache to ensure consistency and maximize hit rates.

To solve this, RebaseJS offers a Distributed Caching adapter powered by a high-performance, multi-core, local-first Redis alternative written in Rust.

Why Distributed Caching?

When using Incremental Static Regeneration (ISR) or useServerData with a cache: { ttl } option, the server caches the result. If you have 5 server instances, a memory cache means the database is queried 5 times (once per instance).

With a distributed cache, the first instance queries the database, caches the result globally, and the other 4 instances instantly read from the shared cache.

Enabling the Distributed Cache

To enable distributed caching, first install the caching package:

ts
// rebasejs.config.ts
import { defineConfig } from "rebasejs/config";

export default defineConfig({
  cache: {
    adapter: "distributed", // Enable the distributed cache adapter
    host: process.env.CACHE_HOST || "127.0.0.1",
    port: 6379,
  }
});

That's it! All ISR pages, server function caches, and manual cache entries are now synchronized across your entire infrastructure.

Cache Invalidation

RebaseJS supports tag-based cache invalidation. This means you can associate a cached database query with a tag (like posts), and invalidate all related caches whenever a new post is created.

tsx
// Fetch and cache data
export const getPosts = defineServerFn(async () => {
  const posts = await db.collection("posts").find({}).toArray();
  return posts;
}, { 
  cache: { ttl: 3600, tags: ["posts"] } 
});

// Invalidate the cache when a mutation occurs
export const createPost = defineServerFn(async (data) => {
  await db.collection("posts").insertOne(data);
  await invalidateCacheTags(["posts"]);
  return { success: true };
});

Because you are using the distributed cache adapter, calling invalidateCacheTags on one server will instantly invalidate the cache for all other servers in your cluster.

Released under the MIT License.