Free preview

Backend Service: Two Replication Models

Why this matters: this is the stateful tier — the one that actually holds messages, and therefore the one where durability is won or lost. The two models differ on a single question: is there a named owner for each queue?

Key takeaway

The backend service handles message storage, routing, and replication. The frontend queries the metadata service to find which broker is responsible for the queue, forwards the message there, and it is replicated to other nodes to improve availability and durability.

Internal versus external cluster managers

Before the models, the two kinds of manager that coordinate them:

Internal cluster managerExternal cluster manager
Scope of assignmentManages the assignment of queues within a clusterManages the assignment of queues across clusters
What it knowsKnows about each and every node within a clusterKnows about each cluster, but has no information on every host inside a cluster
HealthListens to the heartbeat from each nodeMonitors the health of each independent cluster
Failure handlingManages host failure, instance addition, and removals from the clusterManages and utilizes clusters
PartitioningPartitions a queue into several parts, each part getting a primary serverMay split a queue across several clusters, so messages for the same queue are equally distributed between clusters

Model 1: Primary-secondary

Each node acts as the primary host for a specific set of queues. The primary receives requests for its assigned queues and manages data replication. The frontend consults the metadata service to identify the correct primary host for each request.

The worked example: queues 101 and 102 are assigned across four hosts (A, B, C, D). If host B is primary for queue 101, it replicates the queue data to secondary hosts such as A and C. When the frontend receives a request, it queries the metadata service — backed by the internal cluster manager — to determine the primary host. The primary serves the read request and, after receiving an acknowledgment from the consumer, deletes the message and its replicas.

The internal cluster manager maps queues to primary and secondary hosts and assists in primary host selection. This component must be reliable, scalable, and performant.

Model 2: A cluster of independent hosts

Multiple clusters of independent hosts are distributed across data centers. When the frontend receives a message, it queries the metadata service — backed by the external cluster manager — to identify the correct cluster. The message is forwarded to a random host within that cluster, which then replicates it to other hosts storing that queue.

How does a host know where to replicate?

Each host consists of a mapping between queues and hosts within a cluster, making replication easier.

Take cluster Y with hosts A, B, C, holding queues 101 and 103 on different hosts. This table is stored on each host within cluster Y. When a random host — say C — receives a message for queue 103, C replicates it to the other hosts where queue 103 is stored: A and B.

Cluster Y  (this table lives on every host in the cluster)

Queue ID | Hosts
---------|-------------------
   101   | A, C
   103   | A, B, C

A similar process applies to consumer requests: the randomly selected host handles message delivery and cleans up the message after successful processing.

This model introduces an external cluster manager to map queues to specific clusters. It handles queue management and assigns queues to appropriate clusters. The frontend routes traffic to the appropriate cluster, where nodes handle storage and delivery.

Synchronous versus asynchronous replication

"How is message replication handled in distributed queue systems?"

SynchronousAsynchronous
SequenceThe primary replicates the message to all relevant queues on other hosts, and only after receiving acknowledgment from secondaries does it notify the client of receiptOnce the primary receives the message it acknowledges the client, then replicates to other hosts
GainsMessages remain consistent across all queue replicasLower client-visible latency
CostsAdditional communication delay, and partial or no availability while an election is in progress to promote a secondaryReplication lag and consistency issues

Based on the application's needs, we can pick one or the other.

Key takeaway

Primary-secondary gives a named owner per queue — orderable, but unavailable during election. Independent clusters let any host accept a write — highly available and hotspot-free, but with no total order. Managers split hierarchically so neither one's state grows with the whole fleet. And for a queue, async replication can lose acknowledged messages silently.

Interview signal by level

LevelWhat a strong answer sounds like
L4"We replicate the queue across several servers so it survives a failure."
L5Contrasts the models: "primary-secondary gives each queue an owner that handles replication, or a cluster of independent hosts where any host takes the write and replicates from its local mapping table."
Staff+Names what each model buys and the durability trap: "a named primary is the only way to get a total order, because someone has to observe the whole sequence — but it costs availability during election. Independent hosts are leaderless: available, no hotspot, no ordering. And I'd replicate synchronously to at least one replica, because async here means an acked message can vanish with the primary, with the producer believing it succeeded and no error anywhere."

Next: checking the design against the requirements.

Enjoying the preview?

Create a free account to unlock the rest of this course, the in-browser judge, and live AI mock interviews.

Sign up free to continue