โš™๏ธ Server Configuration & Tuning

This guide covers operational parameters, memory pool configuration, CPU core pinning, and kernel bypass tuning for KacheDB.


๐Ÿš€ Daemon Command-Line Arguments (kachedb-server)

The kachedb-server binary accepts several command-line flags to control hardware allocation and network binding:

./target/release/kachedb-server [OPTIONS]

Options Reference

FlagLong OptionDescriptionDefaultRecommended Production
-c--config <PATH>Path to configuration file (e.g., kachedb.conf)None/etc/kachedb/kachedb.conf
-p--port <PORT>TCP listening port63796379
-w--workers <NUM>Number of worker threads (1 per CPU core)Auto (all physical cores)Equal to physical CPU cores
--pool-mb <MB>Megaslab memory pool allocated per core in megabytes64256 or 1024
--maxclients <NUM>Maximum simultaneous client connections1000010000โ€“65535
--requirepass <PASS>Password required for client authentication via AUTHNoneSet strong password
--snapshot / --no-snapshotEnable or disable periodic binary snapshottingtruetrue
--snapshot-file <PATH>Path to binary snapshot file (dbfilename)dump.kdb/var/lib/kachedb/dump.kdb
--snapshot-interval <SECS>Interval in seconds between snapshots300300โ€“900
--snapshot-encryptionEnable AES-256-GCM / ChaCha20 streaming encryptionfalsetrue (for sensitive/KV data)
--snapshot-cipher <CIPHER>AEAD cipher (aes-256-gcm or chacha20-poly1305)aes-256-gcmaes-256-gcm
--snapshot-key <KEY>Secret passphrase or 64-character hex keyNonePassphrase or hex string
--snapshot-key-file <PATH>Path to 32-byte raw binary master key fileNone/etc/kachedb/snapshot.key
--aofEnable Append-Only File (AOF) persistencefalsetrue (if durability needed)
--aof-path <PATH>Path to Append-Only File logkachedb.aof/var/lib/kachedb/kachedb.aof
--appendfsync <POLICY>AOF disk sync policy (always, everysec, no)everyseceverysec
--shm <true|false>Enable POSIX Shared Memory (/dev/shm) IPCtruetrue
--tls-cert <PATH>Path to TLS server certificate PEM fileNoneOptional
--tls-key <PATH>Path to TLS private key PEM fileNoneOptional
--tls-ca <PATH>Optional path to CA certificate for mTLS verificationNoneOptional

๐Ÿ“„ Configuration File (kachedb.conf)

KacheDB supports declarative configuration using a canonical kachedb.conf file:

# Network & Binding
bind 127.0.0.1
port 6379
maxclients 10000

# Worker Threads & Memory
workers 4
pool_mb_per_core 256

# IPC & LLM Tensor Streaming
shm yes

# Binary Snapshotting & Encryption-at-Rest
snapshot yes
dbfilename dump.kdb
snapshot-interval 300
snapshot-encryption yes
snapshot-encryption-cipher aes-256-gcm
# snapshot-encryption-key-file /etc/kachedb/snapshot.key
snapshot-encryption-key "your_secret_passphrase_here"

# Persistence (Append-Only File)
appendonly no
appendfilename kachedb.aof
appendfsync everysec

# Security
# requirepass my_secret_token

To launch kachedb-server using the configuration file:

./target/release/kachedb-server -c /path/to/kachedb.conf

Note: Any explicit CLI flags will take precedence over directives inside the configuration file.


๐Ÿงต Thread-per-Core Topology & CPU Pinning

KacheDB operates on a shared-nothing, thread-per-core architecture:

  • Each active worker thread is pinned to a dedicated physical CPU core using core_affinity.
  • Zero Cross-Core Contention: Each worker thread owns its private 2 MB Megaslab arena pool and independent Swiss Table shard.
  • Request execution requires no global mutex locks, eliminating lock contention and cache-line bouncing.

Example: Running on a Dedicated 8-Core Node

# Pin 8 workers to cores 0..7 with 512 MB memory per core (4 GB total)
./target/release/kachedb-server -p 6379 -w 8 --pool-mb 512

๐Ÿงฑ Memory Sizing & S3-FIFO Quota Management

Memory is managed through uniform 2 MB Megaslabs:

  • Rather than calling malloc() on every request, KacheDB pre-allocates contiguous megaslab page frames.
  • Per-Core Sizing: If --pool-mb is set to 256 on a 4-core machine, total initial memory allocated across the daemon is $4 \times 256\text{ MB} = 1.024\text{ GB}$.
  • Elastic Borrowing: The dynamic WorkloadQuota manager elastically allocates megaslabs between application key-value cache and tensor memory based on current demand.

