Just how effective is web server caching? Very.
Tags: performance, nginx, webdev, caching
Published: 2018-11-07 05:40:00

---
title: Just how effective is web server caching? Very.
published: true
description: Example of how web server caching can turn a small machine into a power house.
tags: #performance, #nginx, #webdev, #caching
cover_image: https://blog.davidjeddy.com/wp-content/uploads/2018/11/high_performance_gauge.jpg
---

<!-- wp:paragraph -->
<p>Running a high load website can be used as a test case for the negative effects of stress over long periods of time has on the human body. Mental pressure, hair loss, lack of sleep, burn out, social disconnection. Thankfully it does not have to be like this. Running a high load web CMS does not have to be stressful. Come with me as I setup and Wordpress CMS to handle 1000 users <strong>per minute</strong> for less than the cost of lunch.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Note: This is not a HA (high-availability) configuration and is NOT production ready. Thought, it is a solid starting point for one.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Tools for the toolbelt!</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The tools this solution leverages are pretty mundane. No fancy HAProxy cluster with multi-cloud CDN edge locations here. Simplicity is king. A basic, bottom dollar Lightsail instance running an Ubuntu Server. Total cost to run the minimal infrastructure: about $10/USD month. How much you will pay in data transfer depends on you.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Tech Stack</strong>:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>AWS IaaS provider<br></li><li>Lightsail - Compute Instance</li><li>Ansible, Terraform, and Gatling for testing</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Install all the things (programs)</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To make setting up and testing different configurations as easy as possible I created a simple Terraform configuration and used Ansible to install and configure the different. The three config under test are no caching, Wordpress plugin caching and Nginx micro caching. Nothing to fancy there. If you are interested <a href="https://github.com/davidjeddy/high_load_cms_for_less_than_the_price_of_lunch">here is the GiT repo</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Caching, hoah, what is it good for!</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Every time a user requests a page the request is processed by Nginx (the web server), then the interpreter process the Wordpress PHP logic and outputs the HTML/JS/CSS. Passing the payload to Nginx to be returned to the requesting user. Two steps along this process can be speed up massively with caching. Caching keeps a copy of the response for a given amount of time, returning the stored copy rather than re-processing the entire request every time.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Two caching settings we will need to implement:  Wordpress via the <a href="https://blog.davidjeddy.com/wp-admin/plugin-install.php?tab=plugin-information&amp;plugin=wp-super-cache&amp;TB_iframe=true&amp;width=600&amp;height=550">WP Super Cache</a> plugin and Nginx micro caching.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Since we will be running PHP 7.x OPcache is enabled by default so we do not need to worry about that now (unlike with PHP 5.x).</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2><del>Party</del> Picture time!</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>All configurations below have a base line of 1000 users over a 1 minute time length on Ubuntu 18.04 running latest Nginx, MySQL, and PHP as of 2018-11-02.<br></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First up, lets look at a basic LEMP install with no caching at all.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1112,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/no_cache_requests.png" alt="" class="wp-image-1112"/><figcaption>Wordpress CMS, no caching.</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:image {"id":1109,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/no_cache.png" alt="" class="wp-image-1109"/><figcaption>Looks ugly after about 200 concurrent users.</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Wow, that's rough. But is gives us a base line. Looks like the machine can handle around 200 users per second before things start going bad.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Installing and configuring the Wordpress caching with the below settings we test a type of application level caching.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1114,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/wordpress_cache_settings.png" alt="" class="wp-image-1114"/><figcaption>Highest rated Wordpress cache plugin, here's the settings.</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:image {"id":1110,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/wordpress_cache.png" alt="" class="wp-image-1110"/><figcaption>A bit better, but was really hoping for better,</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>So the application level caching brought down the failure rate; but not really enough to matter.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Lastly, I tore down the setup and stood up a fresh setup. This time with Nginx micro caching with a one minute life time.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1113,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/nginx_cache_requests.png" alt="" class="wp-image-1113"/><figcaption>Now this is more like it.</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:image {"id":1111,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/nginx_cache.png" alt="" class="wp-image-1111"/><figcaption>Request in, response out; fast.</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Boom! Look at that! Sure, still some errors but WOW! 0 to 1000 linear increase in users over a minute and it ticks along like someones business. I noticed the max concurrent users was much lower given the Nginx cache. After digging in I found that it was due to the requests being completed and not clogging up the system.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Wrapping it up boss.</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>1000 users per second at the end of a minute and the little web server was ticking along like nothing was happening. I'd consider that high performance given the little effort it takes to configure.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So what does performance do for the business? Lowers cost of operation. The less time and hardware resources each requests requires, the more requests for a given piece of hardware can handle. This directly translates to lower operating cost for a given workload. Performance is king, within reason.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Leave a comment below and let me know what you think; or even your experience with performance tuning.</p>
<!-- /wp:paragraph -->