RecipesMulti-tenant SaaS

Multi-tenant SaaS

The unit of isolation is the workspace, and the API key decides the workspace. recall has no workspaceId argument. To keep customer A’s memory away from customer B, give each tenant its own workspace and its own key.

One workspace and key per tenant

The first key of a workspace comes from the terminal. Without --workspace, the script generates a new workspace and prints the key once:

docker compose exec server node scripts/create-api-key.cjs --label "tenant acme"

Pass --workspace <uuid> to mint a key for a workspace that already exists. The value must be a UUID. With a key that holds the admin:keys scope, the CLI can mint more keys for that same workspace:

mnemo keys create --label "acme backend" --scopes memory:read,memory:write

Store each tenant’s key in your secret store.

One client per tenant

import { MnemosyneClient } from "@mnemo-ai/client-ts";
 
const clients = new Map<string, MnemosyneClient>();
 
function memoryFor(tenantId: string): MnemosyneClient {
  let client = clients.get(tenantId);
  if (!client) {
    client = new MnemosyneClient({
      url: "http://localhost:3939",
      apiKey: keyForTenant(tenantId), // your secret store
    });
    clients.set(tenantId, client);
  }
  return client;
}
 
app.get("/recall", auth, async (req, res) => {
  const memory = memoryFor(req.user.tenantId);
  const result = await memory.recall({ query: String(req.query.q) });
  res.json(result);
});

The alternative: one user token

A user token (uk_live_*) belongs to a user who can be a member of several workspaces. With it, one client can target each workspace through the X-Workspace-Id header:

const tenantMemory = memory.forWorkspace(workspaceId);
await tenantMemory.recall({ query: "firmware release cadence" });

The server checks that the user is a member of the requested workspace. A workspace key that asks for another workspace gets a 403. User tokens are minted with mnemo token issue <tenantId> <userId> --label <name> --workspace <homeWorkspaceId>.

What the database enforces

Tenant tables have PostgreSQL row-level security with FORCE. The server sets the workspace and the actor on every transaction, and in production it connects with a role that is NOSUPERUSER and NOBYPASSRLS, so a query that forgets a WHERE workspace_id clause still only sees the current workspace.

⚠️

The local stack started with docker compose up connects to Postgres as the database owner, which bypasses row-level security. Workspaces are not isolated from each other there. The production overlay (docker-compose.prod.yml) provisions the restricted role and turns the isolation on.

Backup per tenant

Export and import work on the workspace of the key you authenticate with. mnemo export writes gzip JSONL to stdout:

MNEMO_KEY=<acme key> mnemo export > acme.jsonl.gz
MNEMO_KEY=<restore key> mnemo import acme.jsonl.gz --conflict-resolution skip

A native export is imported without --from. The import always lands in the workspace of the key used, whatever workspace the file names.

Scopes

A key carries scopes, for example memory:read, memory:write and memory:hard_delete. A new key cannot be granted a scope its creator does not hold. No scope lets a workspace key read another workspace.