NGINX.COM
Web Server Load Balancing with NGINX Plus

Perhaps it’s your first time on the top of Hacker News, or maybe your site’s been picked up by that popular news outlet. If you’ve never worried about scaling and web performance before, we’ll take a look at the basics and give you a place to start.

Don’t worry, NGINX and NGINX Plus can help.

Numerous articles will tell you conflicting information about when you should worry about performance and scalability. Before jumping right into it, it’s important to note Step 0 in our process:

Don’t let concern over scale prevent you from moving your project forward.

Now, let’s jump into the basics.

Improve Performance

Spiff Up your Code

When looking for ways to improve performance, an easy first place to start is to go through your app and optimize your code. If you are trying to proactively set yourself up for speed, avoid perfection paralysis by following best practices for your chosen language and framework. DRY your application code (keeping in mind someone will later have to understand it) and focus on using the right tools for the job. Make your database your frenemy and only interact with it when you really really have to. Stop showing off your knowledge of recursion and use fewer loops. (To understand recursion, one must first understand recursion… and then use it as rarely as possible.)

Next, use a tool like Datadog, Graphite, or New Relic to better understand where your bottlenecks are. Identify issues and then refactor your way towards better performance.

Tuning Your Database

After you’ve improved your application code, check out your database. Tuning your database properly can significantly improve performance. If you’re using an SQL database, it’s a good idea to make sure you’ve indexed properly. Check out this detailed set of tutorials on indexing and database tuning. Find ways to limit the total trips to your database, keeping a particular eye on how many joins you’re using. Have you considered buffering and flushing your database?

Consider Using a CDN

Content delivery networks (CDNs can improve performance by serving your CPU-expensive static content to your users from the servers closest to them in the network. MaxCDN uses NGINX to deliver content and improve performance for their customers. Netflix does as well.

You can set up your own CDN using NGINX like NGINX community member Ibrahim Diallo.

Caching

Caching on a per‑object basis, on a full‑page basis, or at a level in between can improve performance significantly and enable your site to handle more traffic. For example, NGINX community member Martin Fjordvald took advantage of NGINX’s memcached module to handle 12,000 requests per second.

Be Prepared to Scale Horizontally

Immutable Infrastructure

Creating immutable infrastructure is a great way to be prepared for horizontal scaling, and provides further benefits including atomic deployments, quick recovery, and increased resilience, by acclimating teams to the kinds of failures common with cloud‑based infrastructure.

One of the fastest ways to get up and running with NGINX is by using the official Docker Image. If you’re new to Docker and NGINX, check out these deployment tips.

Load Balancing

When spinning up multiple instances of your application, you can use NGINX as a load balancer, as explained in this tutorial for Rails application developers.

Whether or not your application requires session persistence can affect both performance and scaling. Support for session persistence is one of the many features you’ll get with NGINX Plus. Check out our posts on load balancing with session persistence or try NGINX Plus yourself free for 30 days and start hacking now!

Conclusion

When faced with the delightfully terrifying prospect of a spike in traffic to your site, don’t slink away from the challenge. Keep the basics of performance and scalability in mind. NGINX Plus is here to help you meet the challenge with load balancing, caching, and high‑performance web serving.

(Learn how at our User Summits and Trainings!)

A special thanks to Florian Motlik and Kelly King for improving this article.

Hero image
Free O'Reilly eBook: The Complete NGINX Cookbook

Updated for 2024 – Your guide to everything NGINX



About The Author

Shannon Burns

Shannon Burns

Developer Advocate

About F5 NGINX

F5, Inc. is the company behind NGINX, the popular open source project. We offer a suite of technologies for developing and delivering modern applications. Together with F5, our combined solution bridges the gap between NetOps and DevOps, with multi-cloud application services that span from code to customer.

Learn more at nginx.com or join the conversation by following @nginx on Twitter.