SaaS Architecture & Startup Engineering · درس

موازنة الأحمال واكتشاف الخدمات

تعرّفوا إلى كيفية توزيع الواجهات الخلفية لخدمات SaaS لحركة المرور عبر عدة مثيلات باستخدام موازِنات الأحمال وفحوصات السلامة واكتشاف الخدمات.

الدرس 4 من 413 خطوة

موازنة الأحمال واكتشاف الخدمات درس مجاني في SaaS Architecture & Startup Engineering على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.

البدء مجانًا

تعلم SaaS Architecture & Startup Engineering مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «موازنة الأحمال واكتشاف الخدمات» مجاني؟

نعم — نص درس «موازنة الأحمال واكتشاف الخدمات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SaaS Architecture & Startup Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة SaaS Architecture & Startup Engineering 4 دروس في المجموع.

ماذا ستتعلم في «موازنة الأحمال واكتشاف الخدمات»؟

تعرّفوا إلى كيفية توزيع الواجهات الخلفية لخدمات SaaS لحركة المرور عبر عدة مثيلات باستخدام موازِنات الأحمال وفحوصات السلامة واكتشاف الخدمات. تتمرن على SaaS Architecture & Startup Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ SaaS Architecture & Startup Engineering؟

لا تُشترط خبرة سابقة. SaaS Architecture & Startup Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «موازنة الأحمال واكتشاف الخدمات»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس SaaS Architecture & Startup Engineering هذا؟

نعم. كل درس في SaaS Architecture & Startup Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تقنيات التوسع الأفقي
  2. قوائم الرسائل والبنى المعتمدة على الأحداث
  3. أساسيات البنية Serverless
  4. موازنة الأحمال واكتشاف الخدمات
← العودة إلى SaaS Architecture & Startup Engineering