Install and Run DianaDb Server

Linux


sudo echo "deb [arch=amd64,arm64 trusted=yes] https://dist.databikers.com stable main" | sudo tee /etc/apt/sources.list.d/diana-db.list
sudo apt-get update
sudo apt-get install -y diana-db
    

The same commands work on amd64 and on 64-bit ARM (arm64): apt picks the package for your machine (diana-db_4.12.0_amd64.deb or diana-db_4.12.0_arm64.deb).


sudo service diana-db [status|start|stop]
    

wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-2.35-r1.apk
apk add glibc-2.35-r1.apk
wget https://dist.databikers.com/x86_64/diana-db-4.12.0-r0.apk
apk add --allow-untrusted ./diana-db-4.12.0-r0.apk;
    

On 64-bit ARM (aarch64) skip the glibc lines and use the aarch64 package:


wget https://dist.databikers.com/aarch64/diana-db-4.12.0-r0.apk
apk add --allow-untrusted ./diana-db-4.12.0-r0.apk;
    

sudo rc-service diana-db [status|start|stop]
    

Diana DB Command-Line Tool

Manage your self-hosted Diana DB server with a CLI tool diana-db supports starting/stopping the daemon, user, access and backup management.


# Start server (default config)
diana-db start

# Start with custom config
diana-db start -c /path/to/diana-db.conf

# Stop server
diana-db stop -c /path/to/diana-db.conf

# Display version
diana-db --version

# Display settings
diana-db --settings

# User management
diana-db add-user    -u alice -p s3cr3t
diana-db grant-access -u alice -db main:3
diana-db remove-access -u alice -db main
diana-db remove-user -u alice

# Backup management
diana-db create-backup -n test_backup
diana-db list-backups
diana-db apply-backup -n test_backup
diana-db remove-backup -n test_backup
    
    
CommandFlags / ArgsDescriptionExit Code 0 on
--settings-sDisplays current settings
--version-vDisplays current version
start-c <conf>Stops any running instance, then launches diana-db-server in background.Server started
stop-c <conf>Kills running diana-db-server process.No instance found or Stopped
add-user-u <user> -p <pass>Creates the user. The password itself is never stored: since 4.6.5 the users file holds a random salt and verifiers made from the password (and, for clients older than 4.6.5, the key they use). Run it on every server of a cluster. The name may hold letters, digits, _, -, + and @, 64 at most (since 4.8.33). For a user that exists it changes the password.User added, or its password changed
remove-user-u <user>Removes the given user. Fails when there is no such user (since 4.8.33).User removed
grant-access-u <user> -db <db:access>Grants database access: diana-db grant-access -u admin -db main:3
Access levels: 1 read, 2 write, 3 manage (0 no access). See Security for what each level allows.
Access granted
remove-access-u <user> -db <db>Removes database accessAccess removed
list-backupsLists the backups: the .didb files next to the current dumpList displayed
create-backup-n <name>Creates backup named [name].didb. The server must be stopped: the command refuses to run next to a running server, and after a crash until the server was started and stopped once, because the dump would lack the last changes. The name may hold letters, digits, _, - and ., and can't be currentBackup created
apply-backup-n <name>Applies backup named [name].didb and deletes the write-ahead log files, which belong to the replaced data. Server should be stoppedBackup applied
remove-backup-n <name>Removes backup named [name].didb. Backup removed

diana-db and diana-db-server read a simple key=value config (/etc/diana-db/diana-db.conf, or the file given with -c / --config). Each key has an environment variable of its own.

Since 4.10.1 the server reads the file itself. Before, it read the environment only: started by systemd or OpenRC it ignored the file, and the port, the directories and every other setting in it had no effect. After the upgrade those settings apply, so check the file before you restart. A variable that is set in the environment wins over the file, which is how a container is configured (docker run -e DIANA_DB_PORT=…). A file that was named, or that exists and cannot be read, stops the start. The server's log names the settings it took from the file and the names in the file that are not settings.

