Load Balancing and Scaling with NGINX

28
Load Balancing and Scaling with NGINX Introduced by Andrew Alexeev Presented by Owen Garrett Nginx, Inc.

Transcript of Load Balancing and Scaling with NGINX

Page 1: Load Balancing and Scaling with NGINX

Load Balancing and Scaling with NGINX

Introduced by Andrew Alexeev

Presented by Owen Garrett

Nginx, Inc.

Page 2: Load Balancing and Scaling with NGINX

About this webinar

When one server just isn’t enough, how can you scale out? In this

webinar, you'll learn how to build out the capacity of your website. You'll

see a variety of scalability approaches and some of the advanced

capabilities of NGINX Plus.

Page 3: Load Balancing and Scaling with NGINX

INTRODUCING NGINX…

Page 4: Load Balancing and Scaling with NGINX

What is NGINX?

Internet

N

Web ServerServe content from disk

Application ServerFastCGI, uWSGI, Passenger…

ProxyCaching, Load Balancing… HTTP traffic

Application Acceleration

SSL and SPDY termination

Performance Monitoring

High Availability

Advanced Features: Bandwidth Management

Content-based Routing

Request Manipulation

Response Rewriting

Authentication

Video Delivery

Mail Proxy

GeoLocation

Page 5: Load Balancing and Scaling with NGINX

143,000,000Websites

NGINX Accelerates

Page 6: Load Balancing and Scaling with NGINX

22%Top 1 million websites

37%Top 1,000 websites

Page 7: Load Balancing and Scaling with NGINX

NGINX and NGINX Plus

NGINX F/OSS

nginx.org

3rd party modules

Large community of >100 modules

Page 8: Load Balancing and Scaling with NGINX

NGINX and NGINX Plus

NGINX F/OSS

nginx.org

3rd party modules

Large community of >100 modules

NGINX Plus

Advanced load balancing featuresEase-of-managementCommercial support

Page 9: Load Balancing and Scaling with NGINX

WHY LOAD-BALANCE?

Page 10: Load Balancing and Scaling with NGINX

Load-balancing Web Servers

Internet

N

Improved Application AvailabilityManagement Increased Capacity Advanced techniques e.g. A|B testing

Why? DNS Round Robin Hardware L4 load balancer Software Reverse Proxy LB Cloud solution

How?

Page 11: Load Balancing and Scaling with NGINX
Page 12: Load Balancing and Scaling with NGINX

Three Load Balancing case studies

Basic Load Balancing with NGINX

When you need more control

Advanced techniques

1

2

3

Page 13: Load Balancing and Scaling with NGINX

1. Basic Load Balancing

• Simple scalability

– All servers have same applications/services

– Load-balancer extracts optimal performance

Page 14: Load Balancing and Scaling with NGINX

Basic load balancingserver {

listen 80;

location / {

proxy_pass http://backend;

}

}

upstream backend {

zone backend 64k;

server webserver1:80;

server webserver2:80;

server webserver3:80;

server webserver4:80;

}

Page 15: Load Balancing and Scaling with NGINX

Basic load balancing

• Use logging to debug: “$upstream_addr”

log_format combined2 '$remote_addr - $remote_user [$time_local] '

'"$request" $status $body_bytes_sent '

'"$upstream_addr"';

192.168.56.1 - - [09/Mar/2014:23:08:56 +0000] "GET / HTTP/1.1" 200 30 "127.0.1.1:80"192.168.56.1 - - [09/Mar/2014:23:08:56 +0000] "GET /favicon.ico HTTP/1.1" 200 30 "127.0.1.2:80"192.168.56.1 - - [09/Mar/2014:23:08:57 +0000] "GET / HTTP/1.1" 200 30 "127.0.1.3:80"192.168.56.1 - - [09/Mar/2014:23:08:57 +0000] "GET /favicon.ico HTTP/1.1" 200 30 "127.0.1.4:80"192.168.56.1 - - [09/Mar/2014:23:08:57 +0000] "GET / HTTP/1.1" 200 30 "127.0.1.1:80"192.168.56.1 - - [09/Mar/2014:23:08:57 +0000] "GET /favicon.ico HTTP/1.1" 200 30 "127.0.1.2:80"192.168.56.1 - - [09/Mar/2014:23:08:58 +0000] "GET / HTTP/1.1" 200 30 "127.0.1.3:80"192.168.56.1 - - [09/Mar/2014:23:08:58 +0000] "GET /favicon.ico HTTP/1.1" 200 30 "127.0.1.4:80"

