Document updated on Mar 27, 2025
Redis connection pools for stateful functionality
KrakenD’s architecture allows developers to define all Redis connections in a single redis namespace, so you can reuse the definitions in other components across the configuration.
You can configure both connection pools and clusters. Whether you use one option or both, each pool and cluster is identified by a unique name that must not clash (make sure of it), which is then referenced by the components requiring a connection. This approach eliminates the need to repeatedly specify Redis connection details in every component configuration.
For example, a configuration that defines 2 connection pools and 1 cluster could be:
{
  "$schema": "https://www.krakend.io/schema/v2.11/krakend.json",
  "version": 3,
  "extra_config": {
    "redis": {
      "connection_pools": [
        {
          "name": "shared_redis_pool",
          "address": "192.168.1.45:6379"
        },
        {
          "name": "ratelimit_counters",
          "address": "192.168.1.52:6379",
          "db": 2
        }
      ],
      "clusters": [
        {
          "name": "shared_redis_cluster",
          "addresses": ["192.168.1.90:6379", "192.168.1.91:6379"]
        }
      ]
    }
  }
}
The block above creates two Redis connection pools named shared_redis_pool and ratelimit_counters, and it also creates a connection set to a Redis Cluster shared_redis_cluster that utilizes internally two different addresses. These names are how other components (like the Redis-based service rate limit) can select which one to use.
Configuration of Redis connection pools
Different pools can connect to different redis instances, or to the same instances but with different parameters. The configuration allows you to declare extra options to fine tune how the pool should behave: secure connection, retries, etc ..
Best practices to select those parameters will depend on the components that will use the pool.
The full list of options is explained below.
Configuration of Redis Connection Pools
- connection_poolsarray
- Defines all the connetion pools available to Redis functionality. The different components requiring Redis will access the pool based on its nameEach item of connection_pools accepts the following properties:- address* string
- The Redis host where you want to connect using the format host:port.Example:"shared.redis.example.com"
- client_namestring
- You can set how this connection shows in Redis when listing all the connections CONN LISTExample:"krakend_pool"
- conn_max_idle_timestring
- The maximum amount of time a connection may be idle. Should be less than server’s timeout.Expired connections may be closed lazily before reuse. If <= 0, connections are not closed due to a connection’s idle time.Specify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).Defaults to"30m"
- conn_max_life_timestring
- The maximum amount of time a connection may be reused. When the attribute is not declared, the connection does not expireSpecify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).
- dbinteger
- The database number to select after connectingDefaults to0
- dial_timeoutstring
- Dial timeout for establishing new connectionsSpecify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).Defaults to"5s"
- max_active_connsinteger
- Maximum number of connections allocated by the pool at a given time. When zero, there is no limit on the number of connections in the pool.Defaults to0
- max_idle_connsinteger
- Maximum number of idle connections. The value 0means connections not closedDefaults to0
- max_retriesinteger
- The number of times you want to retry a Redis command using a different connection from the pool, applying a random delay between min_retry_backoffandmax_retry_backoff. Use-1to never retry (it might not be a good idea)Defaults to3
- max_retry_backoffstring
- Every retry is executed increasing randomly starting at the min_retry_backoffuntil themax_retry_backoffis reached. This is the longest possible time.Specify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).Defaults to"512ms"
- min_idle_connsinteger
- Minimum number of idle connections which is useful when establishing new connection is slow. 0means connections are not closed.Defaults to0
- min_retry_backoffstring
- Every retry is executed increasing randomly starting at the min_retry_backoffuntil themax_retry_backoffis reached. This is the shortest possible time.Specify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).Defaults to"8ms"
- name* string
- The connection pool name. This is an arbitrary name used to reference this connection in other parts of the configuration needing Redis.Examples:"shared_instance","Redis_for_ratelimit"
- opentelemetryobject
- The OpenTelemetry configuration establishing how to report Redis connection activity- disable_metricsboolean
- Disables any metrics of this Redis poolDefaults tofalse
- disable_tracesboolean
- Disables any traces of this Redis poolDefaults tofalse
- traces_static_attributesarray
- Static attributes you want to pass for traces.
 
- passwordstring
- Sets the password to connect to Redis.
- pool_sizeinteger
- The pool size defines the number of concurrent Redis commands that can be executed by this Redis pool. Take into account that this only saves the connection time to the server, but, Redis will still be a non-concurrent service. When 0it maps to its default 10Defaults to10
- pool_timeoutstring
- Amount of time the client waits for a connection if all connections are busy.Specify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).
- tlsobject
- An object with any client TLS configuration to this connection
- user_namestring
- The username to connect to Redis
 
