DEV Community

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

Posted on

3

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

Sentry blog image

Identify what makes your TTFB high so you can fix it

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

Read more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay