Node.js Backend Development Bootcamp · Ders

İş Kuyrukları, Önceden Getirme ve Rekabet Eden Tüketiciler

Önceden getirme sınırları ve rekabet eden tüketiciler kalıbıyla yükü tüketiciler arasında adil biçimde dağıtın.

4. ders / 413 adım

İş Kuyrukları, Önceden Getirme ve Rekabet Eden Tüketiciler, CoddyKit'te ücretsiz bir Node.js Backend Development Bootcamp dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Node.js Backend Development Bootcamp öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Node.js Backend Development Bootcamp kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Why Work Queues?

A work queue (sometimes called a task queue) lets your Node.js backend push time-consuming jobs to a queue instead of running them inside the HTTP request. Think image resizing, sending emails, or generating PDFs.

  • The producer publishes a small message describing the job.
  • One or more consumers (worker processes) pull jobs and execute them in the background.
  • The HTTP response returns immediately, keeping your API fast.

RabbitMQ stores the messages durably until a worker is free to process them.

A Minimal Producer

The producer connects to RabbitMQ, declares a durable queue, and sends a task message. Each message is just a buffer of bytes — here we send a plain string describing the job.

Notice { durable: true } on the queue and { persistent: true } on the message. Together they let tasks survive a broker restart.

const amqp = require('amqplib');

async function send(task) {
  const conn = await amqp.connect('amqp://localhost');
  const ch = await conn.createChannel();
  const queue = 'tasks';

  await ch.assertQueue(queue, { durable: true });
  ch.sendToQueue(queue, Buffer.from(task), { persistent: true });
  console.log('Sent: %s', task);

  await ch.close();
  await conn.close();
}

send('resize-image:42');

A Minimal Consumer

The consumer asserts the same durable queue and registers a callback with ch.consume. Whenever a message arrives, RabbitMQ delivers it to the handler.

Here we simulate work by counting the dots in the message body, then sleeping that many seconds. The queue name is the contract that links producer and consumer.

const amqp = require('amqplib');

async function worker() {
  const conn = await amqp.connect('amqp://localhost');
  const ch = await conn.createChannel();
  const queue = 'tasks';

  await ch.assertQueue(queue, { durable: true });
  console.log('Waiting for tasks...');

  ch.consume(queue, (msg) => {
    const body = msg.content.toString();
    console.log('Received: %s', body);
  });
}

worker();

Acknowledgements Prevent Lost Work

By default, manual acknowledgements protect you from losing jobs if a worker crashes. With { noAck: false }, RabbitMQ keeps a message in an unacknowledged state until the worker calls ch.ack(msg).

  • If the worker dies before acking, RabbitMQ requeues the message and another worker picks it up.
  • Always ack after the work succeeds, never before.
  • Use ch.nack(msg, false, requeue) to reject a poisoned message.
ch.consume(queue, async (msg) => {
  try {
    await doWork(msg.content.toString());
    ch.ack(msg);            // success: remove from queue
  } catch (err) {
    ch.nack(msg, false, false); // failure: drop (or send to DLX)
  }
}, { noAck: false });

Competing Consumers Pattern

The competing consumers pattern simply means: run multiple worker processes that all consume from the same queue. RabbitMQ delivers each message to exactly one of them.

  • Want more throughput? Start more workers — no code change needed.
  • The queue acts as a load buffer during traffic spikes.
  • This is how you scale background processing horizontally in Node.js.

But how RabbitMQ chooses which worker gets the next message matters a lot.

The Problem: Round-Robin Dispatch

Out of the box, RabbitMQ dispatches messages to consumers in a strict round-robin order — it counts messages, not workload.

Imagine two workers. The queue has tasks where odd-numbered ones are heavy and even-numbered ones are light. Round-robin sends every other task to each worker, so one worker may end up with all the heavy jobs while the other sits idle.

RabbitMQ does not look at how busy a consumer already is — unless you tell it to with prefetch.

Prefetch: Limit Unacked Messages

Prefetch (QoS) caps how many unacknowledged messages a single consumer may hold at once. You set it per channel with ch.prefetch(count).

  • ch.prefetch(1) means: don't give a worker a new task until it has acked the previous one.
  • This turns dispatch from "round-robin by count" into "give the next task to whoever is free" — fair dispatch.
  • Higher values (e.g. 10) increase throughput by pipelining, at the cost of fairness.
const amqp = require('amqplib');

async function worker() {
  const conn = await amqp.connect('amqp://localhost');
  const ch = await conn.createChannel();
  const queue = 'tasks';

  await ch.assertQueue(queue, { durable: true });
  await ch.prefetch(1); // fair dispatch: one in-flight task per worker

  ch.consume(queue, async (msg) => {
    await doWork(msg.content.toString());
    ch.ack(msg);
  }, { noAck: false });
}

worker();

Tuning the Prefetch Value