KeyEnvironment VariableTypeDescriptionExample
portDIANA_DB_PORTIntegerListening TCP port34567
dump_create_intervalDIANA_DB_DUMP_CREATE_INTERVALhour or dayHow often to create a backup of the current dump. The first one is taken one interval after start, so a damaged dump can't overwrite the last good backup at bootday
logs_directoryDIANA_DB_LOG_DIRECTORYString (path)Directory where server logs are written/var/log/diana-db
dump_directoryDIANA_DB_DUMP_DIRECTORYString (path)Directory where DB dumps are stored/var/lib/diana-db
logs_ttl_valueDIANA_DB_LOG_TTLString, <days>dTime-to-live for log retention7d (7 days)
backup_nameDIANA_DB_BACKUP_NAMEStringDefault filename used when creating a backup without -nbackup.didb
store_strategyDIANA_DB_STORE_STRATEGYStringStorage backend for dumps/backups: local or s3local
dump_compressionDIANA_DB_DUMP_COMPRESSIONString How snapshots are written: none (default) or gzip, with both storage backends. Since 4.10.0. See belowgzip
store_s3_endpointDIANA_DB_STORE_S3_ENDPOINTString (URL)S3-compatible endpoint URL. Required when store_strategy=s3https://s3.us-east-1.amazonaws.com
store_s3_bucketDIANA_DB_STORE_S3_BUCKETStringTarget bucket name. Required when store_strategy=s3my-diana-backups
store_s3_regionDIANA_DB_STORE_S3_REGIONStringS3 region. Required when store_strategy=s3us-east-1
store_s3_access_key_idDIANA_DB_STORE_S3_ACCESS_KEY_IDStringS3 access key ID. Required when store_strategy=s3AKIA...
store_s3_secret_access_keyDIANA_DB_STORE_S3_SECRET_ACCESS_KEYStringS3 secret access key. Required when store_strategy=s3wJalrXUt...
ssl_cert_fileDIANA_DB_SSL_CERT_FILEString (path)Path to the server's TLS certificate file. Enables TLS when set together with ssl_key_file. Since 4.6.13 the server does not start when a certificate, key or CA file is missing or unreadable, or when only a part of the TLS settings is given, instead of starting without TLS/etc/ssl/certs/server.crt
ssl_key_fileDIANA_DB_SSL_KEY_FILEString (path)Path to the server's TLS private key file/etc/ssl/private/server.key
ssl_ca_fileDIANA_DB_SSL_CA_FILEString (path)Path to a CA certificate used to verify client certificates (mTLS)/etc/ssl/certs/ca.crt
ssl_request_certDIANA_DB_SSL_REQUEST_CERTBooleanRequest a client certificate for mutual TLS (mTLS)1
ssl_reject_unauthorizedDIANA_DB_SSL_REJECT_UNAUTHORISEDBooleanReject connections presenting certificates that fail verification1
current_dump_nameDIANA_DB_DUMP_CURRENT_NAMEStringFilename for the current/latest dumpcurrent.didb
transactions_min_auto_rollback_valueDIANA_DB_TRANSACTION_AUTOROLLBACK_MINInteger (ms)Smallest autoRollBackAfterMs a client may request1000
transactions_max_auto_rollback_valueDIANA_DB_TRANSACTION_AUTOROLLBACK_MAXInteger (ms)Largest autoRollBackAfterMs a client may request, and the timeout of transactions started without one60000
walDIANA_DB_WALBoolean Write-ahead log, kept in dump_directory with both storage backends (with s3 since 4.9.0). On by default; 0 turns it off. See below1
wal_fsyncDIANA_DB_WAL_FSYNCBooleanfsync the log before answering. 0 trades crash safety for latency: a power loss can lose the last writes1
server_idDIANA_DB_SERVER_IDString (exactly 12 lowercase letters or digits)First part of every new document _id, the same shape as a MAC address so all _ids look alike. Required in a cluster, different on every server. Since 4.6.5 the server does not start with an id of another shape; _ids stored by earlier versions stay validMAC address
memory_reserve_percentDIANA_DB_MEMORY_RESERVE_PERCENTInteger (1–50)Requests are refused while less than this share of the memory the server can reach is left. See memory guard5
legacy_authDIANA_DB_LEGACY_AUTHBooleanAccept clients older than 4.6.5, which authenticate without a salt and without a key per connection. Set 0 once every client is 4.6.5 or newer. See Security1
handshake_timeoutDIANA_DB_HANDSHAKE_TIMEOUTSeconds, 1–300Since 4.7.15. A connection that has not authenticated within this time is closed. Until then it may send lines of 1024 characters at most. Also the time allowed for a TLS handshake10
max_pending_connectionsDIANA_DB_MAX_PENDING_CONNECTIONSNumberSince 4.7.15. How many connections may be in their handshake at the same time. One more is closed at once. Authenticated connections are not counted and not limited1024
max_pending_connections_per_ipDIANA_DB_MAX_PENDING_CONNECTIONS_PER_IPNumberSince 4.7.15. The same, per remote address. Raise it when many clients connect through one address (a proxy, NAT) and start at the same moment128
httpDIANA_DB_HTTPBooleanHTTP server with /health and /metrics. See Monitoring1
http_portDIANA_DB_HTTP_PORTInteger (1025–65535)Port of the HTTP server34568
http_hostDIANA_DB_HTTP_HOSTStringAddress the HTTP server listens on. The default accepts requests from the same machine only127.0.0.1
http_tokenDIANA_DB_HTTP_TOKENString (16+ characters)When set, HTTP requests need Authorization: Bearer <token>–
cluster_peersDIANA_DB_CLUSTER_PEERShost:port,host:portThe other servers of the cluster. See Replication–
cluster_secretDIANA_DB_CLUSTER_SECRETString (16+ characters)The same on every server of the cluster: servers authenticate each other with it and encrypt their connections–
cluster_portDIANA_DB_CLUSTER_PORTInteger (1025–65535)Port the other servers connect to34569
cluster_hostDIANA_DB_CLUSTER_HOSTStringAddress the cluster port listens on0.0.0.0
cluster_tombstone_ttlDIANA_DB_CLUSTER_TOMBSTONE_TTL7d, 48hHow long removed documents are remembered, so a late change can't bring them back7d
cluster_oplog_sizeDIANA_DB_CLUSTER_OPLOG_SIZEInteger (100 or more)Changes kept in memory for servers that reconnect. A server further behind receives the whole state100000

