Document updated on Dec 12, 2024
Redis connection pools for stateful functionality
KrakenD’s architecture allows developers to define all Redis connection pools in a single redis namespace. Each pool is identified by a unique name (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 Redis connection pool configuration might look like this:
{
"$schema": "https://www.krakend.io/schema/v2.8/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",
"database": 2
}
]
}
}
}
The block above creates two Redis connection pools named shared_redis_pool and ratelimit_counters. These names are how other components (like the Redis-based service rate limit) can select which one to use.
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.
Configuration of Redis connection pools
The full list of options is explained below.
Fields of Redis configuration
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 using
ns(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 using
ns(nanoseconds),usorµs(microseconds),ms(milliseconds),s(seconds),m(minutes), orh(hours). dbinteger- The database number to select after connectingDefaults to
0 dial_timeoutstring- Dial timeout for establishing new connectionsSpecify units using
ns(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 to
0 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 to
false disable_tracesboolean- Disables any traces of this Redis poolDefaults to
false 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 using
ns(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 pool 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:
- 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.
