0Pricing
SaaS Architecture & Startup Engineering · Lezione

Bilanciamento del carico e service discovery

Impari come i backend SaaS distribuiscono il traffico tra molte istanze usando load balancer, health check e service discovery.

Bilanciamento del carico e service discovery è una lezione SaaS Architecture & Startup Engineering gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento SaaS Architecture & Startup Engineering, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso SaaS Architecture & Startup Engineering include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

The Need for Load Balancing

When one server cannot handle all traffic, you run many copies. A load balancer sits in front and spreads incoming requests across these instances.

This is the backbone of horizontal scaling for SaaS backends.

How a Load Balancer Works

Clients connect to a single address. The load balancer accepts the request and forwards it to one of the backend servers, then relays the response back.

To the client, the cluster looks like one powerful server.

Round Robin

The simplest algorithm is round robin: requests are handed to servers in rotation. Each server gets an equal share.

const servers = ['s1', 's2', 's3'];
let i = 0;
function next() {
  const s = servers[i % servers.length];
  i++;
  return s;
}
console.log(next(), next(), next(), next());

Least Connections

Least connections routes each new request to the server currently handling the fewest active connections.

This adapts better than round robin when requests have uneven durations.

Layer 4 vs Layer 7

Load balancers operate at different network layers:

  • Layer 4 (transport) — routes by IP and port, very fast
  • Layer 7 (application) — inspects HTTP, can route by URL path or headers

Layer 7 enables smart routing like sending /api to one pool and /static to another.

Health Checks

A load balancer must avoid sending traffic to dead servers. It runs periodic health checks against each instance.

A common pattern is a /health endpoint returning 200 OK when the app is ready.

app.get('/health', (req, res) => {
  res.status(200).json({ status: 'ok' });
});

Sticky Sessions

Some apps store session state in server memory. Sticky sessions pin a client to the same server so their session persists.

Better practice: keep servers stateless and store sessions in a shared store, so any server can handle any request.

Service Discovery

In dynamic environments, servers come and go constantly. Service discovery keeps an up-to-date registry of which instances exist and are healthy.

Tools like Consul, etcd, or Kubernetes services automate this.

Client-Side vs Server-Side Discovery

Two models:

  • Server-side — clients hit a load balancer that consults the registry
  • Client-side — clients query the registry directly and pick an instance themselves

Server-side is simpler; client-side reduces a network hop.

Autoscaling Integration

Load balancers pair with autoscaling: when traffic rises, new instances spin up, register with discovery, and the balancer starts routing to them automatically.

When traffic drops, instances are removed gracefully after draining connections.

Graceful Draining

Before shutting down an instance, the balancer should stop sending new requests but let existing ones finish. This is connection draining.

It prevents dropped requests during deployments and scale-down events.

Quick Check

Test your load balancing knowledge.

Recap

You learned how SaaS backends distribute traffic:

  • Load balancers with round robin and least connections
  • Layer 4 vs Layer 7 routing and health checks
  • Stateless design, service discovery, and graceful draining

Together these let a backend scale horizontally and survive failures.

Domande Frequenti

La lezione «Bilanciamento del carico e service discovery» è gratuita?

Sì — il testo completo di «Bilanciamento del carico e service discovery» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso SaaS Architecture & Startup Engineering, passa a CoddyKit PRO. Il corso SaaS Architecture & Startup Engineering include 4 lezioni in totale.

Cosa imparerò in «Bilanciamento del carico e service discovery»?

Impari come i backend SaaS distribuiscono il traffico tra molte istanze usando load balancer, health check e service discovery. Eserciti SaaS Architecture & Startup Engineering con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare SaaS Architecture & Startup Engineering?

Non è richiesta alcuna esperienza precedente. SaaS Architecture & Startup Engineering su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Bilanciamento del carico e service discovery»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione SaaS Architecture & Startup Engineering?

Sì. Ogni lezione SaaS Architecture & Startup Engineering include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Tecniche di scalabilità orizzontale
  2. Code di messaggi e architetture event-driven
  3. Nozioni di base sull'architettura serverless
  4. Bilanciamento del carico e service discovery
← Torna a SaaS Architecture & Startup Engineering