php – Connection timed out [tcp://127.0.0.1:6379] using redis on linux server-ThrowExceptions

Exception or error:

I have an Lumen API in my server, it was working fine. Actually this API will be calling every seconds from another server and update stock rates and store into redis. In this api I’m comparing the received data(request body) and stored data from redis and update the difference rate of current and previous values as response and also I will send current date time in redis to show last rate update time of stock rates.
I have the following code in my lumen api,

use Illuminate\Support\Facades\Redis;
public function updatebaserate(Request $request)
{
        $request_data = array();
        parse_str($request, $request_data);
        if (app('redis')->get("BaseRates")) {
            if(json_decode(app('redis')->get("BaseRates"),true) === json_decode($request->getContent(), true)){
                $this->createrates();
                return app('redis')->get("BaseRatesUpdated");
            }else{
                $receivedrates = json_decode($request->getContent(), true);
                $rate_valid = true;
                if($rate_valid){
                    app('redis')->set("BaseRatesUpdated", Carbon::createFromFormat('d-m-Y H:i:s', date('d-m-Y H:i:s')));
                    app('redis')->set("BaseRates", $request->getContent());
                }
            }
        } else {
            app('redis')->set("BaseRates", $request->getContent());
            app('redis')->set("BaseRatesUpdated", Carbon::createFromFormat('d-m-Y H:i:s', date('d-m-Y H:i:s')));
        }
        $this->createrates();
        return app('redis')->get("BaseRatesUpdated");
}

This get and set of redis operation working continuously for every second 2 api request will call from another server. Whether it will cause any issue.
In my lumen .env settings as

BROADCAST_DRIVER=redis

CACHE_DRIVER=redis
QUEUE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PORT=6379

In server redis info showing following details,

# Server
redis_version:3.2.10
tcp_port:6379

# Clients
connected_clients:13
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:1802320
used_memory_human:1.72M
used_memory_rss:2387968
used_memory_rss_human:2.28M
used_memory_peak:2200392
used_memory_peak_human:2.10M
total_system_memory:4026261504
total_system_memory_human:3.75G
used_memory_lua:39936
used_memory_lua_human:39.00K
maxmemory:0

# Stats
total_connections_received:302107
total_commands_processed:30017647
instantaneous_ops_per_sec:302
total_net_input_bytes:6532076041
total_net_output_bytes:67794026721
instantaneous_input_kbps:59.68
instantaneous_output_kbps:642.24
rejected_connections:0

Is there any issue on continuously read and write keys in redis for every second. And also I have set redis timeout value

127.0.0.1:6379> config get timeout
1) "timeout"
2) "100"

Is there any way to open and close connection in lumen api for redis. I have often receiving the error in error log like

[2020-03-17 17:17:29] production.ERROR: Predis\Connection\ConnectionException: Connection timed out [tcp://127.0.0.1:6379] in /home/public_html/winapi/vendor/predis/predis/src/Connection/AbstractConnection.php:155
Stack trace:
#0 /home/public_html/winapi/vendor/predis/predis/src/Connection/StreamConnection.php(128): Predis\Connection\AbstractConnection->onConnectionError('Connection time...', 110)
#1 /home/public_html/winapi/vendor/predis/predis/src/Connection/StreamConnection.php(178): Predis\Connection\StreamConnection->createStreamSocket(Object(Predis\Connection\Parameters), 'tcp://127.0.0.1...', 4)
#2 /home/public_html/winapi/vendor/predis/predis/src/Connection/StreamConnection.php(100): Predis\Connection\StreamConnection->tcpStreamInitializer(Object(Predis\Connection\Parameters))

Please help any one to resolve the issue.

How to solve:

Please try by changing the Redis timeout value in /etc/redis/redis.conf to some non-zero value. This can resolve your problem.

Leave a Reply

Your email address will not be published. Required fields are marked *