The load balancer

A load balancer is a type of HTTP server responsible for distributing traffic (routing) to various web servers based on the rules defined by the developer. A load balancer, in general, is a very fast and specialized application. Trying to implement similar logic in a web server might not be optimal because the resources available to your web server have to be split between handling requests for your business logic and requests that need to be routed. Also, a load balancer provides us with a lot of features out of the box such as these:

  • Load balancing algorithms: The following are some algorithms for load balancing:
    • Random: Distribute randomly across the servers.
    • Round robin: Distribute equally and sequentially across servers.
    • Asymmetric load: Distribute between servers in certain proportions. For example, for 100 requests, send 80 to Server A and 20 to Server B.
    • Least connections: Send a new request to the server with the least number of active connections (an asymmetric load can also be integrated with least connections).
  • Session persistence: Imagine an e-commerce site where a user has added items to his shopping cart and the information about items in the cart is stored on, Server A. However, when the user wants to complete the purchase, the request is sent to a different server, Server B! This would be an issue for the user because all details related to his shopping cart is on Server A. Load balancers have the provision to ensure that such requests are redirected to the relevant server.
  • HTTP compression: Load balancers also have the provision to compress the outgoing response using gzip so that it has less data to send to the user. This tends to greatly improve the user experience.
  • HTTP caching: For sites that serve more than REST API content, a lot of files can be cached because they do not change as often and cached content can be delivered much faster.

Depending on which load balancer is being used, they can provide a lot more features than the ones stated above. This should give an idea about the capability of a load balancer.

The following figure shows how a load balancer and multiple servers might work together:

The user's requests reach the load balancer, which then routes the request to one of the many instances of the blog server. However, note that even now we are using the same database for read and write operations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset