Speed Up Your Website: LEMP Stack Caching Explained (Redis, FastCGI & OPcache)

If you’ve ever wondered why some websites load instantly while others make you wait long enough to boil a kettle, the secret often comes down to one thing: caching.

Let’s take a look at how caching works in a typical LEMP stack — that’s Linux, Nginx, MySQL, and PHP-FPM — and how a few clever helpers like Redis, FastCGI Cache, and OPcache can make your site faster than a greyhound on espresso.

The Basics: What’s a LEMP Stack?

Before we talk about caching, let’s set the scene.

A LEMP stack is the open-source engine room behind many modern websites:

  • Linux — The operating system that everything runs on.

  • Nginx — The web server that handles requests and serves pages.

  • MariaDB/MySQL — The database where your content lives.

  • PHP-FPM — The bit that actually runs your website’s code (for example, WordPress).

Every time someone visits your site, Nginx receives the request, PHP-FPM runs your code, MySQL fetches your data, and the result is sent back as a web page.

That’s fine — but if you’ve got hundreds or thousands of visitors, your poor server will start to sweat.
This is where caching steps in.

What Exactly Is Caching?

Caching is a bit like keeping leftovers in the fridge. Instead of cooking a meal from scratch every time someone’s hungry, you serve up the pre-prepared version.

Your website can do the same — store pre-rendered pages, database results, or compiled code, and hand them out when needed. The result? Fewer resources used, less waiting around, and much happier visitors.

Meet the Caching Dream Team

1. FastCGI Cache – “The Page Butler”

FastCGI cache is part of Nginx. It saves the full HTML output of your pages after PHP has done all the heavy lifting.

Next time someone asks for that page, Nginx just says:

“Oh, I’ve got that one ready to go!”

No PHP, no database, just instant delivery straight from disk.
Perfect for blogs, news sites, and static-style pages.

2. Proxy Cache – “The Gatekeeper”

If your setup has a reverse proxy (say, one Nginx server in front of several others), you can use proxy cache.

It works the same way as FastCGI cache, but instead of talking directly to PHP, it stores responses from your backend servers.

Think of it as a warehouse at the front of your network, handing out copies of popular items before anyone has to go rummaging in the back room.

3. Redis – “The Memory Wizard”

Redis is a tiny but lightning-fast data store that lives entirely in memory (RAM).

It’s perfect for caching objects — little pieces of data your site uses all the time, like WordPress settings, user info, or database query results.

Instead of MySQL digging around on the hard drive for the same data repeatedly, Redis serves it instantly from memory.

It doesn’t replace your database — it just gives it a well-deserved rest.

4. OPcache – “The PHP Booster”

OPcache is the unsung hero hiding inside PHP itself.

Normally, every time PHP runs a script, it has to read it, interpret it, and compile it into something the computer understands. OPcache skips all that nonsense by keeping the compiled code in memory.

That means PHP can get straight to work instead of repeating itself on every single page load.

It doesn’t cache your content — it just makes PHP run faster.

Here’s how these caches stack up in action:

  1. Nginx checks its cache — if it’s got a ready-made page, boom, instant load.

  2. If not, it passes the request to PHP-FPM.

  3. OPcache makes sure PHP runs as efficiently as possible.

  4. PHP checks Redis to see if the data it needs is already there.

  5. If Redis doesn’t have it, PHP queries MySQL, then stores the result in Redis for next time.

  6. The finished page is sent back up, and Nginx saves it to its own cache for good measure.

And, for the next visitor? Everything’s already done.

ToolSpeeds UpWorks WhereWhat It Caches
OPcachePHP executionInside PHP-FPMCompiled PHP code
RedisData fetchingIn-memoryObjects & database queries
FastCGI CachePage deliveryNginxFull HTML pages
Proxy CacheMulti-server setupsNginx (reverse proxy)Full backend responses

Caching isn’t magic, but it’s pretty close.
By stacking these layers smartly — OPcache, Redis, FastCGI Cache, and Proxy Cache — your LEMP setup becomes a lean, mean, page-serving machine.

So next time your website feels sluggish, don’t rush out to buy a bigger server.
Just give it a bit of caching love — and watch it fly.

A Quick Note About Varnish

You might have heard of Varnish Cache — it’s another powerful web accelerator that sits in front of your web server, a bit like Nginx’s proxy cache on steroids.

Varnish is brilliant at full-page caching. It keeps ready-made versions of your web pages in memory and can serve them faster than just about anything else. It’s especially popular for very high-traffic or enterprise sites where every millisecond counts.

However, in a LEMP setup, most people skip Varnish for one simple reason:
Nginx can already do a similar job using its built-in FastCGI cache or proxy cache.

Adding Varnish would mean another moving part — another port, another cache layer to purge, and more complexity. For many small-to-medium sites, it’s simply not worth it.

That said, if you’re running a huge site, or want ultra-fine control over caching rules (like different policies for logged-in users, cookies, or headers), Varnish is a strong choice. It’s fast, scriptable, and proven at scale.

So in short:

  • Use Nginx caching for simplicity and speed.

  •  Consider Varnish if you’re managing serious traffic or need enterprise-level flexibility.

Till next time…Keep it speedy!

Tim