Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(redis): enhance Redis connection handling with logging and error management

Victor Hugo dos Santosec39e9f3ce2a
diff --git a/backend/src/lib/config/redis.ts b/backend/src/lib/config/redis.ts
index df787a7c95..c37ee0bc96 100644
--- a/backend/src/lib/config/redis.ts
+++ b/backend/src/lib/config/redis.ts
@@ -1,4 +1,6 @@
-import { Redis } from "ioredis";
+import { Cluster, Redis } from "ioredis";
+
+import { logger } from "@app/lib/logger";
export type TRedisConfigKeys = Partial<{
REDIS_URL: string;
@@ -17,59 +19,91 @@ export type TRedisConfigKeys = Partial<{
REDIS_SENTINEL_PASSWORD: string;
}>;
+const REDIS_ERROR_LOG_THROTTLE_MS = 10_000;
+
+const attachConnectionLogging = (client: Redis | Cluster) => {
+ let lastLoggedAt = 0;
+ let isDown = false;
+
+ client.on("error", (err: Error) => {
+ const now = Date.now();
+ if (now - lastLoggedAt < REDIS_ERROR_LOG_THROTTLE_MS) return;
+
+ lastLoggedAt = now;
+ isDown = true;
+ logger.error({ err }, `Redis connection error [error=${err.message}]`);
+ });
+
+ client.on("ready", () => {
+ // Skip the initial connect; only a recovery is worth a line.
+ if (!isDown) return;
+ isDown = false;
+ lastLoggedAt = 0;
+ logger.info("Redis connection restored");
+ });
+
+ return client;
+};
+
export const buildRedisFromConfig = (cfg: TRedisConfigKeys) => {
if (cfg.REDIS_URL) {
- return new Redis(cfg.REDIS_URL, {
- maxRetriesPerRequest: null,
- reconnectOnError(err) {
- // Reconnect when hitting a read-only replica during failover
- const targetError = "READONLY";
- if (err.message.includes(targetError)) {
- return 2; // Reconnect and resend command
- }
- return false;
- }
- });
- }
-
- if (cfg.REDIS_CLUSTER_HOSTS) {
- return new Redis.Cluster(cfg.REDIS_CLUSTER_HOSTS, {
- dnsLookup: cfg.REDIS_CLUSTER_AWS_ELASTICACHE_DNS_LOOKUP_MODE
- ? (address, callback) => callback(null, address)
- : undefined,
- retryDelayOnClusterDown: 300,
- redisOptions: {
- username: cfg.REDIS_USERNAME,
- password: cfg.REDIS_PASSWORD,
- tls: cfg?.REDIS_CLUSTER_ENABLE_TLS ? {} : undefined,
+ return attachConnectionLogging(
+ new Redis(cfg.REDIS_URL, {
+ maxRetriesPerRequest: null,
reconnectOnError(err) {
+ // Reconnect when hitting a read-only replica during failover
const targetError = "READONLY";
if (err.message.includes(targetError)) {
return 2; // Reconnect and resend command
}
return false;
}
- }
- });
+ })
+ );
+ }
+
+ if (cfg.REDIS_CLUSTER_HOSTS) {
+ return attachConnectionLogging(
+ new Redis.Cluster(cfg.REDIS_CLUSTER_HOSTS, {
+ dnsLookup: cfg.REDIS_CLUSTER_AWS_ELASTICACHE_DNS_LOOKUP_MODE
+ ? (address, callback) => callback(null, address)
+ : undefined,
+ retryDelayOnClusterDown: 300,
+ redisOptions: {
+ username: cfg.REDIS_USERNAME,
+ password: cfg.REDIS_PASSWORD,
+ tls: cfg?.REDIS_CLUSTER_ENABLE_TLS ? {} : undefined,
+ reconnectOnError(err) {
+ const targetError = "READONLY";
+ if (err.message.includes(targetError)) {
+ return 2; // Reconnect and resend command
+ }
+ return false;
+ }
+ }
+ })
+ );
}
- return new Redis({
- // refine at tope will catch this case
- sentinels: cfg.REDIS_SENTINEL_HOSTS!,
- name: cfg.REDIS_SENTINEL_MASTER_NAME!,
- maxRetriesPerRequest: null,
- sentinelUsername: cfg.REDIS_SENTINEL_USERNAME,
- sentinelPassword: cfg.REDIS_SENTINEL_PASSWORD,
- enableTLSForSentinelMode: cfg.REDIS_SENTINEL_ENABLE_TLS,
- username: cfg.REDIS_USERNAME,
- password: cfg.REDIS_PASSWORD,
- reconnectOnError(err) {
- // Reconnect when hitting a read-only replica during failover
- const targetError = "READONLY";
- if (err.message.includes(targetError)) {
- return 2; // Reconnect and resend command
+ return attachConnectionLogging(
+ new Redis({
+ // refine at tope will catch this case
+ sentinels: cfg.REDIS_SENTINEL_HOSTS!,
+ name: cfg.REDIS_SENTINEL_MASTER_NAME!,
+ maxRetriesPerRequest: null,
+ sentinelUsername: cfg.REDIS_SENTINEL_USERNAME,
+ sentinelPassword: cfg.REDIS_SENTINEL_PASSWORD,
+ enableTLSForSentinelMode: cfg.REDIS_SENTINEL_ENABLE_TLS,
+ username: cfg.REDIS_USERNAME,
+ password: cfg.REDIS_PASSWORD,
+ reconnectOnError(err) {
+ // Reconnect when hitting a read-only replica during failover
+ const targetError = "READONLY";
+ if (err.message.includes(targetError)) {
+ return 2; // Reconnect and resend command
+ }
+ return false;
}
- return false;
- }
- });
+ })
+ );
};
diff --git a/docs/self-hosting/configuration/envars.mdx b/docs/self-hosting/configuration/envars.mdx
index adfd0c011c..bd8b4e7d58 100644
--- a/docs/self-hosting/configuration/envars.mdx
+++ b/docs/self-hosting/configuration/envars.mdx
@@ -304,6 +304,10 @@ ClickHouse can be used as an alternative audit log storage backend for high-volu
Redis is used for caching and background tasks. You can use either a standalone Redis instance, Redis Sentinel, or Redis Cluster setup.
+Redis is required: the instance will not start unless one of `REDIS_URL`, `REDIS_SENTINEL_HOSTS`, or
+`REDIS_CLUSTER_HOSTS` is set. For what happens when a configured Redis becomes unreachable at
+runtime, see [Behavior when Redis is unavailable](/self-hosting/configuration/requirements#behavior-when-redis-is-unavailable).
+
<Info>
An **active-passive** setup is recommended for Redis. Infisical has not been tested with an **active-active** Redis setup, which may result in undocumented behavior.
</Info>
diff --git a/docs/self-hosting/configuration/requirements.mdx b/docs/self-hosting/configuration/requirements.mdx
index 45681b4d90..eb77434a82 100644
--- a/docs/self-hosting/configuration/requirements.mdx
+++ b/docs/self-hosting/configuration/requirements.mdx
@@ -53,7 +53,10 @@ Recommended resource allocation based on deployment size. You may require more r
### Redis
-Redis is utilized for session management and background tasks in Infisical.
+Infisical treats Redis as a **required, persistent datastore**. Beyond
+caching, it holds the background job queue, distributed locks, cross-instance coordination state
+for scheduled jobs, and rate-limit counters. An instance refuses to start unless a Redis
+connection is configured, and a running instance is degraded for as long as Redis is unreachable.
<Info>
**Active-passive** setup is recommended for Redis. **Active-active** setup for Redis has not been tested.
@@ -62,9 +65,19 @@ Redis is utilized for session management and background tasks in Infisical.
Redis requirements:
- Use Redis versions 6.x or 7.x. We advise upgrading to at least Redis 6.2.
-- Redis Cluster mode is currently not supported; use Redis Standalone, with or without High Availability (HA).
+- Redis Standalone (with or without High Availability), Redis Sentinel, and Redis Cluster are all
+ supported. See [Redis configuration](/self-hosting/configuration/envars#redis) for the
+ environment variables each mode expects.
- Redis storage needs are minimal: a setup with 2 vCPU, 4 GB RAM, and 30GB SSD will be sufficient for small deployments.
-- Set cache eviction policy to `noeviction`.
+- Set cache eviction policy to `noeviction`. Because Redis holds queue and coordination state,
+ evicting keys under memory pressure would silently drop work. This setting is required, not a
+ tuning suggestion.
+- Enable persistence (AOF, or RDB snapshots at minimum) and include Redis in your backup strategy.
+ Pending background jobs (secret rotations, syncs, webhook deliveries) live in Redis; a Redis
+ that comes back empty loses them.
+- Because Redis is a hard dependency, give it the same availability target as the Infisical
+ instances themselves. A single unreplicated Redis is a single point of failure for the whole
+ deployment.
## Supported Web Browsers