Browse documentation

FyroDB documentation

Production Tips

Best practices and configuration for running FyroDB in production.

Pre-warming the Hash Table

FyroDB starts with minimal shard tables (8 slots per shard) to keep idle memory at ~5 MB. When you first load 1M+ keys, the tables grow dynamically through several doublings (8→16→32→...→32768). Each growth pauses that shard briefly.

To avoid growth pauses during peak traffic, pre-warm the server after startup:

# Pre-warm with dummy keys, then delete them
redis-cli -p 8000 DEBUG POPULATE 100000
redis-cli -p 8000 FLUSHALL

Or insert your expected key count at deployment time before routing traffic. Benchmark the first-load and preloaded phases separately; the current suite reports insertion and overwrite rates as different metrics. FLUSHALL may release/replace warmed tables, so verify retained capacity rather than assuming that dummy insertion followed by a flush preserves it.

Choose workers from measurements

Physical cores, SMT threads and client CPU time are different resources. Sweep the worker count with a dedicated client CPU allocation first, and measure any shared-client/all-core case separately. The normal TCP results show gains from one to three physical cores, a smaller incremental GET gain with six workers on those cores plus SMT, and a different twelve-worker result when clients share all CPUs.

More workers can increase shared-counter traffic, cache/memory pressure and allocator footprint. Keep the value guards and Lua/transaction isolation enabled; reducing coordination is useful only when matched end-to-end tests improve. All benchmark methods, ranges and raw evidence.

Recommended Configuration

High-Throughput (dedicated server, 8+ cores)

FYRODB_WORKERS=0          # auto-detect CPU cores
FYRODB_SHARDS=0           # auto (workers × 4)
FYRODB_MAX_KEYS=10000000  # 10M keys
FYRODB_MAX_CLIENTS=50000

Memory-Constrained (Docker, 512MB limit)

FYRODB_WORKERS=2
FYRODB_SHARDS=8
FYRODB_MAX_KEYS=1000000
FYRODB_MAX_CLIENTS=5000

Low-Latency (small dataset, fast responses)

FYRODB_WORKERS=4
FYRODB_SHARDS=16
FYRODB_MAX_KEYS=100000

Docker Best Practices

services:
  fyrodb:
    image: rana718/fyrodb:latest
    ports:
      - "8000:8000"
    environment:
      - FYRODB_WORKERS=0
      - FYRODB_MAX_KEYS=5000000
      - FYRODB_RDB_PATH=/data/fyrodb.rdb
      - FYRODB_MALLOC_PURGE_DELAY_MS=0
    volumes:
      - fyrodb-data:/data
    deploy:
      resources:
        limits:
          memory: 2G

FyroDB sets its allocator options at startup, which overrides MIMALLOC_PURGE_DELAY. Use FYRODB_MALLOC_PURGE_DELAY_MS=0 instead to return freed memory to the OS immediately; this keeps container RSS close to live data at the cost of more page faults when memory is reused. The default is 3,000 ms. Independently, a worker that has been idle for one second returns its freed allocator memory.

Memory Behavior

FyroDB uses mimalloc which eagerly decommits unused pages. After bulk deletes or FLUSHALL:

  • RSS drops within 10 seconds (periodic mi_collect)
  • FLUSHALL triggers immediate collection + purge
  • No manual MEMORY PURGE needed (unlike Redis with jemalloc)

If you store 1M keys and then delete them, RSS returns close to the ~5 MB baseline within 10-20 seconds automatically.

What does not come back

A mixed workload leaves some memory resident that a pure string workload does not. After the full benchmark suite plus FLUSHALL, a node reports used_memory: 0.58 MB against ~50 MB resident — almost nothing is live, and the rest is allocator pages held for reuse. Deleting only part of a keyspace is worse: half-emptied pages cannot be returned at all, and mem_fragmentation_ratio in INFO will show it. Both figures are now real rather than derived from RSS, so the ratio is worth watching.

Redis Cluster behaves the same way in this test — 120 MB resident before and after a flush — so this is allocator behaviour rather than something specific to FyroDB.

Sorted Set Performance

Sorted sets use a score-ordered Vec with a bloom filter for fast membership checks:

  • Ascending score inserts (leaderboards, time-series): O(1) append — fastest path
  • Random score inserts: O(log n) position search + O(n) shift
  • Score range queries (ZRANGEBYSCORE, ZCOUNT): O(log n) binary search
  • Member lookup (ZSCORE, ZRANK): O(n) linear scan

