untymage's avatar

Redis cache is slower than laravel file store ?

I'm just wondering if redis is so popluar why it is slower than laravel cache file store ?

Benchmark:

Caching 100 user in both stores:

Cache::store('file')->rememberForever('users', fn() => User::factory()->count(100)->make());
Cache::store('redis')->rememberForever('users', fn() => User::factory()->count(100)->make());

Getting cache key (Iterate 1000 time)

alt text

> $start = microtime(true);foreach (range(1,1000) as $item) {Cache::store('redis')->get('users');}$result = microtime(true) - $start;dump($result);
0.84473991394043 // vendor/psy/psysh/src/ExecutionLoopClosure.php(55) : eval()'d code:6
= 0.84473991394043 <----------- redis store

> $start = microtime(true);foreach (range(1,1000) as $item) {Cache::store('file')->get('users');}$result = microtime(true) - $start;dump($result);
0.72488903999329 // vendor/psy/psysh/src/ExecutionLoopClosure.php(55) : eval()'d code:6
= 0.72488903999329 <----------- file store

So i heard redis is blazing fast and leverage memory speed, Shouldn't it at least faster than file store cache in laravel?

Note than this is php redis native extension.

0 likes
7 replies
Sinnbeck's avatar

Cannot test currently but 2 questions.

  1. How many times did you tests? 1 time is not a baseline
  2. Any change if you swap them out or only do one at a time?
untymage's avatar

@Sinnbeck

  1. Multiple times and redis is slower than file store
  2. I do benchmark one at a time what you mean by swap ?
Sinnbeck's avatar

@untymage I mean exit tinker and enter it again. Or run file before redis

What is the set up? Docker? Mac? Windows?

senrab's avatar

I've also noticed this occasionality where a customer has a remote Redis server somewhere either down the LAN or out on the Internet somewhere and the local server has NVMe disk or similar. File Cache is faster due to the Redis network latency.

fideloper's avatar

The timings depend on quite a few factors (network speed, how close the redis instance is to your web server, etc).

The most common benefit of Redis is that it doesn't need to be hosted on the local server, and thus multiple servers can access it. This gives you the ability to load balance web requests across all servers, while using a central cache and/or session store.

I believe if you use Octane, you can re-use a Redis connection as well, so you don't need to keep opening/closing TCP connections on each request.

Please or to participate in this conversation.