Load balancing

What is the load balancing?

Load balancing is the process of distributing tasks over resources (a set of computing nodes) to improve the performance and reliability of the system.

A load balancer can be a hardware or software system, and it has implications for security, user sessions, and caching.

Types:

  • Application Load Balancer = OSI L7 application layer. Has access to application content → HTTP and HTTPS. Path, query string, header, request method.
  • Network Load Balancer = OSI L4, transport layer, TCP and UDP. could be a hardware load balancer.
    • DNS load balancing = directing clients to different IP addresses associated with a single hostname, based on a set of predefined rules.

Advantages of load balancing

  1. Enable horizontal scaling
  2. Dynamic scaling depending on load on system
  3. Throughput: service availability and response time are unaffected by overall traffic.
  4. Redundancy: distributing load over a cluster means no one server is a single point of failure. LB itself should become the single point of failure.
  5. Continuous deployment: changing version of system

Algorithms for implementing load balancing

image
  1. Random
  2. Round Robin — redirect by order
  3. Weighted Round Robin — take into consideration the load of server
  4. Based on Load — smart algorithm that uses health check, if the health of server is not good, then requests will not be locked
  5. Sticky session load balancer — kind of IP based, caching is used
  6. Path based, based of URL — if payment url then request will go to payment server, if GET method requests are coming the redirect to different server.
  7. Based on HTTP headers, query parameter, ports.
  8. Sub-domain: You can also design your application in ways that help you split traffic to different sub domains, thereby reducing the number of requests 1 LB has to handle.
  9. It is also possible to implement a round-robin DNS, where you would have 1 DNS entry point to several client facing LBs instead of just one as you've depicted. 1 domain to many IPs. Disadvantage is that if LB fails, users still will be redirected.

Elastic Load Balancer

Load balancer automatically scales out itself to handle an increase in traffic to your application.

Round Robin DNS

Many IP addresses point to 1 domain name. Like in google.com.

This is round robin DNS. This is a quite simple solution for load balancing. Usually DNS servers rotate/shuffle the DNS records for each incoming DNS request. Unfortunately it's not a real solution for fail-over. If one of the servers fail, some visitors will still be directed to this failed server.

But we can use Round Robin DNS to overcome load balancers load.

https://stackoverflow.com/questions/36771929/do-load-balancers-flood

Load balancer & Authentication

JWT → make expiration time smaller → use 1 database to save auth tokens → BUT if you have many regions that is HELL, can you keep the data synced? → NO

Sticky session

  • Send requests of 1 user to the same server
  • Cons:
    • The load will be uneven across your servers. Some users will spend more time than others on your website.
    • The load balancer will not read and process the request, that’s not its job. Thus, the only way that it can identify where that request is coming from is by IP and I don’t think that I need to tell you how many people use their mobile phone data and then switch to wi-fi in a café. This results in losing the session. To put it shortly: inconsistency

Distributed sessions state

  • distributed cache + session state = distributed sessions
  • Distributed cache is simply cache that is accessible by multiple parties. It offers a layer of consistent data between all of your servers.
    • Server looks for some information, if it’s cached it uses it and end of story
SuperMade with Super