SaaS Architecture & Startup Engineering · レッスン

ロードバランシングとサービスディスカバリ

ロードバランサー、ヘルスチェック、サービスディスカバリを使って、SaaSのバックエンドが多数のインスタンスにトラフィックを分散する仕組みを理解します。

レッスン 4/413 ステップ

「ロードバランシングとサービスディスカバリ」はCoddyKit上の無料SaaS Architecture & Startup Engineeringレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSaaS Architecture & Startup Engineering学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SaaS Architecture & Startup Engineeringコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.

無料で開始

AI チューターと学ぶ SaaS Architecture & Startup Engineering — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「ロードバランシングとサービスディスカバリ」レッスンは無料ですか?

はい。「ロードバランシングとサービスディスカバリ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SaaS Architecture & Startup Engineeringコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SaaS Architecture & Startup Engineeringコースには全4レッスンが含まれています。

「ロードバランシングとサービスディスカバリ」で何を学びますか?

ロードバランサー、ヘルスチェック、サービスディスカバリを使って、SaaSのバックエンドが多数のインスタンスにトラフィックを分散する仕組みを理解します。 ブラウザで直接実行するハンズオンコードでSaaS Architecture & Startup Engineeringを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SaaS Architecture & Startup Engineeringを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSaaS Architecture & Startup Engineeringは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「ロードバランシングとサービスディスカバリ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSaaS Architecture & Startup Engineeringレッスンでコードを書いて実行できますか?

はい。すべてのSaaS Architecture & Startup Engineeringレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 水平スケーリングのテクニック
  2. メッセージキューとイベント駆動
  3. サーバーレスアーキテクチャの基礎
  4. ロードバランシングとサービスディスカバリ
← SaaS Architecture & Startup Engineeringに戻る