I am experiencing unexpected latency issues with Redis in my Laravel application. Here are the details:
Setup:
Redis Client: I am using the phpredis extension.
Environment: Redis is running on Windows Subsystem for Linux (WSL).
Issue:
When I measure the time for the first Redis connection, it takes around 27.18 milliseconds.
Subsequent connection times are significantly lower, around 0.19 milliseconds, which seems normal.
However, when I run the same code to measure latency multiple times within the same second, I observe that the timing for each request remains consistently high, even though the connection should already be established.
Additional Context:
Redis Insight shows a process time of 8.629 milliseconds, which is lower than the latency observed in my application.
this my code :
Route::get('redis/{website}', function ($website) {
$firstconnectionStart = microtime(true);
$website_id = Redis::hget("websites", $website);
$firstconnectionEnd = microtime(true);
if (empty($website_id)) {
$website_id = DB::select("SELECT `id` FROM `websites` where `domain` = '$website' ")[0]->id;
Redis::hset("websites", $website, $website_id);
}
//categoriesTree handling
$start = microtime(true);
$categoriestree = Redis::hget("categoriestree", $website_id);
$end = microtime(true);
if (empty($categoriestree)) {
$categories = Category::where("website_id", $website_id)->get();
$categoriestree = CategoryTree::tree(categories: $categories);
$serializedModel = serialize($categoriestree);
Redis::hset("categoriestree", $website_id, $serializedModel);
} else {
$categoriestree = unserialize($categoriestree);
}
$time1 = ($firstconnectionEnd - $firstconnectionStart) * 1000;
$time2 = ($end - $start) * 1000;
echo "first connction time : $time1 <br>";
echo "second connction time : $time2";
dd($categoriestree);
});
and this the output output
i tried to erified Redis configuration and checked for persistence settings and
Monitored system performance and resource usage.
جهاد الادهمي is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.