Back to results for “Caching”
PHP › Caching Strategies in PHP

What is the difference between file based caching and in memory caching using tools like Redis or Memcached?

Beginner

Answer

File based caching stores cached data as files on the server's disk, which is simple to set up and requires no additional infrastructure but is generally slower to read and write compared to memory, while in memory caching solutions like Redis or Memcached store data directly in RAM, offering significantly faster read and write speeds and additional features like automatic expiration and shared access across multiple servers, making them the preferred choice for high traffic applications.
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('user_count', 1500, 300);
Real-world example A high traffic application switches from file based caching to Redis after noticing that file based caching struggled to keep up under heavy concurrent load, gaining both speed and the ability to share cached data across multiple web servers.

Common follow-ups: What are the tradeoffs of setting up a separate Redis server compared to simple file caching?;How does caching behavior change when running an application across multiple servers?