DEV Community

Cover image for Layer 4 V.S Layer 7 load balancing
Sameh Muhammed
Sameh Muhammed

Posted on

Layer 4 V.S Layer 7 load balancing

What is Load Balancing?

load balancing is a mechanism or strategy applied when you have an API or back end service that heavily used in the system so you can run multiple instances form that component and on top of them there is another service that distribute requests among of them.

Load Balancing Algorithms

Most popular two load balancing algorithms are IP Hashing and Round Robin

IP Hashing

Here the load balancer hashes the request and match the hash with one of services, whenever this request came again it will stick with same service

Image description

Round Robin

Here the requests distributed among service equally as image below

Image description

So, What is Layer 4 and Layer 7?

regarding to OSI (open system interconnection) Model the network model has 7 layers described as below image

Image description

So Layer 4 and Layer 7 we are talking about is transport layer and application layer in OSI model, let's talk about each one.

Layer 7 : Application Layer

Here your load balancer service will receive the whole request from the client and decide which service should receive this request and forward the request to it, This means that the load balancer service has full awareness of what is received and what is sent, Therefore as benefit from this you can enable caching for responses, Whenever got this request before it's not needed to forwarded the request to server just pull up the response from the cache and return it to client, also you can define here some URLs you don't want some users to access them so load balancer can act as security layer to prevent this access, But it also has a security wise if load balancer service hacked, Hacker can now know a lot of data about your system.

Layer 4 : Transport Layer

Here as OSI model describers the data packet is surrounded by source and destination addresses (IP address and port) as below image

Image description

So when you enable layer 4 load balancing, Your load balancer service will not wait until gather all packets and recognize the request, It will directly open a connection with one of your back end servers and send the packets to it, Therefore the load balancer has no awareness of what happen here it just act as transporter only, So you can't get benefit of caching or prevent some URLs from being processed in load balancing layer, but it also enhance the security of load balancer service as it's now don't have any information about your system and also it faster in packet transporting.

Image description

For more better explanation watch this video

IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.

Buy Me A Coffee

Top comments (0)