More than a technology, caching is best thought of as a technology-enabled *strategy.* Like all strategies, you need to carefully consider your goals and resources, understand the available technologies and re-evaluate regularly as your goals, resources and available technology change.
Why cache?
By storing and retrieving commonly-needed information in memory instead of disc, caching improves site performance in two key ways – speed and economics. First, users will have a much happier experience with your site when the stuff they want loads quickly, and caching makes that happen.
For example, when Walmart set out to improve their site’s performance, for every 1 second improvement in site speed they achieved, they saw up to a 2% increase in conversions. And StrangeLoop found that a 1 second delay can cost 7% in sales.
Beyond improving the user experience, some research has also shown a correlation between web speed and Google search rankings, with faster sites appearing higher.
Secondly, caching reduces the computational resources that your site will need to serve visitors. Serving webpages from an in-memory cache is relatively “cheap” in infrastructure terms, versus making lots of resource-intensive database calls. This results in real dollar savings as your business grows, because your server infrastructure will be able to serve more pages and visitors before hardware upgrades are required.
Breaking down the caching stack
You can implement caching across the entire site stack, from code, to database, to CMS to browser. How much caching you need will be determined by your goals, resources (money, time and expertise), and site load.
This blog focuses on Magento ecommerce sites because they represent a very common use case where responsiveness and server utilization are mission critical. Depending on your site’s objectives, you may not need to implement caching at every level described below.
PHP caching with OPCache
Many dynamic sites are built with PHP, including Magento. When a PHP script is requested, it is parsed, compiled and then executed. These steps increase response time, and are often unnecessary since application code changes infrequently.
OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request. This extension is bundled with PHP 5.5.0 and later.
In their article on the subject, AppDynamics found that adding OPcache improved performance by 14%. Also check out this great article from sitepoint for more about OPcache.
Database caching
Being a database-driven application, Magento makes numerous db actions with every page load:
- There are a series of database queries, each of which is parsed before the query is executed
- The data is retrieved from disk or storage
- The data is sorted and manipulated
- Ultimately, the data is returned to the client
This series of steps, and in particular accessing information from disk, slows site performance. MySQL query_cache_size is one common solution that tells the server to store the result of the query in memory, which is much faster than accessing from disk.
Like everything else, the impact MySQL query caching yields depends on the site characteristics. Sites driven by a frequently-updated table will not see much, if any, improvement with this approach, and it could actually be a detriment.
Key-store caching with Redis or memcached
Memcached and Redis are the leading NoSQL, in-memory key-value data stores for caching frequently-requested site content in RAM. Tranquil Hosting customers overwhelmingly prefer Redis to memcached, and to read this infoworld article comparing the two, it’s easy to see why. Redis offers greater power and flexibility to tune your cache. It also seems to be better-supported with a more active community. Significantly, with version 1.14.2, Magento Enterprise Edition includes Redis, making it the default choice for ecommerce.
Full page caching
There are several Magento extensions that provide full page caching. This can dramatically reduce page load speeds, as shown in the following graph from Mirasvit, one of the more popular Full Page Cache extensions for Magento.
When implementing this extension, be sure to exclude pages with sensitive or personalized content. While we’re on the topic, web site personalization is an important trend that does complicate your caching strategy. It doesn’t mean you can’t cache, only that you need to be deliberate about what you build into the base page and what you fetch using Ajax calls or cookies. Akamai provides a thorough discussion of caching highly personalized sites here.
Content caching using NGINX
NGINX and Varnish sit in front of static, dynamic or app servers. After the first time a user requests a specific page, the page is stored in the cache’s memory and the next time that page is requested, it is served to the user directly from the cache, significantly reducing the load on origin server. While Varnish is a pure proxy/cache server, NGINX is that and also a full-blown web server that can replace Apache. NGINX claims to serve 12% of the Internet’s sites, delivering compelling performance improvements, on the order of 50-75% hardware load reduction.
At Tranquil, we have worked extensively with NGINX and know how to optimize it for sites running Magento and other custom code.
Before you dump Apache and expect to see a big performance boost with NGINX, beware that unlike Apache, NGINX does not include a PHP module. This means that you will need to use a fastcgi processor to handle PHP pages.
Microcaching is another strategy that is often used with NGINX for highly dynamic content. Basically, you store content in the NGINX cache for say 1 second. This approach can increase the number of requests per second your infrastructure can handle by 10X versus Apache or NGINX without microcaching.
Caching risks and gotchas
Cache, not Crutch
A comprehensive and well-executed caching strategy is not a substitute for a well-coded site. Customers often call us when their sites load slowly thinking it’s a server or network issue. When our datacenter troubleshooting returns a clean bill of health, the next place to look is the code. Sometimes, extensive caching has masked the bad code while loads were low, only to become problematic as traffic grew. In special circumstances, we have assisted clients with site audits, which have revealed things like production sites logging activity to a database, bringing performance to a crawl.
Security
Caching lots of things makes sense, but customer credentials or shopping carts would be exceptions. Smart caching needs to factor in and protect customer PII. Caching needs to be turned on in a careful, well-thought out way. Otherwise you may end up with a super fast site where Customer A sees the shopping cart or checkout info for previously-cached Customer B.
You also need to secure your caching storage from outside access. Memcached, for instance, acts as a ‘server’ and will allow any client to connect without any form of authentication. If memcached is setup on a public IP address and not protected by a firewall, anyone can view your entire cache, which may contain sensitive data, and a hacker can wreak havoc on your site by manipulating the cached data. Whether you are using Redis or Memcache, access has to be controlled by a firewall.
Tranquil Hosting Cache Services
As part of our Managed Cloud and Dedicated Hosting offers, Tranquil will consult with you to determine how much caching you need to meet your goals, and will help you select the best technical options. We will then set-up and maintain the caching tools to ensure your site meets your business needs and your end-users’ expectations. Partnering with Tranquil effectively bolsters your caching resources by putting our decade plus of experience optimizing site performance to work for you.
If you’d like to discuss site performance, please comment below or contact us for a completely free, no strings-attached consultation.