Skip to content
LATEST

🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.

Upgradix
Upgradix Code. Learn. Ship.

Message Durability, Persistence & Publisher Confirms

To prevent message loss during a RabbitMQ restart, you need three things together: durable exchanges and queues, persistent messages written to disk, and Publisher Confirms so your C# app knows the broker safely saved the message.

Message Durability, Persistence & Publisher Confirms

Quick Answer: To prevent message loss during a RabbitMQ restart, you need three things together: durable exchanges and queues, persistent messages written to disk, and Publisher Confirms so your C# app knows the broker safely saved the message.

Imagine Meena runs a UPI payment app in Hyderabad. At 7:30 PM, Karthik pays 2,500 rupees for his electricity bill.

Meena's Payment API pushes a settlement message to RabbitMQ and shows "Payment Received" to Karthik. Two seconds later, the RabbitMQ server loses power and restarts.

When the broker comes back up, the queue is empty. Karthik's money is debited, but his bill is never paid. How do we make sure messages survive a broker crash?

1. Why Do Messages Disappear on Restart?

The problem without durability and confirms

By default, RabbitMQ keeps queues and messages in fast RAM (memory) for speed. It also uses fire-and-forget publishing, so your C# code never checks if the broker actually saved the data.

Example 1: Unsafe default publishing in C#

This works until the broker restarts or the network drops a packet.

// Unsafe: durable is false, message is not persistent, no confirm check
await channel.QueueDeclareAsync("q.upi.settlements", durable: false, exclusive: false, autoDelete: false);

await channel.BasicPublishAsync(
    exchange: string.Empty,
    routingKey: "q.upi.settlements",
    body: Encoding.UTF8.GetBytes("{\"txnId\":\"UPI-9921\",\"amount\":2500}"));

What happens when RabbitMQ restarts

Payment API: Published UPI-9921 (assumed success)
RabbitMQ   : Power cut -> Restarting...
RabbitMQ   : Queue q.upi.settlements is GONE. 0 messages recovered.

This setup works fine on a developer laptop. However, it fails silently in production.

  1. Non-durable queues vanish completely when RabbitMQ restarts.
  2. Transient (in-memory) messages are wiped out even inside a durable queue.
  3. Without Publisher Confirms, the Payment API has no idea the broker crashed before saving the message.

The solution: Durability + Persistence + Publisher Confirms

Durable Queue / Exchange = a broker setting that saves the queue and exchange definition to disk so they survive restarts.

Persistent Message = a message marked with DeliveryModes.Persistent so RabbitMQ writes its payload to disk.

Publisher Confirms = a lightweight broker acknowledgment (Basic.Ack) sent back to the C# producer after the message is safely stored.

Live Flow: Disk Persistence & Publisher Confirm Round-Trip

Watch Karthik's UPI payment get written to disk, confirmed back to C#, and survive a broker restart.

Step 1: Payment API publishes persistent message
Meena's Payment API (.NET)
Karthik's UPI-9921 · Confirms Enabled
Publish (Persistent)
Basic.Ack (Saved!)
q.upi.settlements
Durable Queue · Survives Reboot
Broker Disk (fsync)
UPI-9921 written safely to SSD
C# Producer Durable Queue / ACK Disk Storage Active Step

RabbitMQ only sends Basic.Ack after the persistent message is safely flushed to disk. If the broker restarts later, q.upi.settlements reloads UPI-9921 from disk.

2. How to Configure Durability & Confirms in C#

In RabbitMQ.Client v7+, you enable Publisher Confirms directly when creating the channel using CreateChannelOptions.

Example 2: Durable queue, persistent message, and async Publisher Confirms

BasicPublishAsync automatically awaits the broker's confirm ACK when tracking is enabled.

var options = new CreateChannelOptions(
    publisherConfirmationsEnabled: true,
    publisherConfirmationTrackingEnabled: true);

await using var channel = await connection.CreateChannelAsync(options);

// 1. Declare a durable exchange and durable queue
await channel.ExchangeDeclareAsync("upi.exchange", ExchangeType.Direct, durable: true);
await channel.QueueDeclareAsync("q.upi.settlements", durable: true, exclusive: false, autoDelete: false);
await channel.QueueBindAsync("q.upi.settlements", "upi.exchange", "settlement");

// 2. Mark the message as persistent
var props = new BasicProperties { DeliveryMode = DeliveryModes.Persistent };
var body = Encoding.UTF8.GetBytes("{\"txnId\":\"UPI-9921\",\"amount\":2500}");

// 3. Publish and await broker confirmation (throws if Nack'd)
await channel.BasicPublishAsync(
    exchange: "upi.exchange",
    routingKey: "settlement",
    mandatory: true,
    basicProperties: props,
    body: body);

Expected output

Payment API: Published UPI-9921 (Persistent)
RabbitMQ   : Flushed to disk -> Sent Basic.Ack
Payment API: Confirm received! Safe to reply 200 OK to Karthik.

If RabbitMQ cannot save the message or runs out of disk space, it sends a Basic.Nack and BasicPublishAsync throws an exception so Meena's API can retry.

3. Default Publishing vs. Durable + Confirmed Publishing

Unsafe default setup vs. durable confirmed setup
Default (In-Memory) Durable + Persistent + Confirms
Fastest throughput, zero disk I/O. Slightly higher latency due to disk sync.
Messages lost on broker restart. Messages survive broker restarts safely.
Producer never knows if broker saved it. Producer gets explicit Basic.Ack proof.

The important terms to remember

  • Durable = exchange and queue metadata survives a broker restart.
  • Persistent = message body is written to disk, not just RAM.
  • Publisher Confirms = broker acknowledgment confirming safe receipt of a published message.

Watch out: Setting durable: true on a queue does NOT automatically make messages persistent. You must set DeliveryModes.Persistent on every critical message.

4. Common Mistakes to Avoid

1. Enabling durability without Publisher Confirms

Even with a persistent message, RabbitMQ might crash in the millisecond before flushing it to disk. Without confirms, your C# app thinks it succeeded.

Better approach: Always pair persistent messages with Publisher Confirms for financial or order data.

2. Awaiting confirms one-by-one in a tight loop

Publishing 10,000 messages and awaiting each confirm one at a time adds 10,000 network round-trips.

Better approach: Publish messages in small batches (e.g., 50–100 tasks) and await them together with Task.WhenAll.

5. Interview Questions & Practice

Question 1: Does setting durable=true on a queue guarantee zero message loss?

What the interviewer expects: Knowing that durability alone is not enough.

Strong answer: "No. A durable queue only survives a restart itself. To keep Karthik's UPI payment safe, we must also mark the message as Persistent and enable Publisher Confirms so the Payment API waits until RabbitMQ confirms the disk write."

Practice: Railway ticket booking confirmation

A railway portal publishes seat-booking messages to q.ticket.bookings. List the three C# settings you must turn on so no paid ticket is lost during a midnight broker reboot.

Expected result: durable: true on exchange/queue, DeliveryModes.Persistent on message properties, and publisherConfirmationsEnabled: true on the channel.

6. Next Steps

  • Previous Article: Request-Reply (RPC) Pattern with Correlation IDs in C#
  • Next Article: Manual Acknowledgments & Consumer Prefetch (BasicQos)

Discussion & Comments

0
Guest mode · Login / Register
Post directly as a guest or log in first.

No comments yet

Start the discussion above — ask a question, share a tip, or reply to a comment.

This article is protected. Printing and saving as PDF are disabled.