Load Balancer

System DesignReliability & Scaling

Topic: Load Balancer

Presenter: 猴子


Sign Up Form:

QRCode

Customer Service for joining future events System design wechat group

System Design Summary

Load balancer

Load balancer + real server

Classification method

software/hardware

Client based:

URI

Selection of server such as games

Ribbon

L4 vs L7 level load balancer

Why do we need load balancer?

Highly concurrent access

High volume of data

OSI 7 levels

  1. Application

  2. Presentation layer

  3. Session layer

  4. Transport layer

  5. Network layer

  6. Data link layer

  7. Physical layer

If we need to send a rest request, it starts with the application layer, then it sends the request down to lower layers. The physical layer will send the request to the recipient.

Most application runs at layer 5, 6, 7.

Layer 1-4 are handled by the operating system.

Load balancing algorithms

Most simple is round robin

Layer 7 load balancer

Nginx, openresty, HAProxy

Layer 7 load balancer is feature rich

Load balancer can see URI

Management of connection: rate limiting, request throttling etc

Cache static resources

Upgrading of protocol, e.g. websocket (upgraded from HTTP). Security, traffic control

Sticky session

VIP sessions / Quality of service (QoS)

Audience:

Google supplied TCP socket (L4) load balancer breaking layer separation

Handshake

L7 -> returning traffic always go through LB

L4 -> returning traffic may or may not go through LB

Client -> DNS -> layer 7 LB -> real server

Load balancing strategy

Round robin

Weighted round robin

Ip hash

Least hops

L7 LB -> L7 reverse proxy

==

L4 load balancer

==

Basic of local network

MAC, IP address, and port

How to send data packet from one device to another device?

To get to internet, a device needs to join a local network.

MAC address: represents which next hop to send data packet to

ARP protocol

Translates from ip address to mac address

Each message contains:

Source IP

Dest IP

Source MAC

Dest MAC

Messages are broadcasted to every node on ethernet if dest MAC == FFFFFFFF. The device with the wrong IP will drop the packet.

===

ifconfig ens33

Machine A

Ifconfig ens33:2 192.168.88.88

Machine B

route add -host 192.168.88.88 gw 192.168.150.12

Listing routing table

route -n

or

ip r

Route add -host 192.168.88.88 gw 192.168.150.12

===

How does a packet forwarded by a router

Start from routing table

Destination: 192.168.3.0

Gateway: 192.168.11

Mask: 255.255.255.0

Sending packet:

Contains IP address of source and destination

Mac address of source and router

Routing packet (by router)

Contains IP address of source and destination

Mac address of router and destination

===

NAT (network address translation

Src: 192.168.1.2: 22222

Dst: 6.6.6.6: 80

Router may receive the same packet from 2 different machines of local machines. It maps each source IP:port pair to its own IP:port pair

D-NAT

Load balancer: maps public IP:port to multiple internal IP:port pairs

Disadvantage of D-NAT

we must send the returning traffic through the load balancer

Incoming traffic’s IP address will be visible to the

IP encapsulation

Wrap the original header into payload

We need VIP setting

==

Direct routing (DR) model

Listen to Virtual IP (VIP), 6.6.6.6

Level 2: LB makes a small change: Change the MAC to real server’s MAC

Packet reaches destination

All destination machines must use the same IP address 6.6.6.6

However this introduces a problem: multiple machines on the same network has 6.6.6.6 as IP address

Linux has a loopback device lo

Connect to 127.0.0.1 will direct reach the local services

We can put VIP(6.6.6.6) onto the loop back device lo

Need to change linux core to silence VIP address

arp_ignore

arp_announce

Mac spoofing

Biggest problem: single point of failure

Highest eff