The backtest-kit-redis-mongo-docker integration replaces backtest-kit’s default file-based persistence with a production-grade dual-store system. MongoDB acts as the source of truth for every write, while Redis holds a compact O(1) ID cache that short-circuits the most frequent read operations. Thirty-three IoC-wired services—grouped into base infrastructure, 15 cache services, and 15 DB services—collaborate behind a clean adapter interface so that trading strategies remain completely unaware of the underlying persistence mechanism.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/theonetrade/backtest-kit-redis-mongo-docker/llms.txt
Use this file to discover all available pages before exploring further.
Three-Layer Architecture
The system is organized into three distinct layers that sit between backtest-kit’s abstract adapter contracts and the physical data stores.Layer 1 — Adapter Interface
backtest-kit definesIPersist* interfaces (e.g., IPersistSignalInstance, IPersistRiskInstance) with two core async methods per domain: readXData() and writeXData(). Each concrete adapter class registered via PersistXAdapter.usePersistXAdapter(...) is instantiated per trading context (symbol, strategy, exchange). Adapters delegate every I/O operation to the IoC service layer—they contain no direct database calls.
Layer 2 — IoC Service Layer
The centralioc container exposes exactly 33 services:
BaseCRUD (Mongoose model operations) and each cache service extends BaseMap (Redis string key/value operations). Both base classes are produced by a dependency-injection factory so they receive the logger and connection clients automatically.
Layer 3 — Infrastructure
Three Docker-managed services provide the runtime:| Service | Image | Role |
|---|---|---|
| MongoDB | mongo:8.0.4 | Durable document store, compound indexes |
| Redis | redis:7.4.1 | In-memory ID cache, SCAN-based iteration |
| Node.js | node:22-alpine | Strategy runner, IoC container host |
Data Flow
Write Path
Every write goes to MongoDB first, then synchronously updates Redis before the adapter method returns. This guarantees that the next read—from any code path—sees the freshly written value.Read Path
Reads attempt the Redis cache first. A cache hit resolves to a MongoDBfindById lookup by _id—an O(log n) index seek on the primary key. Only on a cache miss does the system fall back to a compound-field query, after which it immediately backfills the cache.
Infrastructure Initialization Gate
Before any adapter performs its first I/O, asingleshot gate ensures both MongoDB and Redis connections are fully established—regardless of how many adapter instances are constructed concurrently:
singleshot (from functools-kit) guarantees the inner function executes exactly once, caching the resulting Promise so every concurrent caller awaits the same initialization sequence.
Explore Further
Persistence Layer
Deep dive into the 15 MongoDB adapter implementations, BaseCRUD methods, and the upsert pattern.
Redis Cache
How BaseMap namespaces keys, manages TTL, and provides O(1) ID lookups for all 15 cache services.
Atomicity
Read-after-write guarantees, look-ahead bias protection, and the
when: Date timestamp contract.