The right prefetch depends on your workload:

  • prefetch(1) — best for long, uneven, CPU-heavy tasks. Maximum fairness, slight latency between ack and next delivery.
  • prefetch(10–50) — best for short, uniform tasks (e.g. tiny webhook fan-out) where round-trip latency would otherwise dominate.

Rule of thumb: short tasks + low latency network → larger prefetch. Long tasks or unpredictable durations → small prefetch. Measure, then tune.

Simulating Fair Dispatch Locally

You don't need RabbitMQ to understand fair dispatch. This standalone program models a single shared queue and two workers under a prefetch=1 policy: each worker only takes a new job when it is free.

Heavy jobs take longer, so the free worker keeps grabbing the next task — the load self-balances instead of being split blindly by count.

const tasks = [5, 1, 5, 1, 5, 1]; // seconds of "work"
let clock = 0;
const workers = [
  { name: 'W1', freeAt: 0 },
  { name: 'W2', freeAt: 0 },
];

for (const cost of tasks) {
  // prefetch=1: assign to whichever worker is free soonest
  const w = workers.reduce((a, b) => (a.freeAt <= b.freeAt ? a : b));
  const start = Math.max(clock, w.freeAt);
  w.freeAt = start + cost;
  console.log(`${w.name} runs ${cost}s at t=${start}`);
}

const finish = Math.max(...workers.map((w) => w.freeAt));
console.log('All done at t=' + finish);

Prefetch Is Per-Channel, Per-Consumer

A common gotcha: ch.prefetch(count) applies to each consumer on that channel by default. If you run one worker process per queue, this is exactly what you want.

  • The limit counts only unacked messages, so forgetting to ack will stall the worker once it hits the cap.
  • A second optional argument, ch.prefetch(count, true), makes the limit apply across the whole channel (global), which is rarely needed.
  • One channel per worker process is the simplest, safest setup.

Putting It Together

A production-ready consumer combines all the pieces: a durable queue, fair-dispatch prefetch, manual acks on success, and nack on failure.

Run several copies of this exact file as separate processes and you have competing consumers that share load fairly. Scale up by launching more workers; scale down by stopping some — the queue absorbs the difference.

const amqp = require('amqplib');

async function start() {
  const conn = await amqp.connect('amqp://localhost');
  const ch = await conn.createChannel();
  const queue = 'tasks';

  await ch.assertQueue(queue, { durable: true });
  await ch.prefetch(1);

  ch.consume(queue, async (msg) => {
    try {
      await handle(JSON.parse(msg.content.toString()));
      ch.ack(msg);
    } catch (err) {
      ch.nack(msg, false, false); // drop / route to dead-letter exchange
    }
  }, { noAck: false });
}

start().catch(console.error);

Quick Check

You run 3 worker processes consuming the same queue. Task durations vary wildly — some take 200ms, some take 30s. You notice one worker is always busy while others go idle, so jobs pile up. What is the most direct fix?

Recap

You learned how to distribute background work fairly across Node.js workers with RabbitMQ:

  • Work queues offload slow jobs from the request path into a durable queue.
  • Competing consumers means multiple workers consume the same queue; each message goes to exactly one worker, and you scale by adding processes.
  • Default dispatch is round-robin by count, which can overload one worker with heavy tasks.
  • Prefetch (ch.prefetch(n)) caps unacked messages per consumer; prefetch(1) gives fair dispatch, higher values pipeline short tasks.
  • Always use manual acks (ch.ack on success, ch.nack on failure) so crashed workers don't lose jobs.
Başlamak ücretsiz

Yapay zeka eğitmeniyle JavaScript öğren — ücretsiz

Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.

Kurslar
22
Dersler
92

Sıkça Sorulan Sorular

“İş Kuyrukları, Önceden Getirme ve Rekabet Eden Tüketiciler” dersi ücretsiz mi?

Evet — “İş Kuyrukları, Önceden Getirme ve Rekabet Eden Tüketiciler” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Node.js Backend Development Bootcamp kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Node.js Backend Development Bootcamp kursu toplamda 4 dersten oluşur.

“İş Kuyrukları, Önceden Getirme ve Rekabet Eden Tüketiciler” dersinde ne öğreneceğim?

Önceden getirme sınırları ve rekabet eden tüketiciler kalıbıyla yükü tüketiciler arasında adil biçimde dağıtın. Node.js Backend Development Bootcamp ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Node.js Backend Development Bootcamp öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Node.js Backend Development Bootcamp, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“İş Kuyrukları, Önceden Getirme ve Rekabet Eden Tüketiciler” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Node.js Backend Development Bootcamp dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Node.js Backend Development Bootcamp dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Üreticiler, Tüketiciler ve AMQP Modeli
  2. Değiş Tokuş Türleri: Doğrudan, Konu, Yayın ve Üst Bilgiler
  3. Onaylar, Ölü İleti Kuyrukları ve Yeniden Denemeler
  4. İş Kuyrukları, Önceden Getirme ve Rekabet Eden Tüketiciler
← Node.js Backend Development Bootcamp Sayfasına Dön