A Boolean setting takes 1, yes, true, on or 0, no, false, off, in any letter case. Since 4.8.32 any other value stops the start; before, only 0, no and false turned a setting off, so wal=off left the write-ahead log on. A wrong setting ends the start with one line, 000 Configuration Error: …, and exit code 1.

Example: /etc/diana-db/diana-db.conf

This file uses simple key=value syntax. Comments starting with # are ignored.



# Path to Diana DB server binary
binary_path=/usr/bin/diana-db-server

# Server port
port=34567

# Auto rollback settings for transactions
transactions_min_auto_rollback_value=1000
transactions_max_auto_rollback_value=60000

# Log directory and retention policy
logs_directory=/var/log/diana-db
logs_ttl_value=7d

# Directory where database dumps are stored
dump_directory=/var/lib/diana-db
current_dump_name=current.didb
backup_name=backup.didb
dump_create_interval=day

# Storage strategy: local (default) or s3
store_strategy=local
# store_strategy=s3
# store_s3_endpoint=
# store_s3_bucket=
# store_s3_region=
# store_s3_access_key_id=
# store_s3_secret_access_key=

# Snapshots written as gzip: about a tenth of the size, for some processor time at every save
# dump_compression=gzip

# Write-ahead log, kept in dump_directory with both storage strategies (on by default)
# wal=1
# wal_fsync=1

# First part of new _ids: exactly 12 lowercase letters or digits (default: the MAC address)
# server_id=dianadbnode1

# HTTP server for monitoring: /health and /metrics
# http=0
http_port=34568
# http_host=0.0.0.0
# http_token=

# Clients older than 4.6.5 (set 0 once every client is 4.6.5 or newer)
# legacy_auth=0

# Connections that have not authenticated yet: seconds they may take, how many at once in all and per address
# handshake_timeout=10
# max_pending_connections=1024
# max_pending_connections_per_ip=128

# Replication: the other servers and the secret they share
# cluster_secret=
# cluster_peers=10.0.0.2:34569,10.0.0.3:34569
# cluster_port=34569
# cluster_tombstone_ttl=7d
# cluster_oplog_size=100000

# TLS / mTLS (optional)
# ssl_cert_file=/path/to/file
# ssl_key_file=/path/to/file
# ssl_ca_file=/path/to/file
# ssl_request_cert=1
# ssl_reject_unauthorized=1

Durability: snapshots and the write-ahead log

DianaDB keeps data in memory and saves a snapshot of every database to dump_directory about every 37 seconds. Since 3.0.0 the server also keeps a write-ahead log there (wal-*.log), so changes made after the last snapshot survive a crash:

  • Every change — inserted documents, final values of updates, removed ids, schema, view and migration changes — is written to the log. Changes from one event-loop turn are written together in one durable write, and the server answers those requests only after that, so an acknowledged write is on disk.
  • Since 4.8.36 a log file takes its space in advance, 1 MiB at a time, and entries are written into that space. A write that makes a file longer costs the file system a second trip to the disk; on ext4 this change took a durable write from about 0.7 ms to under 0.3 ms. So a current log file is a whole number of mebibytes in size and ends in zeros. A finished file is cut back to its entries, and after a crash replay stops at the unused space. With wal_fsync off the file simply grows.
  • When a snapshot is taken the log switches to a new file; files the snapshot covers are deleted once it is safely on disk.
  • At start-up the snapshot is restored and the remaining log is replayed. Replay is idempotent, and a fresh snapshot is written right after it.
  • Snapshots are written to a temporary file, flushed and renamed over the old one, so a crash or full disk during a save never leaves a truncated dump. SIGINT/SIGTERM save every database before the process exits.
  • If the log can't be written, the change stays applied in memory and the client gets a DURABILITY error.
  • Since 4.9.0 the log is used with store_strategy=s3 too: see Snapshots in S3 below. Before, with s3, changes made since the last uploaded snapshot were lost when the server was killed.
  • Log writes and fsync run on a background thread: a slow disk delays the answers waiting for durability, not other requests.
  • Snapshots are written straight from the databases, one field at a time, with other requests running in between. Saving needs memory for one field's copy, not for a copy of every database.

