PHP Caching Guide: APCu vs File Cache vs Redis – How to Choose the Right Strategy

PHP Caching Guide: APCu vs Redis vs File Cache
Oleksandr PolishchukOleksandr P.
|
03 March 2026

Caching in PHP web applications is often treated as a performance tweak. In reality, it is an architectural decision that directly affects scalability, infrastructure cost, consistency, stability under load, and even security.

Many developers start thinking about caching only after performance issues appear. Pages become slow. Database CPU spikes. API calls start timing out. That’s when caching is added reactively.

But the right way to approach caching is to design it as part of your system architecture - especially if you are deploying to cloud platforms like Azure, AWS, or GCP where horizontal scaling (multiple instances behind a load balancer) is common.

In this article we will explore:

  • What caching really means in PHP applications
  • id How caching affects performance and TTFB
  • APCu, File Cache, and Redis from an architectural perspective
  • Multi-instance challenges
  • When caching is not appropriate
  • Security considerations
  • A real-world Yii2 + Azure + 2 instance case study
  • Practical decision guidelines

This is not just a tool comparison. It is a decision framework.


What Is Caching in PHP Applications?

Caching is the act of storing the result of expensive operations so they don’t need to be executed again.

Expensive operations may include:

  • Complex SQL queries
  • Aggregations and reporting
  • External API requests
  • HTML fragment rendering
  • Configuration loading
  • Business logic calculations

Every web request typically goes through:

  1. Web server (Nginx / Apache)
  2. PHP runtime
  3. Framework bootstrap
  4. Database queries
  5. Business logic execution
  6. View rendering
  7. Response generation

If parts of this pipeline can be skipped by reusing precomputed data, response time drops dramatically.

Caching effectively reduces the amount of work your server has to do per request.


How Caching Impacts Performance and TTFB

From the browser’s perspective, one of the most critical metrics is TTFB (Time To First Byte). This is the time between the request being sent and the first byte of response arriving.

If your backend needs 800ms to generate a page, users will feel lag.

If the same page is returned from cache in 40–60ms, the perceived performance improves dramatically.

Caching improves:

  • Latency stability
  • Database load
  • CPU utilization
  • Scalability under traffic spikes

It also helps smooth out performance variability. Without caching, response time fluctuates depending on database load and API latency. With caching, response time becomes predictable.

Predictability is often more important than raw speed.


Types of Caching Relevant to PHP

There are multiple caching layers in modern web systems:

  • OPcache (PHP bytecode cache)
  • APCu (in-memory per instance)
  • File-based cache
  • Redis / Memcached (external in-memory store)
  • HTTP-level caching (CDN, reverse proxies)

This article focuses on application-level data caching:

  • APCu
  • File Cache
  • Redis

Each of these operates at a different architectural level.


APCu – In-Memory Cache Per Instance

APCu stores cached data directly in memory within a single PHP runtime environment.

It is extremely fast because:

  • Memory access is faster than disk
  • No network round trips
  • No serialization over TCP

For single-server environments, APCu is often the simplest and fastest solution.

However, in multi-instance environments, APCu introduces a fundamental limitation:

APCu is not shared between servers.

If you have:

  • Two Azure App Service instances
  • A load balancer distributing traffic
  • Requests randomly routed

Each instance maintains its own independent APCu memory.

This creates several issues:

  • Cache inconsistency between instances
  • Difficult invalidation
  • Unpredictable data freshness
  • Debugging complexity

You might update cache on instance A, but instance B still serves stale data.

APCu works perfectly in development. It becomes problematic in production multi-instance setups.


File Cache – Shared Filesystem-Based Caching

File-based caching stores serialized data in files on disk.

It is slower than APCu because disk access is slower than memory access. However, when deployed on shared storage, it gains a major architectural advantage:

It can be shared across instances.

In cloud environments, shared storage might be:

  • Mounted network disk
  • NFS
  • Azure shared filesystem
  • Shared container volume

If all instances read and write from the same cache directory, cache consistency is restored.

Advantages of File Cache:

  • No additional services required
  • Works across instances
  • Low infrastructure cost
  • Easy integration with tag-based invalidation (e.g., Yii2 TagDependency)

Limitations:

  • Disk I/O overhead
  • Large number of small files
  • Performance depends on storage latency

Despite being slower than in-memory solutions, File Cache can be architecturally stable and cost-efficient.


Redis – Distributed In-Memory Caching

