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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

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

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay