MDDB Architecture

Note: This document describes the foundational architecture (storage layout, request flow). For an up-to-date list of every shipped subsystem (vector search, FTS, geo, MCP, GraphQL, auth, replication, TLS, UDS, etc.) see FEATURES and the canonical version table in the root README.

Overview #

MDDB is an AI-native embedded document database built on top of BoltDB. It serves a triple-protocol surface — HTTP/JSON REST, gRPC/Protobuf, and GraphQL — over either TCP or Unix Domain Sockets, with optional TLS / mTLS. A built-in MCP server (116 MCP tools, MCP 2026-07-28 and 2025-11-25 side by side) exposes the same operations to LLM agents over stdio, Streamable HTTP, and SSE transports.

Search is a layered stack: metadata indexes for filter pre-pruning, full-text search (TF-IDF / BM25 / BM25F / PMISparse, 8 modes, 17-language stemming, fuzzy / proximity), vector / semantic search (Flat / HNSW / IVF / PQ / OPQ / SQ / BQ + per-collection int8/int4 quantization, plug-in OpenAI / Ollama / Cohere / Voyage embeddings), geospatial search (R-tree + geohash), and hybrid search that combines BM25 and dense vectors via alpha blending or Reciprocal Rank Fusion.

Beyond storage and search, MDDB ships JWT authentication + RBAC, mTLS client-cert auth, leader-follower binlog replication, automation (triggers / crons / webhooks / sentiment / template variables), document TTL, revisions, schema validation, aggregations, and a React admin panel.

High-Level Architecture #

graph TB
    Client[HTTP Client]
    API[REST API Layer]
    Server[MDDB Server]
    Storage[BoltDB Storage]
    
    Client -->|HTTP Requests| API
    API -->|Route & Validate| Server
    Server -->|Read/Write| Storage
    Storage -->|Persist| DB[(mddb.db)]
    
    Server -->|Hooks| Webhooks[External Webhooks]
    Server -->|Hooks| Commands[System Commands]

Components #

1. Protocol layer (HTTP / gRPC / GraphQL / MCP) #

Routes requests to handlers, applies middleware (JSON, CORS, auth, rate limit, logging), enforces access modes.

For the full endpoint catalogue see API (HTTP/REST), GRPC (gRPC), GRAPHQL (GraphQL schema), MCP (MCP tools), and the REST API reference generated from openapi.yaml. The endpoint list lives in services/mddbd/endpoints_handlers.go and is queryable at GET /v1/endpoints.

2. Storage Layer (BoltDB) #

Database Structure:

mddb.db
├── docs/          # Current document versions
│   └── doc|{collection}|{docID} → JSON
├── idxmeta/       # Metadata indices
│   └── meta|{collection}|{key}|{value}|{docID} → 1
├── rev/           # Revision history
│   └── rev|{collection}|{docID}|{timestamp} → JSON
├── bykey/         # Key-to-ID mapping
│   └── bykey|{collection}|{key}|{lang} → docID   (key, lang: ASCII lower case)
└── schema_version/ # On-disk format version of this file
    └── version → uint64, big-endian

Foundational buckets (the four every collection always uses):

  1. docs — latest document version. Key: doc|{collection}|{docID}. Value: protobuf-encoded Doc (compressed via the configured codec).
  2. idxmeta — metadata index for fast filter pre-pruning. Key: meta|{collection}|{metaKey}|{metaValue}|{docID}. Value: existence marker. Enables prefix scans.
  3. rev — revision history. Key: rev|{collection}|{docID}|{timestamp}. Value: encoded document snapshot, sorted by timestamp.
  4. bykey — key/lang → docID lookup. Key: bykey|{collection}|{key}|{lang}, with the key and language normalised as the docID is (ASCII letters in lower case; the collection as written), so any spelling of a key finds its document.

Every file also records its on-disk format in schema_version; the server migrates an older file before it reads or writes it — see STORAGE-LAYOUT.

Subsystems add their own buckets on top: vectors, vector_meta (vector store), fts_* (full-text inverted index, stop words, synonyms, BM25 stats), geo (R-tree + geohash), webhooks, schemas, auth_users / auth_apikeys / auth_perms / auth_groups, automation, automation_log, binlog (replication), mcp_apikeys, memory_* (RAG sessions), and ttl_*. Each is initialised by its owning module's EnsureBucket() call at startup.

Data Flow #

Add/Update Document #

sequenceDiagram
    participant C as Client
    participant S as Server
    participant DB as BoltDB
    
    C->>S: POST /v1/add
    S->>S: Validate request
    S->>DB: Begin transaction
    S->>DB: Check existing doc
    S->>DB: Update/Create doc
    S->>DB: Update metadata indices
    S->>DB: Create revision
    S->>DB: Update bykey mapping
    S->>DB: Commit transaction
    S->>C: Return document
    S->>S: Trigger hooks (if configured)

Search Documents #

sequenceDiagram
    participant C as Client
    participant S as Server
    participant DB as BoltDB
    
    C->>S: POST /v1/search
    S->>S: Parse filters
    S->>DB: Begin read transaction
    
    alt No filters
        S->>DB: Scan all docs in collection
    else With filters
        S->>DB: Scan metadata indices
        S->>S: Intersect results (AND logic)
        S->>DB: Fetch matching docs
    end
    
    S->>S: Sort results
    S->>S: Apply pagination
    S->>C: Return documents

These four search subsystems each have a dedicated guide — this file does not duplicate the algorithm or API details:

  • SEARCH — full-text search modes, BM25/BM25F/PMISparse scoring, multi-language stemming, fuzzy/proximity, vector index algorithms (Flat, HNSW, IVF, PQ, OPQ, SQ, SQ4, BQ, and auto), quantization
  • PMISPARSE — the BM25 + PPMI two-phase ranker
  • RAG-PIPELINE — hybrid search (alpha blending and Reciprocal Rank Fusion), retrieval-augmented generation patterns
  • GEOSEARCH — R-tree and geohash indexes, radius / bbox queries, composition with FTS and vector
  • EMBEDDING_PROVIDERS — OpenAI / Ollama / Cohere / Voyage configuration

Get Document with Templating #

sequenceDiagram
    participant C as Client
    participant S as Server
    participant DB as BoltDB
    
    C->>S: POST /v1/get (with env)
    S->>DB: Lookup by key+lang
    S->>DB: Fetch document
    S->>S: Apply template substitution
    Note over S: Replace %%var%% with env values
    S->>C: Return processed document

Key Design Decisions #

1. Deterministic IDs #

Documents are identified by: {collection}|{key}|{lang}

Benefits:

  • Predictable IDs
  • Natural deduplication
  • Easy to reason about
  • No need for separate ID generation

2. Metadata as Multi-Value Maps #

Metadata: map[string][]string

Benefits:

  • Flexible schema
  • Multiple values per key (tags, categories)
  • Easy to query and filter
  • Indexed for performance

3. Prefix-Based Indexing #

Index keys: meta|{collection}|{key}|{value}|{docID}

Benefits:

  • Fast prefix scans in BoltDB
  • Efficient range queries
  • No need for secondary indices
  • Automatic sorting

4. Revision History #

Every update creates a new revision with timestamp.

Benefits:

  • Full audit trail
  • Point-in-time recovery
  • Change tracking
  • Can be truncated to save space

5. Embedded Database (BoltDB) #

Benefits:

  • No external dependencies
  • Single file storage
  • ACID transactions
  • Fast local access
  • Easy backup/restore

Trade-offs:

  • Single-writer (not an issue for most use cases)
  • One database file per node (or one file per collection with MDDB_STORAGE_LAYOUT=per-collection)
  • A collection is limited to one machine; node groups spread whole collections, never parts of one (Collection placement)

Access Modes #

Read Mode (read) #

  • Only GET operations allowed
  • Write operations return 403
  • Useful for read replicas

Write Mode (write) #

  • Only write operations allowed
  • Rarely used in practice

Read-Write Mode (wr) #

  • All operations allowed
  • Default and recommended mode

Extension Points #

Webhooks, Automation, Custom MCP tools #

MDDB exposes three layered extension mechanisms — each documented in its own file:

  • Webhooks — HTTP webhooks fired on document events (doc.added, doc.updated, doc.deleted, batch events). Per-collection scoping, retry with backoff, payload signing. See Automations for configuration.
  • AUTOMATIONS — triggers, crons, conditional rules, sentiment analysis, template variables. Configurable via REST/gRPC/MCP.
  • CUSTOM-TOOLS — YAML-defined custom MCP tools that wrap built-in actions (semantic_search, search_documents, full_text_search, fts_languages) with domain-specific defaults so AI agents see purpose-built tools instead of generic primitives.

Performance Characteristics #

Read Performance #

  • Get by key: O(log n) - BoltDB B+tree lookup
  • Search with metadata: O(m * log n) - where m = matching documents
  • Full collection scan: O(n) - linear scan

Write Performance #

  • Add/Update: O(log n + m) - where m = metadata keys
  • Index updates: O(k) - where k = number of metadata values

Storage #

  • Document size: Typically 1-100 KB
  • Metadata overhead: ~100 bytes per key-value pair
  • Revision overhead: Full document copy per revision
  • Index overhead: ~50 bytes per indexed value

Scalability Considerations #

Vertical Scaling #

  • BoltDB performs well with SSDs
  • Memory-map for faster reads
  • Single-writer limitation

Horizontal Scaling #

  • Run multiple read-only instances
  • Single write instance
  • Leader-follower replication: the leader streams its binlog to followers over gRPC (REPLICATION)
  • Leader election among three or more nodes (CLUSTER)
  • Collection placement across node groups (2.19.8+, below): by whole collection, never splitting one

Collection placement #

With MDDB_PLACEMENT_GROUPS the nodes form groups, each a replicated cluster, and every collection belongs as a whole to one group (CLUSTER). The pieces, in the order a request meets them:

  1. Router (placement_route.go, placement_grpc.go). Every REST route is wrapped by it after the access gate, so a request is authenticated and authorised where it arrives. It looks up the group of each collection the request names and serves the request, sends it to that group's leader (only addresses from the configuration), or answers 421 in redirect mode. gRPC unary calls are routed the same way by an interceptor.
  2. Placement map (placement_map.go). Which group holds each collection and which collections are moving, versioned by the meta group's term and a counter. A collection the map does not name belongs to the group a consistent hash of its name gives (consistent_hash.go); group leaders pin the collections they hold.
  3. Meta group (placement_sync.go, placement_global.go). Its leader stores the map (bucket placement) and the global settings (users, keys, tenants, aliases, webhooks, automations, embedding and chat settings); the other groups' leaders copy those settings every few seconds through their own logs.
  4. Entry gate in the log (placement_keys.go). The binlog asks it before it logs a write: a write to a collection the node's group does not hold, or one being switched over by a move, is refused inside the write's transaction. This catches writes from surfaces the router does not see (GraphQL, /es, MCP, uploads).
  5. Fan-out (placement_fanout*.go). Cross-search over several groups and /v1/stats ask each group's leader for its part and merge the answers on the receiving node.
  6. Moves (placement_move.go, placement_import.go, placement_export.go, placement_drop.go). The meta leader copies a collection to another group while it takes writes, fences the source, switches the map once the target has every write, and the old group drops its copy later.

Database Size #

  • Suitable for: 10K - 1M documents
  • Document size: < 1 MB each
  • Total DB size: < 10 GB recommended
  • Regular revision truncation important

Security Model #

This section describes the layers that gate every request reaching the storage engine. Per-feature usage (env vars, config files, recipes) lives in the dedicated guides linked at the bottom — and the version history for each layer lives in CHANGELOG, not here.

Trust boundaries #

A request hitting MDDB passes through up to four trust gates in order; each is independently configurable and may be disabled:

  1. Transport — TCP+TLS, TCP plaintext, or Unix Domain Socket. TLS terminates inside MDDB (buildServerTLSConfig in services/mddbd/tls_config.go). UDS is authenticated by filesystem ownership (0600) instead of TLS.
  2. Peer authentication (optional) — mTLS verifies the client certificate against a configured CA bundle. The server only learns "this peer's cert chains to a trusted CA"; it does not impose identity semantics on the cert subject.
  3. Application authentication — JWT bearer token or API key. Validated in HTTP / gRPC / GraphQL middleware (services/mddbd/auth_middleware.go, auth_grpc.go, graphql_handler.go). On success, claims are written to the request context.
  4. Authorization — every handler / resolver that touches a collection calls AuthManager.CheckPermission(ctx, collection, op), which resolves the caller's effective permissions through both direct user grants and group membership. Operation modes are also gated per protocol (MDDB_MCP_MODE, MDDB_API_MODE, MDDB_GRPC_MODE, MDDB_HTTP3_MODE) — a single deployment can serve read-write to gRPC and read-only to MCP, for example.

The MCP transport adds a fifth gate above its protocol entry point — its own API-key store and per-key rate limiter (services/mddbd/mcp_apikeys.go) — so an MCP client can be issued credentials independently from the main JWT/API-key store.

When AuthManager is nil (auth disabled, the default for an unconfigured deployment) gates 3 and 4 short-circuit to allow-all. mTLS (gate 2) is independent and can be enabled without enabling JWT.

Out of scope for the engine #

Three things MDDB deliberately does not do; they are the operator's responsibility:

  • Encryption at rest — BoltDB writes plaintext to disk. Use filesystem-level encryption (LUKS, FileVault, dm-crypt) or full-volume encryption.
  • Backup blob encryption — /v1/backup produces a plaintext copy of the database file. Encrypt the resulting blob before uploading to remote storage.
  • Public-internet hardening — even with TLS and JWT enabled, prefer to bind to a private interface or a UDS path and front MDDB with a reverse proxy that adds WAF, rate limit, and DDoS protection.

Reference docs #

  • AUTHENTICATION — JWT, API keys, RBAC, group permissions, env vars, recipes
  • TLS — HTTPS + mTLS setup, openssl recipes, deployment patterns, troubleshooting
  • config — UDS transport, env-var reference for MDDB_HTTP_ADDR=unix:...
  • DEPLOYMENT — production hardening checklist
  • CHANGELOG — when each layer landed and how it has evolved
  1. Access Control:
    • Implement collection-level permissions
    • Add user roles
    • Audit logging

Monitoring & Observability #

Metrics to Track #

  • Request rate per endpoint
  • Response times
  • Database size
  • Number of documents
  • Number of revisions
  • Error rates

Logging #

  • Request/response logging
  • Error logging
  • Audit trail for writes
  • Performance logging

Health Checks #

  • Database connectivity
  • Disk space
  • Memory usage
  • Response time