Redis is an external, dedicated in-memory data store.

Unlike APCu, Redis runs as a separate service and is shared across all instances.

Benefits:

  • True multi-instance support
  • High performance
  • Built-in TTL handling
  • Atomic operations
  • Pub/Sub
  • Advanced data structures

Redis is the professional-grade solution for distributed caching.

However, Redis introduces:

  • Additional infrastructure complexity
  • Monitoring requirements
  • Higher cloud cost
  • Operational overhead

For high-traffic or enterprise applications, Redis is often the right choice. For cost-sensitive projects, it may be excessive.


Comparative Table

CriterionAPCuFile Cache (Shared Storage)Redis
SpeedVery HighMediumVery High
Multi-instance SupportNoYesYes
Infrastructure CostNoneLowMedium to High
ScalabilityPoorLimitedExcellent
ConsistencyLowHighVery High
Operational ComplexityLowLowMedium

Multi-Instance Environments: Why Architecture Matters

In cloud environments, horizontal scaling is common.

When your application runs on multiple instances:

  • Memory is not shared
  • Local disk is not shared (unless explicitly mounted)
  • Processes are independent

This means that:

  • APCu becomes per-instance
  • Local file sessions break
  • Cache invalidation becomes non-trivial

Your caching strategy must match your infrastructure.

Choosing APCu in a multi-instance environment without sticky sessions is an architectural mismatch.


When Caching Should NOT Be Used

Caching is powerful - but it is not universally beneficial.

There are cases where caching may introduce complexity without real benefit.

1. Highly Personalized Content

If every user sees:

  • Custom dashboards
  • Personal offers
  • Unique transaction lists
  • Individual metrics

Then caching entire fragments may result in:

  • Huge cache growth
  • Memory pressure
  • Complicated invalidation logic

Sometimes optimizing database indexes is more effective than caching millions of variations.

2. Frequently Changing Data

If data changes every second, caching adds little value.

Short TTL may help, but constant invalidation reduces effectiveness.

3. Security-Sensitive Data

Never cache:

  • Sensitive PII without protection
  • Access tokens
  • Private financial data

If you cache data in shared storage, ensure that no sensitive content can leak between users.

4. Incorrect Cache Scope

Caching global state for user-specific logic is dangerous.

For example:

  • Caching authorization state globally
  • Caching permission-based content without user context

Incorrect cache scoping can introduce serious bugs.


Performance Considerations Beyond Speed

Caching improves speed - but also stability.

Under load:

  • Database connections saturate
  • Query time increases
  • API calls stack up

Caching reduces backend pressure and improves stability under peak traffic.

However, improper invalidation can cause:

  • Serving stale data
  • Race conditions
  • Hard-to-debug behavior

Correct invalidation strategy is as important as cache storage.


Real Case Study: Yii2 + Azure + Two Instances

In one of my Yii2 projects deployed on Azure, I initially implemented APCu caching.

Locally, everything worked perfectly.

After scaling to two instances:

  • Cache invalidation became inconsistent
  • Synchronization state appeared different on each instance
  • Users saw inconsistent data
  • Debugging became frustrating

Redis was the logical solution.

However:

  • It increased infrastructure cost
  • Required additional configuration
  • Added operational overhead
  • Budget constraints applied

The practical solution was switching to FileCache with shared storage:

 
/home/runtime/frontend/cache
/home/runtime/backend/cache
 

Both instances read and wrote to the same cache directory.

Results:

  • Consistent cache invalidation
  • Predictable behavior
  • No additional services
  • Lower cost
  • Acceptable performance trade-off

It was not the fastest possible solution, but it was architecturally aligned with infrastructure and budget.


Final Thoughts

Caching is not just about speed.

It is about architectural alignment.

APCu is excellent for single-server environments.

File Cache is a strong budget-friendly solution for multi-instance systems when shared storage is available.

Redis is ideal for scalable, high-traffic distributed systems.

The right choice depends on:

  • Infrastructure
  • Budget
  • Traffic profile
  • Consistency requirements
  • Operational complexity tolerance

The fastest solution is not always the correct one.

Sometimes the simplest architecture is the most reliable.


If you would like, I can next:

  • Expand this further into a 30-minute deep technical essay
  • Add architecture diagrams (APCu vs Shared FS vs Redis)
  • Add a “Common Caching Mistakes” section
  • Add performance measurement strategies
  • Prepare a version tailored for Medium or Dev.to publication

Just tell me the target platform.