Updated February 17, 2023
Introduction to Redis Timeout
Redis timeout can occur for a variety of reasons, including conditions caused by either the client or the server. Redis will accept client connections on the port that we have configured and using the UNIX socket that we have enabled. When a new client connection is accepted by the server, if the client encounters a problem, it will throw a timeout error.
- Timeout in redis will occur in multiple ways. It will occur on the client as well as the server side.
- If suppose the redis timeout exception contains the value as a worker or busy then we need to set the thread limit value as min, we can change the thread limit value.
What is Redis Timeout?
Because redis is a key value database, it will work on mapping the specific string key that was typing the particular value of key. If we create a key in Redis, it will exist in Redis unless we remove it manually. Redis will allow us to specify a time limit for the specified key. We can tell Redis to create a key and delete it after a certain amount of time by using this functionality. The volatility key is useful when we need to temporarily store a value.
To create a redis with an expiration time, we must use the set command and the ex option to specify the expiration time. When we set the expiration time, the session key will time out at the time we specified. We are using the ex option, which takes a number in seconds and sets the number of seconds until the specified key expires.
Why Redis Timeout Occur?
There are multiple causes why timeout issues will occur. Below are the causes of why redis timeout will occur.
1. Bandwidth size limits
We need to monitor the server metrics for checking whether we have reached bandwidth size limits or not. To tackle this issue we need to upgrade the pricing tier of the redis server. We are checking iftop command to check the bandwidth as follows.
2. High CPU, bandwidth, or memory usage
The high memory and CPU usage will cause the request which was not processed within the specified time intervals and it will cause the request timeout. To solve this issue we need to execute the top command and need to check which process is consuming more CPU and memory.
3. Client and server application is not in the same region in azure
Azure support is recommended that the client and server are needed to set in the same region.
4. Too many requests
If redis timeout exception contains the busy for worker then we need to change the maxclients parameter in a redis.conf file. We can check the max connection limit by using the following command as follows.
config get maxclients
5. TCP keep alive timeout
The main cause of client timeout is the value of TCP keep alive which we have set into the redis.conf file. The below example shows the TCP keep alive timeout.
Redis Timeout Error
If we are using the redis server for maintaining the state management of the web application and we are putting the configuration into the web.config file, then we are getting the timeout error from redis server.
The redis timeout error looks as below:
To tackle this situation from the server side we need to check the parameter value of the timeout parameter. We can see that we have set the timeout parameter value as 0. The default timeout value in redis is 0. We can change the same as per our requirement. The below example shows the timeout.
config get timeout
In the below example, we are checking the value of the TCP keep alive parameter. To tackle this situation we also need to change the value of this parameter.
config get tcp-keepalive
To troubleshoot this issue we need to follow the below steps as follows. This issue troubleshooting is not easy.
- Need to increase the timeout parameter as per error.
- Need to check whether the error was returned after the error is returned or operation was performed on the key which was big.
- Redis will limit the size of every string value. We need to keep the size as 10 KB otherwise CPU is heavily overloaded.
- Need to increase the value of PingConnectionInterval parameter.
Redis Client Timeouts
We are checking the client timeout by using the following command.
There are multiple ways for checking the client timeout. The first method will specify the number of clients which was starting from the redis server. The below example shows to check the redis client timeout as follows, also we are setting the max client limit as 1500 as follows.
redis-server --maxclients 1500
We can also set the timeout by using redis-cli. The below example shows how we can set the timeout by using redis cli.
config set maxclients 2500
config get maxclients
We can check the connected client by using the following command. Client list command is used to check the connected client.
Examples of Redis Timeout
In the below example, we are using ex arguments to set the timeout of the key as follows.
SET key_timeout key_val EX 30
In the above example, we have defined timeout as 30 sec by using an ex argument with the set method. In the below example we are using the set method to define the key and value, after that we are using expire command to expire the key at a specified timeout, below we are defining the key expiration timeout as 15 sec as follows.
SET key_timeout key_val
EXPIRE key_timeout 15
We can set the redis timeout by using UNIX time. The below example shows how we can set the unit timeout as follows.
SET key_timeout key_val
EXPIRE key_timeout 1640984400
Given below are the FAQs mentioned:
Q1. How can we set timeout in redis?
Answer: For creating redis with expiration time we need to use the set command and also need to use the EX argument with set command, ex will take a number of seconds for the key to be valid.
Q2. What is the cause of redis timeout?
Answer: Redis will hit timeout either in IOCP threads or either in worker threads. There are multiple reasons for redis timeout.
Q3. Can we set TTL in redis timeout?
Answer: Basically redis TTL command is used to get the remaining time of key that we have created. We can use TTL in IT to check the remaining time of key.
Redis will allow us to set the limited lifetime for the specified keys. By using this functionality we can tell to redis that create a key and delete the same after a specified number of times. It will occur for multiple reasons, it will occur in various conditions which were caused by either client or the server.
This is a guide to Redis Timeout. Here we discuss the introduction, why redis timeout occur, error and examples respectively. You may also have a look at the following articles to learn more –