Configuration of Redis clusters
The configuration of Redis cluster is identical to connection pools, except that the addresses field is an array, that the db is not available, and a new field max_redirects is added.
A simple configuration would look like this:
{
  "$schema": "https://www.krakend.io/schema/v2.11/krakend.json",
  "version": 3,
  "extra_config": {
    "redis": {
      "clusters": [
        {
          "name": "shared_redis_cluster",
          "addresses": ["192.168.1.45:6379", "192.168.1.52:6379"]
        }
      ]
    }
  }
}
The full list of options is:
Configuration of Redis Clusters
- clustersarray
- Defines all the clusters available to Redis functionality. The different components requiring Redis will access the pool based on its nameEach item of clusters accepts the following properties:- addresses* array
- The list of redis members that conform the cluster using the format host:port. All members must be WRITEABLEExample:["192.168.1.45:6379","192.168.1.52:6379"]
- client_namestring
- You can set how this connection shows in Redis when listing all the connections CONN LISTExample:"krakend_pool"
- conn_max_idle_timestring
- The maximum amount of time a connection may be idle. Should be less than server’s timeout.Expired connections may be closed lazily before reuse. If <= 0, connections are not closed due to a connection’s idle time.Specify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).Defaults to"30m"
- conn_max_life_timestring
- The maximum amount of time a connection may be reused. When the attribute is not declared, the connection does not expireSpecify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).
- dial_timeoutstring
- Dial timeout for establishing new connectionsSpecify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).Defaults to"5s"
- max_active_connsinteger
- Maximum number of connections allocated by the pool at a given time. When zero, there is no limit on the number of connections in the pool.Defaults to0
- max_idle_connsinteger
- Maximum number of idle connections. The value 0means connections not closedDefaults to0
- max_redirectsinteger
- The maximum number of redirects to follow when requesting a key that is in another instanceDefaults to3
- max_retriesinteger
- The number of times you want to retry a Redis command using a different connection from the pool, applying a random delay between min_retry_backoffandmax_retry_backoff. Use-1to never retry (it might not be a good idea)Defaults to3
- max_retry_backoffstring
- Every retry is executed increasing randomly starting at the min_retry_backoffuntil themax_retry_backoffis reached. This is the longest possible time.Specify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).Defaults to"512ms"
- min_idle_connsinteger
- Minimum number of idle connections which is useful when establishing new connection is slow. 0means connections are not closed.Defaults to0
- min_retry_backoffstring
- Every retry is executed increasing randomly starting at the min_retry_backoffuntil themax_retry_backoffis reached. This is the shortest possible time.Specify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).Defaults to"8ms"
- name* string
- The connection pool name. This is an arbitrary name used to reference this connection in other parts of the configuration needing Redis.Examples:"shared_instance","Redis_for_ratelimit"
- opentelemetryobject
- The OpenTelemetry configuration establishing how to report Redis connection activity- disable_metricsboolean
- Disables any metrics of this Redis poolDefaults tofalse
- disable_tracesboolean
- Disables any traces of this Redis poolDefaults tofalse
- traces_static_attributesarray
- Static attributes you want to pass for traces.
 
- passwordstring
- Sets the password to connect to Redis.
- pool_sizeinteger
- The pool size defines the number of concurrent Redis commands that can be executed by this Redis pool. Take into account that this only saves the connection time to the server, but, Redis will still be a non-concurrent service. When 0it maps to its default 10Defaults to10
- pool_timeoutstring
- Amount of time the client waits for a connection if all connections are busy.Specify units usingns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours).
- tlsobject
- An object with any client TLS configuration to this connection
- user_namestring
- The username to connect to Redis
 
Best practices for Redis connection management
While the library sets defaults for all the options (except the address and the name), you might want to tweak several of the parameters available. Generally speaking, the additional parameters are not needed. A few things to have in mind when configuring pools and clusters:
- Set realistic timeouts and limits: Use appropriate values for timeouts and connection limits to prevent overloading Redis and ensure smooth operations during peak traffic.
- Don’t disable monitoring: Let OpenTelemetry to track metrics and traces, helping you identify and address potential bottlenecks. Add the Redis Grafana dashboard for greater visibility.
- Align pools with functionality: Use separate pools for different functionalities to isolate workloads and ensure predictable performance.

