How to Prepare for a Traffic Spike

How to Prepare for a Traffic Spike image

Here's a nightmare scenario: You've been working for months to prepare your company's codebase for its next big product launch. But minutes into the launch, the website starts throwing 404 errors. How did this happen? Or, even better, how can you prevent this from happening?

Locust.io is a Python open-source load testing tool that can swarm your system with millions of simulated users. Its UI provides you with easily understandable graphics and charts to understand where the bottlenecks in your system lie. It's a great tool to use whenever you want to stress-test your architecture.

The Locust UI general dashboard

In the above screenshot, you can see every endpoint's performance. This makes it easy to figure out if any endpoint is causing problems. Here, we see that the /home endpoint went from a max of eleven seconds to an average of less than two. So we probably need to work on the first load time of that endpoint.

Some of the out-of-the-box graphs Locust provides

In the above screenshot, we can see that our website goes from zero to about twenty-four requests per second (RPS) with the response time rising along with it. Then the cache kicks in and the response time decreases by about ninety percent to one and a half seconds. Not bad.

You can use Locust with the command line too. This gives you the ability to integrate the tool into your CICD architecture without requiring a UI or callbacks.  

Using the command line to automate CICD flows

So how can you avoid the horror story described in the intro of this blog post? Well, it only requires a few lines of code:

The Locust code you need

To run the test, simply fire up a terminal and navigate to the directory where you've saved your Locust file. Run this line: locust -f {xteam_demo.py}. Then run your web UI URL at http://0.0.0.0:8089.

Starting Locust with UI 

In the UI, you just need to specify how many users you want and how quickly they should swarm your website.

Locust web UI configuration screen

That's all there is to it. That's how you can stress-test your website and ready it for an expected (or unexpected) traffic spike. I hope this example has demonstrated the power of Locust.

If you want more information, here are a few good resources to help you get started:

KEEP MOVING FORWARD

Mario Soberanis / code