Page 16: Load Balancing and Scaling with NGINX

Basic Load Balancing

• Round-robin is the default– Suitable for consistent pages

• Least Connections– Suitable for varying pages

• IP Hash– Fixed mapping, basic session

persistence

upstream backend {

server webserver1:80;

server webserver2:80;

}

upstream backend {

least_conn;

server webserver1:80;

server webserver2:80;

}

upstream backend {

ip_hash;

server webserver1:80;

server webserver2:80;

}

Page 17: Load Balancing and Scaling with NGINX

Managing the Upstream Group

• Direct config editing:

– nginx –s reload

– upstream.conf file:

• On-the-fly Reconfiguration [NGINX Plus only]

upstream backend {

server webserver1:80;

server webserver2:80;

server webserver3:80;

server webserver4:80;

}

$ curl 'http://localhost/upstream_conf?upstream=backend&id=3&down=1'

Page 18: Load Balancing and Scaling with NGINX

2. When you need more control…

• In many scenarios, you want more control over where traffic is routed to:

– Primary and secondary servers (aka master/slave)

– Transaction state is accumulated on one server

Page 19: Load Balancing and Scaling with NGINX

Internet

‘Master’ and ‘Slave’ servers

• Wordpress admin traffic (e.g. image uploads)

N

‘Master’

‘Slave’

Copy image uploads from master to slave

Page 20: Load Balancing and Scaling with NGINX

‘Master’ and ‘Slave’ servers

• Wordpress admin traffic (e.g. image uploads)

N

server {

listen 80;

location ~ ^/(wp-admin|wp-login) {

proxy_pass http://wpadmin;

}

}

upstream wpadmin {

server server1:80;

server server2:80 backup;

}

‘Master’

‘Slave’

Page 21: Load Balancing and Scaling with NGINX

Session Persistence [NGINX Plus only]

• For when transaction state is accumulated on one server– Shopping carts

– Advanced interactions

– Non-RESTful Applications

• NGINX Plus offers two methods: – sticky cookie

– sticky route“Session persistence also helps performance”

Page 22: Load Balancing and Scaling with NGINX

Advanced Techniques

• You can control load-balancing programmatically

– A|B Testing

– Migration between applications

Page 23: Load Balancing and Scaling with NGINX

A|B Testing

Internet

N

‘backends’ upstream group

Test server

95%

5%

Partition traffic. Send 5% to new application instance

Page 24: Load Balancing and Scaling with NGINX

A|B Testing

split_clients "${remote_addr}AAA" $servers {

95% backends;

5% 192.168.56.1:80;

}

server {

listen 80;

location / {

proxy_pass http://$servers;

}

}

Page 25: Load Balancing and Scaling with NGINX

Application Migration

Internet

N

‘backendsA’ upstream group

‘backendsB’ upstream group

Create new generation of applicationMigrate users from old to newPreserve sessions, no interruptions

Page 26: Load Balancing and Scaling with NGINX

Application Migrationmap $cookie_group $group {

~(?P<value>.+)$ $value;

default backendB; # The default upstream group

}

server {

listen 80;

location / {

add_header Set-Cookie "group=$group; path=/"

proxy_pass http://$group;

}

}

Page 27: Load Balancing and Scaling with NGINX

Three Load Balancing case studies

Basic Load Balancing with NGINX

When you need more control

Advanced techniques

1

2

3

Page 28: Load Balancing and Scaling with NGINX

Closing thoughts

• 37% of the busiest websites use NGINX

• Check out the load-balancing articles on nginx.com/blog

• Future webinars: nginx.com/webinars

Try NGINX F/OSS (nginx.org) or NGINX Plus (nginx.com)