Wordpress as a “Headless CMS”
WordPress is currently responsible for powering over 30% of today’s modern internet. But out of this roughly 74,652,825 sites how many of these are actually more than brochure websites?
WordPress does indeed allow for “the coffee shop coder” to let their hair down and become a true guru of the modern internet, or does it?
In my current role as Lead Technical Engineer / Solutions Architect, I am often posed with questions such
- Why does my site not scale?
- How can I make my site faster?
Both are valid questions with some quick wins and some not so quick. At the end of the day, WordPress is very good at what it does I won’t dispute this. However, it is still going to be a monolith that typically sits on a LAMP server.
Scaling monoliths is tricky but can be achieved with some effort and expertise. A typical configuration is a load balancer in front of a number of Apache / Nginx servers. This removes the serving of the site as an issue, but simply moves the bottleneck back to the database server. A solution is to swap database technologies to allow concurrent connections perhaps using “Amazon Aurora” or sticking to MySQL / MariaDB with some heavy optimizations that are beyond most developers. Using Hyper DB — a drop-in file for WordPress will allow the use of more than one database. Again, this requires advanced know regarding database replicas and replication procedures.
This all sounds good and achievable but with all these bottlenecks more questions arise such as
- How many web servers do we need?
- How many database servers do we need?
While we are simply increasing servers, this in-turn has a cost and increases the complexity of the infrastructure which causes more questions such as how do we keep the servers in sync?
With two servers a round-robin DNS A record could work to reduce costs but it is not an ideal solution and is my least favorite. When we employ more than 2 servers a load balancer becomes essential. Load Balancers with horizontal scaling servers are great — but with a massive increase in traffic, in a short time period of time, our site is down before the additional servers have time to spin up and come online. Setting lower thresholds and using puppet/custom (AMI) images is a solution but it simply moves the needle and incurs technical debt on an architecture level.
Allow me to speculate for a moment. Imagine having a service that allows for auto-scaling of WordPress in mere seconds. Sounds good right? The good news is it is also possible and achievable using Micro Services and Linux Containers (Docker).
We have currently created a concept known as a “WordPress Headless CMS”. The concept is simple. We create one single LAMP server which is used for WordPress Admin only. This WordPress Admin is used for content creation and editing by the Editorial Staff and Copyrighters. No public internet users can see or use this “WordPress instance”.
Once this in place, we add a custom plugin that simply uses WordPress’s core features (hooks and filters) to replicate and transform the post data to a second database. This second database is not a relational database like MySQL but is a document store. We use MongoDB but this could be any document database — such as AWS’s new Document Database.
Now that we have our replica up and running this now becomes very simple. We create a series of Micro Services which query MongoDB and return the content to the front end — this is the bases of any Modern Application Design.
Our Micro Services can be written in anything that runs inside a docker container. A good choice would be GoLang or NodeJS. The front end that runs and connects to the Micro Service and retrieves the data to display. This Micro Service can again be written in any modern front end application framework such as Angular, VueJS or perhaps React.
Not only does this open up WordPress’s powerful content editing feature but allows scaling a much greater level.
Because you are inside the ecosystems of containers you can use Kubernetes for container orchestration. There are a number of tools at our disposal to simply this process such as RedHat’s OpenShift or AWS’s EKS.
With this new line of though you could also go completely serverless using the likes of AWS Lambda. This will execute up to 3000 requests per second per region. Again this is a massive scale and not for the “coffee shop coder”. It is a simple concept which is proven to work based on my experiences.
I have left a number of items out in this article due to the sheer scale of what is involved in this. However, because we are talking Micro Services this can be a step by step approach. Once you have the key foundations in place you can convert one section of your site at a time.
With this Micro Service approach, you are free to engage with the API using your mobile app or even a PWA.
Feel free to reach out to me and talk about this at any stage.
Published by: John Dunne, Threefold
Originally published at https://www.linkedin.com.