Compressed snapshots

With dump_compression=gzip (since 4.10.0) snapshots are written as gzip, with both storage strategies. A snapshot is long runs of similar lines, so it shrinks to about a tenth. Compression runs on a background thread at the fastest level; it costs processor time at every save, about 0.85 CPU-seconds for a snapshot of 95 MB in our measurement.

  • The setting says how snapshots are written. Snapshots and backups of both kinds are read whatever it says (the first two bytes tell them apart), so it can be switched at any time and backups made before the switch can still be applied.
  • The file keeps its name. The users file and the write-ahead log are never compressed.
  • A gzip snapshot that is damaged or cut short stops the start; the server does not begin empty.

Snapshots in S3

With store_strategy=s3 the snapshot, the backups and the users file are objects in the bucket.

  • The write-ahead log (4.9.0). The log is kept in dump_directory on the server's machine and replayed after the snapshot is restored from the bucket. Changes made after the last upload survive a killed server, and stay in the log while uploads fail. dump_directory must exist, be writable and outlive a restart (a volume, in a container); a directory that cannot be written stops the start with a configuration error. wal=no turns the log off.
  • The log belongs to one snapshot. Every uploaded snapshot carries a generation in its metadata, and wal.generation next to the log records which generations the log follows. If the snapshot in the bucket is not one of them (another server wrote to the bucket, the bucket was emptied, a backup was applied from another machine), the log is not replayed onto it: the server refuses to start and names the files to delete to start from the bucket's snapshot.
  • Backups. apply-backup gives the snapshot a new generation and removes the log of the replaced one on the machine it runs on. create-backup refuses while the log has changes the bucket lacks: start the server, stop it, then create the backup.
  • Uploads (4.8.38). A snapshot is uploaded while it is written, a part at a time, and the server waits for S3 to confirm it. Before, the upload started after the snapshot was collected in memory and nobody waited for it: a failed upload was logged as saved, and the snapshot of a clean stop was never uploaded.
  • An empty bucket (4.8.39). The server starts with a bucket that has no snapshot or users yet, and diana-db add-user creates the users file there. Any other error of the bucket (no such bucket, access denied, no connection) stops the start.
  • Text outside ASCII (4.9.1). Earlier versions could damage such text when a snapshot was restored from S3: a character cut by the end of a chunk became two replacement characters, in the restored documents and in the next snapshot. Upgrade before the next restart if your data has non-ASCII text.

The user the service runs as

Packages of 4.10.2 and later run the service as the system user diana-db, not as root. /var/lib/diana-db, /var/log/diana-db and /etc/diana-db can be entered by that user and root only, and the settings file is readable by them only: it can hold the S3 keys, cluster_secret and http_token.

  • Before every start the service gives the data and log directories to diana-db, which repairs installations that an earlier server ran as root.
  • diana-db (the tool) is still run by root. Since 4.10.2 the files it creates there (the users file, backups, the log file of the day) are given to the owner of their directory.
  • A dump_directory or logs_directory changed in the settings file has to be given to the user by hand: chown -R diana-db:diana-db <directory>.

Memory limit and memory guard

All data is in memory. The server binary runs with a 64 GB JavaScript heap limit; the memory the machine or container gives the process is usually the lower limit. Twice a second the server compares what it uses with what it can still get (free system memory, or what is left of the container limit) and with its heap limit:

  • When less than memory_reserve_percent (default 5%) is left, or the next snapshot would not fit, every request is refused with 008 Memory Error (MEMORY in the ODM), except getHealth, getVersion, removals and transaction rollbacks, so memory can be freed without a restart. Snapshots are skipped meanwhile; changes are still in the write-ahead log.
  • Requests are accepted again once twice the reserve is free. Removing data, adding RAM or raising the container limit resolves it.
  • getHealth reports the current numbers in memoryGuard.
For security reasons we do not provide default database user. You add users yourself, choose a strong passwords which will be stored and transmitted in a secure way