Best Caching Practices

Hey there! This is more of a general question, more specific to `Bedrock-Ansible.’ We already include Boilerplate’s caching in Nginx, but by default it’s commented out.

What other solutions/ideas should we be using server side? I’m not a huge fan of using a PHP based solution like w3totalcache.

1 Like

You can’t get much faster than sticking Varnish in front of your site.

Other than that, using Batcache with memcached will speed up the site overall. HHVM seems to work fast enough that you won’t need heavy duty caching either :slight_smile:

@etc has some pretty good Varnish rules already built out if I remember correctly.

We are working on eventually adding HHVM to ansible, right? Did we ever plan on adding Varnish? @kalenjohnson Batcache hasn’t been updated in years. Is this an issue?

Yeah I just noticed that after I had posted it. I’m not sure what’s up with the lack of updates. Basically it just changes the core WP caches, transients, etc. instead of being stored in the database to be stored in memcached.

I don’t think I’ve heard about a lot of issue with it, but I don’t have extensive experience with it. I have Varnish set up on my personal site.

With having a site that’s fully SSL, I have to use NGINX for the SSL termination in conjunction with Varnish right? Also, I’m using a DO VPS that has 1gb of RAM, do you think that’s enough to run Varnish, NGINX and OPCache? I’m using Bedrock Ansible’s default playbook at the moment.

1 GB should be fine but it depends. DO has an article on the setup you want:

I was just about to post a topic on Cache, I’ll roll my questions / comments here.

Re: Varnish, is that something Bedrock-Ansible may include? Seems more powerful than Memcached but maybe it’s a bit much.

On the current Bedrock-Ansible setup Memcached gets installed but is that the only step? I’ve used Memcached on AWS and they supply a node (url) to connect use. If it’s just running on the server is that enough?

Same question for Varnish and Bedrock / Roots.

1 Like

The thing to keep in mind is that Varnish and Memcached are different.

Memcached is more of your standard cache, you can save chunks of HTML or code to it, and it stores it in memory for much faster retrieval than if you were to cache something in say the database or a file. Something like Batcache changes WP’s own cache, including transients, to be stored in Memcached rather than the database.

Varnish is also a type of cache, but works completely differently. It sits in front of Apache or Nginx as a reverse proxy. It then takes entire pages of your site, saves the HTML output, and stores that in memory. So when someone clicks on your site, they are first being served by Varnish. If Varnish has the page cached, it returns it from memory and that’s where the huge time savings are. If it does not have the page cached, it passes it on to Apache or Nginx to handle the actual request.

Varnish is great if your site can use it, but it’s rather opinionated and obviously has issues with more dynamic sites, so I wouldn’t suggest that it be installed by default on something like Bedrock.


You’d need to install the batcache + WP memcached plugin yourself and set some constants in WP config. But other than that it’s all ready to go. The plugins default to the standard local memcached settings so you don’t need to configure that unless you were going to use some external memcached instance.

Or the new kid on the block -

I haven’t used it yet, but reading a lot from people who I trust that think it is epic win and worth the paid business model support.

I haven’t had much luck with any of the more comprehensive cacheing solutions. Most require some kind of setup that can’t be handled programmatically.

I can release my Varnish VCL - it’s fairly complex and has comprehensive conditional statements that control cache headers. I highly recommend Varnish over any of the WP cacheing solutions, as it can be controlled through configuration, which tends to make scaling and deployment much easier.

I don’t know if I’d say Varnish is “opinionated,” it just doesn’t have exceptions built in for WordPress. In fact, given the fact that it’s a blank slate out of box, I’d say it’s the reverse of “opinionated.” It does exactly what you tell it to do. Nothing more, nothing less. It’s extremely flexible and you can script it to do just about anything. We have it controlling cookies and headers as well.

I haven’t tried WP Rocket, but I have tried W3TC and Super Cache - and as I understand it, WP Rocket isn’t that far off conceptually.

Another nice thing about Varnish is that you can use it as a WAF (Web Application Firewall). There are VCLs that mimic OWASP mod_sec rules.

Also, be careful about the 1gb limit. To get the full benefit of Varnish, you’ll need to mallloc it, and you should give it plenty of overhead.


I would appreciate out the VCL for sure. I just moved hosts from DO to Media Temple, and have plenty of ram now to play with.

Here’s an older version that doesn’t have as many site specific rules in it. I need to go through and refactor this so it’s a clean VCL that I augment with site specific VCLs. Please don’t use this out of the box - it does need some care, and the rules need to refactored so you don’t have to pipe around them.

Hopefully this helps a bit. There’s a dearth of good source material for VCLs out there.

Again - this is not done and requires a good amount of work, but should give you a good headstart

1 Like

Further info on utilizing Varnish as a WAF

Don’t underestimate the power of being able to manipulate every header or cookie that passes through Varnish. Once that door opens, there’s little that you can’t do.

@brandon Also, instead of nginx handling the termination, you could use HAProxy, which would open up the possibility of horizontally scaling your installation later. Since WP uses cookie based sessions by default, horizontal scaling is fairly straightforward. HAProxy is also faster and more capable as a load balancer than nginx.


@brandon Here’s a simpler one I came across.

I’d use that as a base and go from there. I have a lot of troubleshooting headers that I add and header “conditioners” in there.

So does anyone have solutions for when something like Varnish is overkill? I’m a little hesitant to move to Memcached given a lack of clear instructions on setting up Batcache and Memcached Backend… and both plugins on being more than 2 years out of date. There is also the issue of creating advanced-cache.php (and possibly other config files) in app/ and how to handle those for deployments.

I work at a small university, for the most part caching is not necessary but I still like to have it setup for cases when some event or news item drives up a bunch of traffic randomly (think emergency situations - active shooter, fire, etc). I feel like Varnish and Memcached are both a little overkill and reach diminishing returns when it comes to setup and configuration compared to WP Super Cache and a properly configured Nginx. But WP Super Cache does not seem to fit into Bedrock since it populates the app/ directory with advanced-cache.php which may or may not change with new versions. I avoid W3TC as it is too complex and bloated. Super Cache provides a nice simple page cache, I just wish I could find a way to set it up with Bedrock so as to not destroy the principals behind the project.

I think you said you were using Apache but Nginx has built-in caching and it’s simple and amazing.

Here’s a good article on it:

Basically you just cache every page for a very short amount of time so you don’t need to worry about cache invalidation. Note that this only applies to users who aren’t logged in which is usually for traffic spikes.


So what is the recommended caching solution for Bedrock-Ansible?

Should I try Batcache or go with the article linked above and get the Nginx cache running?