Skip to content

3. Databases and tenants

The words

  • Database. One server's data directory (dir in the configuration; /data/state in the container image). It holds everything the server stores: the aggregates of your schema, agent memory and agent state, the indexes, the layout registry, and the master key that seals personal data (dir/master.key). One oliviadb-server process serves one database. To create a database, start a server on an empty directory: there is no CREATE DATABASE; the directory is initialised at the first start.
  • Schema. One per database, given to the server in the configuration ("schema": "bank.olv"; step 4). It declares the aggregates the database holds and the profiles of everyone who may use them.
  • Tenant. A namespace inside a database. Every API key (or JWT, through a claim) names its tenant, and every read and write stays inside it: two tenants can each have a Customer/42, and neither sees the other's. All tenants share the schema. A tenant comes into being with its first key: there is nothing else to create.
  • Store. A directory of the storage engine. The server's database is one; a DL/I system (migrate from the mainframe) keeps its databases in stores of its own.

So: separate applications with separate schemas get separate databases (servers, each with its own dir and ports); one application serving several customers of its own uses one database with a tenant per customer.

A second tenant

Each key is one entry of api_keys: its hash, tenant, principal (who it is; the principal selects its profile, step 4) and scopes. Here the server gets a second key, for tenant globex, and is restarted to read its configuration:

(umask 077; openssl rand -hex 24 > api-key-globex)
cat > config.json <<EOF
{"listen": "127.0.0.1:8443", "native_listen": "127.0.0.1:7655", "dir": "data",
 "tls": {"cert": "server.pem", "key": "server.key"},
 "api_keys": [
   {"sha256": "$(oliviadb-server --hash-key "$(cat api-key)")", "tenant": "acme", "principal": "app", "profiles": ["read", "memory"]},
   {"sha256": "$(oliviadb-server --hash-key "$(cat api-key-globex)")", "tenant": "globex", "principal": "app", "profiles": ["read", "memory"]}]}
EOF
# Restart: stop, wait until the process is gone, start, wait until it serves.
kill "$(cat oliviadb.pid)"; while kill -0 "$(cat oliviadb.pid)" 2>/dev/null; do sleep 0.1; done
oliviadb-server --config config.json >> server.log 2>&1 &
echo $! > oliviadb.pid
until curl -fsS --cacert server.pem https://localhost:8443/readyz; do sleep 0.2; done

The container image configures one identity from its variables. For several keys or tenants, mount a configuration of your own and start the server with it (the container image).

Several nodes. A database can have a standby on another machine that takes over when the primary fails (high availability, OliviaDB Enterprise). Spreading one database over many machines is in development for OliviaDB Enterprise (multi-Raft plan).

With your coding agent

With the OliviaDB skills installed (step 0), the agent does this step from one request, and runs the commands above:

Ask your agent

Using the oliviadb-develop skill, add a second tenant to my OliviaDB server with its own API key, and restart it.

Next: write a schema.