For best ZADD performance, insert members with ascending scores when possible. The bloom filter skips the O(n) scan for new members that definitely don't exist yet.

Connection Tuning

Each connection uses:

  • 2 KB read buffer (grows on demand for large commands)
  • Write buffer shrinks to 32 KB after large responses

For thousands of idle connections, memory per connection is minimal. Under load, buffers grow as needed and shrink back automatically.

Persistence

  • RDB saves every 300 seconds by default (FYRODB_RDB_INTERVAL)
  • Saves are atomic: temp file → fsync → rename
  • BGSAVE runs in a background thread (no fork, no copy-on-write spike)
  • On SIGTERM/Ctrl+C, a final save is performed before shutdown

For write-heavy workloads, increase the interval:

FYRODB_RDB_INTERVAL=600  # save every 10 minutes

Monitoring

Use the INFO command to check memory health:

redis-cli -p 8000 INFO

Key metrics:

  • used_memory — actual data allocated
  • used_memory_rss — OS-level RSS (physical memory)
  • mem_fragmentation_ratio — RSS / allocated (close to 1.0 is ideal)
  • connected_clients — active connections
  • total_keys — live key count

DB Cache Proxy in Production

Proxy Connections

Each proxy client connection is served by its own thread, separate from the KV engine's workers. FYRODB_PROXY_MAX_CONNECTIONS caps how many are accepted at once (default 4096, 0 = unlimited); clients over the cap get PostgreSQL's 53300 sorry, too many clients already. Size it to the sum of your applications' connection pools plus headroom.

FYRODB_PROXY_MAX_CONNECTIONS=512   # e.g. 8 app instances x 32-connection pools, doubled

TTL Tuning

Start conservative and tighten based on your staleness tolerance:

FYRODB_PROXY_CACHE_TTL_MS=30000     # 30 s — good default
FYRODB_PROXY_CACHE_TTL_MS=5000      # 5 s — for fast-changing tables
FYRODB_PROXY_CACHE_TTL_JITTER=10    # 10% jitter to spread expiry

FYRODB_PROXY_CACHE_TTL_JITTER spreads cache entry expiry across a ±10% window so a large cohort of entries doesn't expire simultaneously and hammer the database (thundering herd).

Trust Auth

Set FYRODB_PROXY_TRUST_AUTH=true on internal container networks where network-level isolation is already in place. This eliminates the SCRAM round-trip on every new connection, reducing connection setup latency from ~5 ms to under 1 ms.

FYRODB_PROXY_TRUST_AUTH=true   # safe inside Docker Compose / Kubernetes pod network

Upstream Pool

Cache hits never touch the upstream pool — only cache misses and new connections use it. The default of 4 idle connections per proxy worker is enough for most workloads. Increase only if you see upstream connection wait times in your query logs.

FYRODB_PROXY_UPSTREAM_POOL=8   # for workloads with sustained high miss rates

Health Checks

The proxy speaks full PostgreSQL wire protocol. Use standard PostgreSQL health tools:

pg_isready -h fyrodb-host -p 5432
# or from psql
psql -h fyrodb-host -p 5432 -c "SELECT 1"

Cache-Only vs Dual Mode

ModeWhen to use
dual (default)App connects cold; first query per pattern goes to Postgres, results are cached automatically
cache-onlyApp pre-warms the cache at startup; misses return an explicit error — predictable latency, no surprise database calls
FYRODB_PROXY_MODE=dual        # default — automatic cache population
FYRODB_PROXY_MODE=cache-only  # app controls what is cached; misses are errors

Recommended Proxy Configuration

High-throughput (8+ core server)

FYRODB_PROXY_UPSTREAM_POOL=32
FYRODB_PROXY_CACHE_MAX_KEYS=500000
FYRODB_PROXY_CACHE_TTL_MS=60000
FYRODB_PROXY_TRUST_AUTH=true

Low-latency reads (small dataset, sub-ms response target)

FYRODB_PROXY_CACHE_TTL_MS=5000
FYRODB_PROXY_CACHE_TTL_JITTER=5
FYRODB_PROXY_MODE=cache-only
FYRODB_PROXY_TRUST_AUTH=true

Memory-constrained container

FYRODB_PROXY_CACHE_MAX_KEYS=10000
FYRODB_PROXY_UPSTREAM_POOL=2
FYRODB_PROXY_MAX_CONNECTIONS=256