RESP request limits

A connection can buffer up to 64 MiB of unread RESP input. Each command can use up to 64 MiB including framing, 2 MiB per argument, and 65,536 arguments including the command name. The limits apply to plaintext and TLS connections. Pipelined commands each get a separate command budget. A request that exceeds a protocol limit closes the connection.


AOF Durability and Repair

Set appendfsync in the configuration file or pass --appendfsync when launching the server.

PolicyWhen a write returns successFull queue
alwaysAfter the record is written and syncedWaits up to one second before rejecting the write
everysecAfter publication and queueing, with a background sync about once per secondRejects immediately
noAfter publication and queueing, with the OS responsible for routine flushingRejects immediately

A rejected admission leaves the data unchanged. An error reporting an uncertain outcome means the change was already published and its record may be on disk. The server rejects further writes once it detects AOF failure. Successful checkpoints and clean shutdowns sync the journal under every policy.

Background compaction writes a self-contained base and collects up to 64 MiB or 65,536 later records while the active journal continues receiving writes. If the delta budget is exhausted, the server abandons the rewrite and keeps the active journal. After a successful swap, startup uses the embedded base. Its integrity is checked by CRC32, and it is stored in plaintext like the rest of the AOF.

Startup validates journal framing and requires a compatible snapshot when the journal depends on one. It automatically removes an incomplete final record only when the length header is intact and checksummed. For framing damage or an ambiguous tail, stop the server and use the offline repair command:

kachedb-server --aof-repair --aof-file /var/lib/kachedb/kachedb.aof

Repair writes a separate copy of the original journal, then truncates at the end of the last valid frame. A damaged embedded base payload requires recovery from another copy. All records after that point are discarded. If the retained prefix ends before a required snapshot checkpoint, startup reports the mismatch.


๐Ÿ” Snapshot Encryption-at-Rest Configuration

KacheDB provides zero hot-path overhead streaming AEAD encryption for on-disk snapshots (dump.kdb).

Configuration Directives & CLI Flags

kachedb.conf DirectiveCLI Flag EquivalentDefaultDescription
snapshot-encryption yes|no--snapshot-encryption, --no-snapshot-encryptionnoEnable or disable authenticated streaming encryption for snapshots.
snapshot-encryption-cipher <name>--snapshot-cipher <cipher>aes-256-gcmAEAD cipher suite: aes-256-gcm (hardware-accelerated) or chacha20-poly1305.
snapshot-encryption-key-file <path>--snapshot-key-file <path>NonePath to a 32-byte binary master key file (recommended for production).
snapshot-encryption-key <string>--snapshot-key <key>None64-character hexadecimal key or variable-length passphrase (derived via HKDF-SHA256).

Example CLI Launch

# Start daemon with hardware AES-256-GCM encryption using a binary key file
./target/release/kachedb-server -c kachedb.conf \
  --snapshot-encryption \
  --snapshot-cipher aes-256-gcm \
  --snapshot-key-file /etc/kachedb/snapshot.key

For the comprehensive wire format (KDB\x03) and security architecture, see Snapshot Encryption-at-Rest.


๐Ÿง Linux Kernel & io_uring Tuning

For maximum throughput on Linux (> 2.5M QPS), apply the following kernel optimizations:

1. somaxconn & TCP Backlog

sudo sysctl -w net.core.somaxconn=65535
sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535

2. POSIX Shared Memory Limits (/dev/shm)

Ensure /dev/shm has sufficient space for high-volume LLM KV-cache offloading:

# Verify current /dev/shm size
df -h /dev/shm

# Remount /dev/shm with 32 GB (if serving 70B+ LLM inference nodes)
sudo mount -o remount,size=32G /dev/shm

3. File Descriptor Limits

ulimit -n 1048576

๐Ÿณ Production Docker Configuration

Here is the recommended production docker-compose.yml:

services:
  kachedb:
    image: ghcr.io/vubon/kachedb:latest
    container_name: kachedb
    privileged: true
    ipc: host
    network_mode: host
    restart: always
    command: ["-p", "6379", "-w", "4", "--pool-mb", "256"]
    ulimits:
      nofile:
        soft: 1048576
        hard: 1048576
      memlock:
        soft: -1
        hard: -1