Message queues

Don't keep them waiting · Part of Architecture

Key idea

Practice

8 questions · about 4 min

Explained step by step

Message queue

A message queue is a way to connect services asynchronously: it receives messages from producers and passes them to consumers when they can take them. In a small system, storing something always takes about the same time; in a big one, it can take 10 ms or 10 seconds depending on load. The queue absorbs that difference. It's like the order ticket in a kitchen: the waiter leaves the slip and keeps serving, and the cook makes the dishes in order.

A queue brings 4 advantages. Scalability: at a peak, many copies of the app can drop requests into the queue without colliding. Decoupling: components don't depend on each other. Performance: producers and consumers talk to the queue, not to each other, and nobody waits. Reliability: data stays stored even if part of the system goes down. It's like a mailbox: the mail carrier leaves letters even if you're out, and you read them when you're back.

Producer

Messages stay in the queue until they're processed and deleted. The producer publishes a job to the queue and tells the user its status. A consumer takes the job, processes it and signals it's done. Each message is processed only 1 time by 1 single consumer. It's like a bank line: each customer is served by 1 single teller, and then leaves the line.

Pull

There are 2 ways for a consumer to get messages. Pull: the consumer asks the queue again and again if there's anything new. Push: the queue notifies the consumer when a message arrives. With long polling, the pull waits a while, for example 20 seconds, for something to arrive before returning empty. It's like mail: pull is checking the mailbox every hour; push is the mail carrier ringing your bell.

At-least-once

Queues give different delivery guarantees. At-least-once: the queue keeps copies and retries if something fails, so a message can arrive 2 times. Exactly-once: FIFO queues filter duplicates and each message arrives 1 single time. FIFO also guarantees order: first in, first out. Most queues only try to keep order, without guaranteeing it. It's like sending a registered letter twice just in case: it surely arrives, but maybe 2 arrive.

Dead-letter queue

A dead-letter queue is a separate queue where messages that couldn't be processed go. That way they don't block the main queue or burn CPU on a message that may never succeed, and someone can review them later. A poison pill is something else: a special message received on purpose to tell a consumer to stop working. It's like the post office: letters with no recipient go to a separate office for review, and don't hold up the rest.

Task queue

A task queue receives tasks with their data, runs them and delivers the result. It's used for heavy background jobs, like converting a video, and can schedule them for later. Many queues let you set when a message is delivered, for example sending a reminder in 24 hours. It's like leaving clothes at the dry cleaner: you drop them off, leave, and they let you know when they're ready.

Back pressure

If the queue grows a lot, it may not fit in memory and start reading from disk, which is much slower. Back pressure limits the queue size so jobs already in it get processed fast. When it fills up, clients get «server busy» (HTTP 503) and retry later, ideally with exponential backoff: waiting 1, 2, 4, 8 seconds. It's like a club with a capacity limit: when it's full, people wait outside instead of squeezing in.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Don't keep them waiting