Build a WordPress One-Click application on DigitalOcean and scale it with Memcache

One-click applications are also known as Marketplace images.

This tutorial will walk you through the steps of creating a simple WordPress One-Click application on DigitalOcean and then adding Memcache to prevent or alleviate a performance bottleneck.

Adding caching to your web applications can drastically improve performance. The results of complex database queries, expensive calculations, or slow calls to external resources can be stored in Memcache that can be accessed via fast O(1) lookups. Even for small sites, Memcache can make page loads snappy and help future-proof your app.

Prerequisites

Before you complete the steps in this guide, make sure you have all of the following:

  • Some basic familiarity with a terminal or command prompt.
  • Familiarity with PHP (and ideally WordPress).
  • A DigitalOcean account.
  • If you like managing DigitalOcean resource via the CLI, you will need doctl installed and configured.

Create a WordPress One-Click application

To build an app you first need a droplet. Either go to your DigitalOcean dashboard and create one or launch one via the CLI:

Give the droplet a minute to come up and then look up its IP via the dashboard or by typing

Now you can login to your droplet via

WordPress installation

A WordPress installation script will run automatically after login.

  1. Enter a real domain if you have one you would like to use. Otherwise, for this tutorial we will use the Droplet’s IP.

  2. Enter an email address for the WordPress admin user

  3. Enter a username

  4. Enter a password

  5. Enter a title for this website

  6. SSL with Let’s Encrypt

    Without a fully qualified domain name (FQDN), Let’s Encrypt cannot set up, so skip this step.

If all has gone well you will see the following

You should now be able to access your WordPress site at http://<DROPLET_IP>.

Add caching to WordPress

Memcache is an in-memory, distributed cache. Its primary API consists of two operations: SET(key, value) and GET(key). Memcache is like a hashmap (or dictionary) that is spread across multiple servers, where operations are still performed in constant time.

The most common use for Memcache is to cache expensive database queries and HTML renders so that these expensive operations do not need to happen over and over again.

Configure environment

Install the php-memcached library with

To take effect, restart apache

Make sure the memcached module is listed when you run the following

Provision a Memcache

To use Memcache in WordPress, you first need to provision an actual Memcached cache. You can easily get one for free from MemCachier. This allows you to just use a cache without having to setup and maintain actual Memcached servers yourself. Make sure to create the cache in the same region as your droplet is in.

Configure WordPress with MemCachier

There are three config variables you will need for WordPress to be able to connect to your cache.

  1. MEMCACHIER_SERVERS
  2. MEMCACHIER_USERNAME
  3. MEMCACHIER_PASSWORD

Get them from your MemCachier dashboard and add them to your wp-config.php file.

Note: your droplet comes with nano or vim preinstalled. If you have never used an editor in a terminal before, I recommend using nano for now. In your root terminal tab or window, enter the following to edit your wp-config.php file

Add the following just above /* That's all, stop editing! Happy publishing. */.

Note, if you are on a paid MemCachier plan with more than one server, you will add a array( '<MEMCACHIER_SERVER>', 11211 ) value per server. For example, if your plan had two servers, the $memcached_servers value would be:

Use WordPress’ integrated caching

WP Object Cache is WordPress’ integrated object cache. It takes care caching WordPress database queries and clearing stale data automatically.

WP Object Cache is WordPress’ class for caching data which may be computationally expensive to regenerate, such as the result of complex database queries.

The MemCachier WordPress object cache backend integrates the MemCachier caching service with WP Object Cache.

To set it up, get the object-cache.php file and move it to /var/www/html/wp-content.

View Memcache statistics

To test your cache is correctly configured check your MemCachier dashboard. You can see that the first time you access a post or page in WordPress, get misses increases. This is because the first time a post loads, the database queries related to that post are not in the cache. The set cmds also increase because the post’s database queries are saved to the cache. If you refresh the post, get hits increases because the cached database queries are being served.

When you update a post get misses will increase again because the cache was invalidated.

Further reading and resources