MongoDB serves as the durable persistence layer for all live-mode subsystems inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/backtest-kit/backtest-monorepo-parallel/llms.txt
Use this file to discover all available pages before exploring further.
backtest-monorepo-parallel. When running in live mode, six subsystems — Session, Storage, Recent, Notification, Memory, and State — write their data through @backtest-kit/mongo persist adapters backed by MongoDB. In backtest mode, those same subsystems either switch to local-file adapters or in-memory stores, meaning MongoDB is never touched during replay and replay speed stays at ~6,300× real time.
Docker Setup
The project ships a ready-to-use Compose file. Start MongoDB with a single command:docker/mongodb/docker-compose.yaml
./mongo_data so data survives container restarts. The restart: always policy means MongoDB comes back automatically after a system reboot.
Connection String
The connection string is configured via theCC_MONGO_CONNECTION_STRING environment variable, resolved in packages/core/src/config/params.ts:
packages/core/src/config/params.ts
The
wtimeoutMS=15000 query parameter sets a 15-second write-concern timeout. If a write acknowledgement is not received within 15 seconds — for example because the replica set primary is temporarily unavailable — the driver raises a timeout error rather than waiting indefinitely. For local single-node development this is a generous upper bound; tighten it for production deployments with reliable networks.Atomic Upserts with BaseCRUD
All database services in @pro/core extend BaseCRUD, which wraps a Mongoose Model<any> and provides create, update, findById, findByFilter, findAll, iterate, and paginate out of the box:
packages/core/src/lib/common/BaseCRUD.ts
backtest-kit template use the pattern:
E11000 duplicate-key retry loop even when nine symbols write concurrently.
CandleModel Compound Unique Index
The Candle schema demonstrates the compound index pattern that enables idempotent candle upserts across all supported intervals:
packages/core/src/schema/Candle.schema.ts
{ symbol: 1, interval: 1, timestamp: 1 } unique index is the upsert filter. Calling findOneAndUpdate against this index guarantees exactly-once candle storage: re-fetching candles (for example on --cache re-runs or live resyncs) updates the existing document rather than inserting a duplicate.
Live vs. Backtest Adapter Matrix
config/setup.config.ts wires each subsystem to the correct adapter for its execution context. The table below summarises the full adapter map:
| Subsystem | Live Mode | Backtest Mode |
|---|---|---|
| Session | Persist (MongoDB via @backtest-kit/mongo) | Local file |
| Storage | Persist (MongoDB) | In-memory |
| Recent | Persist (MongoDB) | In-memory |
| Notification | Persist (MongoDB) | In-memory |
| Memory | Persist (MongoDB) | Local file |
| State | Persist (MongoDB) | Local file |
| Dump | Markdown | Markdown |
| Markdown | Dummy (no-op) | Dummy (no-op) |
| Log | JSONL | JSONL |
config/setup.config.ts:
config/setup.config.ts
Adding a New MongoDB Collection
Define the schema
Create
packages/core/src/schema/<Name>.schema.ts following the Candle.schema.ts pattern. Add a compound unique index whose shape matches your context key.Wrap with BaseCRUD
Create a
<Name>DbService that extends BaseCRUD(NameModel). This gives you create, update, findById, findByFilter, findAll, iterate, and paginate for free.Use atomic upserts for writes
For any write that can be replayed, use
findOneAndUpdate(filter, { $set }, { upsert: true, new: true, setDefaultsOnInsert: true }) to match the atomicity contract of the built-in persist adapters.