Documentation Index
Fetch the complete documentation index at: https://mintlify.com/backtest-kit/backtest-kit-redis-mongo-docker/llms.txt
Use this file to discover all available pages before exploring further.
RedisService is the infrastructure service that owns the ioredis connection lifecycle. Like its Mongoose counterpart, it is a singleton in the IoC container and exposes waitForInit() so that all CacheService classes can wait for a healthy Redis connection before performing cache operations. The underlying ioredis client is managed through the getRedis() singleshot factory in src/config/redis.ts, which is shared between RedisService and BaseMap.
Implementation
src/lib/services/base/RedisService.ts
src/config/redis.ts
Constructor
RedisService takes no constructor arguments. It is instantiated by the IoC container and receives loggerService via property injection:
ioc.redisService; never construct it directly.
Methods
waitForInit(): Promise<Redis>
Establishes the ioredis connection to Redis and resolves to the connected Redis instance. Safe to call multiple times — the singleshot wrapper ensures the handshake occurs exactly once and all concurrent callers await the same promise.
The method checks redis.status === 'ready' immediately; if Redis is already connected it returns right away. Otherwise it races the ready event against a 15-second timeout. On timeout it clears the singleshot gate (so a future call can retry) and throws Error("Redis connection timeout").
getRedis(): Promise<Redis> (config-level singleton)
getRedis is exported from src/config/redis.ts and is the function BaseMap calls to retrieve the shared ioredis client. It is a singleshot factory — the Redis instance is constructed once and returned to every caller thereafter.
The factory also installs:
- A 30-second keepalive
PINGinterval to prevent idle connection timeouts. - A
SIGINThandler that callsredis.disconnect(false)for a clean shutdown without flushing pending commands.
Configuration
The ioredis client is constructed from four environment variables defined insrc/config/params.ts:
| Variable | Type | Default |
|---|---|---|
CC_REDIS_HOST | string | 127.0.0.1 |
CC_REDIS_PORT | number | 6379 |
CC_REDIS_USER | string | default |
CC_REDIS_PASSWORD | string | mysecurepassword |
Connection Lifecycle
| Stage | Description |
|---|---|
First waitForInit() call | getRedis() constructs the Redis instance; ioredis begins connecting |
redis.status === 'ready' | Already connected — resolves immediately |
ready event fires | Connection confirmed — promise resolves |
| 15 s timeout reached | waitForInit.clear() resets; throws "Redis connection timeout" |
SIGINT | redis.disconnect(false) closes gracefully |
| Every 30 s | redis.ping() keepalive fired to prevent idle disconnects |
Integration with waitForInfra()
RedisService.waitForInit() is always called alongside MongoService.waitForInit() in the shared waitForInfra() singleshot defined in src/config/setup.ts:
waitForInfra() so that both infrastructure layers are ready before any data operation begins.