Redis
Before configuring the Redis driver, please make sure that the Redis Server is installed and running.
You can read more about this in the documentation.
Configuration
In the simplest case, when a full-fledged cluster or a fault-tolerant system is not required, we have one connection to the Redis Server.
The complete redis driver configuration:
Options
Below is a more detailed description of each of the Redis-specific options:
Addresses
addrs
: An array of strings of connections to the Redis Server. Must contain at least one value of an existing connection in the format of host or IP address and port, separated by a colon (:
) character.
Username and Password
username
: Optional value containing the username credentials of the Redis connection. You can omit this field, or specify an empty string if the username of the connection is not specified.
password
: Optional value containing the password credentials of the Redis connection. You can omit this field, or specify an empty string if the password of the connection is not specified.
DB
db
: An optional identifier for the database used in this connection to the Redis Server.
Read more about databases section on the documentation page for the description of the select command.
Timeouts
dial_timeout
: Server connection timeout. A value of 0
is equivalent to a timeout of 5 seconds (5s
). After the specified time has elapsed, if the connection has not been established, a connection error will occur.
Must be in the format of a "numeric value" + "time format suffix", like "2h
" where suffixes means:
h
- the number of hours. For example1h
means 1 hour.m
- the number of minutes. For example2m
means 2 minutes.s
- the number of seconds. For example3s
means 3 seconds.ms
- the number of milliseconds. For example4ms
means 4 milliseconds.
If no suffix is specified, the value will be interpreted as specified in nanoseconds. In most cases, this accuracy is redundant and may not be true. For example 5
means 5 nanoseconds.
All time intervals can be suffixed.
read_timeout
: Timeout for socket reads. If reached, commands will fail with a timeout instead of blocking. Must be in the format of a "numeric value" + "time format suffix". A value of 0
is equivalent to a timeout of 3 seconds (3s
). A value of -1
disables timeout.
write_timeout
: Timeout for socket writes. If reached, commands will fail with a timeout instead of blocking. A value of 0
is equivalent of the value specified in the read_timeout
section. If read_timeout
value is not specified, a value of 3 seconds (3s
) will be used.
pool_timeout
: Amount of time client waits for connection if all connections are busy before returning an error. A value of 0
is equivalent of the value specified in the read_timeout
+ 1s
. If read_timeout
value is not specified, a value of 4 seconds (4s
) will be used.
idle_timeout
: Amount of time after which client closes idle connections. Must be in the format of a "numeric value" + "time format suffix". A value of 0
is equivalent to a timeout of 5 minutes (5m
). A value of -1
disables idle timeout check.
Retries
max_retries
: Maximum number of retries before giving up. Specifying 0
is equivalent to the default (3
attempts). If you need to specify an infinite number of connection attempts, specify the value -1
.
min_retry_backoff
: Minimum backoff between each retry. Must be in the format of a "numeric value" + "time format suffix". A value of 0
is equivalent to a timeout of 8 milliseconds (8ms
). A value of -1
disables backoff.
max_retry_backoff
: Maximum backoff between each retry. Must be in the format of a "numeric value" + "time format suffix". A value of 0
is equivalent to a timeout of 512 milliseconds (512ms
). A value of -1
disables backoff.
Pool Size
pool_size
: Maximum number of RoadRunner socket connections. A value of 0
is equivalent to a 10
connections per every CPU. Please note that specifying the value corresponds to the number of connections per core, so if you have 8 cores in your system, then setting the option to 2 you will get 16 connections.
TLS Configuration
The tls
section allows you to configure Transport Layer Security (TLS) for secure communication with the Redis server. If no options are defined in this section, the connection will default to non-TLS.
cert
: Path to a file containing the client certificate. This certificate is used to authenticate the client when communicating with the server.
key
: Path to a file containing the client private key. This key is used in conjunction with the client certificate for mutual authentication.
root_ca
: Path to a file containing the Certificate Authority (CA) certificates used to verify the server's certificate.
Note: This option can be used independently of the cert
and key
options. In cases where the server does not require client certificate verification, you only need to provide the root_ca
option.
Other
min_idle_conns
: Minimum number of idle connections which is useful when establishing new connection is slow. A value of 0 means no such idle connections. More details about the problem requiring the presence of this option available in the corresponding issue.
max_conn_age
: Connection age at which client retires (closes) the connection. A value of 0
is equivalent to a disabling this option. In this case, aged connections will not be closed.
idle_check_freq
: Frequency of idle checks made by idle connections reaper. Must be in the format of a "numeric value" + "time format suffix". A value of0
is equivalent to a timeout of 1 minute (1m
). A value of -1
disables idle connections' reaper. Note, that idle connections are still discarded by the client if idle_timeout
is set.
read_only
: An optional boolean value that enables or disables read-only mode. If true
value is specified, the writing will be unavailable. Note that this option not allowed when working with Redis Sentinel.
These are all options available for all Redis connection types.
Redis Cluster
In the case that you want to configure a Redis Cluster, then you can specify additional options required only if you are organizing this type of server.
When creating a cluster, multiple connections are available to you. For example, you call such a command redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380
, you should specify the appropriate set of connections. In addition, when organizing a cluster, two additional options with algorithms for working with connections will be available to you: route_by_latency
and route_randomly
.
Where new options means:
route_by_latency
: Allows routing read-only commands to the closest master or slave node. If this option is specified, theread_only
configuration value will be automatically set totrue
.route_randomly
: Allows routing read-only commands to the random master or slave node. If this option is specified, theread_only
configuration value will be automatically set totrue
.
Redis Sentinel
Redis Sentinel provides high availability for Redis. You can find more information about Sentinel on the documentation page.
There are two additional options available for the Sentinel configuration: master_name
and sentinel_password
.
Where Sentinel's options means:
master_name
: The name of the Sentinel's master in string format.sentinel_password
: Sentinel password from "requirepasspassword
" (if enabled) in Sentinel configuration.
Last updated