Service Bus Interview Questions and Answers Part 8 - Transactions and Reliable Messaging Patterns

Service Bus Interview Q&A · Part 8 of 10

In this post we are going to cover transactions and reliable messaging patterns in Azure Service Bus. These are 7 real Service Bus interview questions for data engineers, asked the way they come up on a real project, and the last one is a scenario: an order must reserve stock, take payment and book shipping, and roll back if any step fails.

A Service Bus transaction groups a few messaging operations so they all succeed or all fail. Reliable messaging patterns are what we build around it when one transaction isn't enough: the transactional outbox, request-reply, competing consumers and sagas. Interviewers ask about this because most real failures happen between two systems, like a database and a queue, and they want to know if you understand what is atomic and what is not.

Part 8 of the Real Time Azure Service Bus Interview Questions and Answers series on TechBrothersIT.

The short answer

Say this first

A Service Bus transaction groups send and settle operations on one entity (or via a transfer queue); it never includes a database, and the Python SDK has no transaction API. Reliability comes from patterns: a transactional outbox, idempotent consumers, request-reply with reply sessions, competing consumers, and sagas with compensations.

Let's set up the project

We run Microsoft's official Service Bus emulator locally with SQL Server, and small Python scripts that use the azure-servicebus client, so every output is real. Features the emulator doesn't support are shown as code. Config.json creates eight queues: order-placed and orders-saga with duplicate detection, price-requests, price-replies with sessions, invoices, and one command queue per saga service: stock, payment and shipping. peek.py peeks a queue from sequence number 1 and prints each message ID with its body.

Config.json
{
  "UserConfig": {
    "Namespaces": [
      {
        "Name": "sbemulatorns",
        "Queues": [
          {
            "Name": "order-placed",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT5M",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": true,
              "RequiresSession": false
            }
          },
          {
            "Name": "price-requests",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          },
          {
            "Name": "price-replies",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": true
            }
          },
          {
            "Name": "invoices",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          },
          {
            "Name": "orders-saga",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT5M",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": true,
              "RequiresSession": false
            }
          },
          {
            "Name": "stock",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          },
          {
            "Name": "payment",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          },
          {
            "Name": "shipping",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          }
        ],
        "Topics": []
      }
    ],
    "Logging": {
      "Type": "File"
    }
  }
}
peek.py
import os, sys
from azure.servicebus import ServiceBusClient

queue = sys.argv[1]
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_receiver(queue) as rx:
    peeked = rx.peek_messages(50, sequence_number=1)
print(f"{queue}: peek shows {len(peeked)}", *sorted(
    f"{m.message_id} {m}" for m in peeked), sep="\n  ")

Question 1: A consumer must receive a message and send follow-up messages as one unit. How do Service Bus transactions work?

A Service Bus transaction groups operations on one messaging entity, so they all succeed or all fail. Receive isn't part of it: you receive in peek-lock first, then open the transaction.

  • Inside a transaction - send, complete, abandon, dead-letter, defer, renew lock
  • Not inside - receive: receive in peek-lock, then open the transaction

To receive from one queue and send to another, .NET and Java send via a transfer queue, with cross-entity transactions turned on. Here is the C# pattern from Microsoft's docs: complete the order, send the invoice, commit.

OrderToInvoice.cs
// C# (.NET), the pattern from Microsoft's docs. No Python API.
var options = new ServiceBusClientOptions
    { EnableCrossEntityTransactions = true };
await using var client = new ServiceBusClient(connStr, options);

ServiceBusReceiver orders = client.CreateReceiver("orders");
ServiceBusSender invoices = client.CreateSender("invoices");
ServiceBusReceivedMessage msg = await orders.ReceiveMessageAsync();

using (var ts = new TransactionScope(
    TransactionScopeAsyncFlowOption.Enabled))
{
    await orders.CompleteMessageAsync(msg);
    await invoices.SendMessageAsync(new ServiceBusMessage("inv"));
    ts.Complete();                 // both happen, or neither
}

The limits are short:

Transaction limitValue
Timeout2 minutes
Messages per transaction100
Basic tiernot supported

Now the catch on Python: there is no transaction API. Let's check the SDK itself. check_tx.py lists every name with "transaction" on the package, the client, the sender and the receiver, and prints two method signatures. We run python check_tx.py.

check_tx.py
import inspect
from importlib.metadata import version
import azure.servicebus as sb

print("azure-servicebus", version("azure-servicebus"))
for obj in [sb, sb.ServiceBusClient, sb.ServiceBusSender,
            sb.ServiceBusReceiver]:
    hits = [n for n in dir(obj) if "transaction" in n.lower()]
    print(f"{obj.__name__}: names with 'transaction': {hits}")
for cls, name in [(sb.ServiceBusSender, "send_messages"),
                  (sb.ServiceBusReceiver, "complete_message")]:
    params = inspect.signature(getattr(cls, name)).parameters
    print(f"{name}{tuple(params)}")
Terminal
$ python check_tx.py
azure-servicebus 7.15.0
azure.servicebus: names with 'transaction': []
ServiceBusClient: names with 'transaction': []
ServiceBusSender: names with 'transaction': []
ServiceBusReceiver: names with 'transaction': []
send_messages('self', 'message', 'timeout', 'kwargs')
complete_message('self', 'message')

On azure-servicebus 7.15.0, every list is empty, []. And send_messages takes only 'message' and 'timeout', plus keyword arguments.

So in Python, we do three things:

  • Complete after the sends - succeed
  • Stable MessageId - + duplicate detection
  • Idempotent consumers - redelivery is normal
Real project

If you truly need a transaction for one step, that step runs in .NET or Java.

Question 2: A teammate asks whether a Service Bus transaction can include a database write. What do you tell them?

No. It covers Service Bus operations only. Microsoft says it plainly: code that calls databases or other services, like Cosmos DB, doesn't automatically enlist them in the same transactional scope. It is not a distributed transaction.

OperationIn a Service Bus transaction?
Send, complete, abandon, dead-letter, defer, renew lockYes: one entity, or send via a transfer queue
ReceiveNo: receive in peek-lock first
A database write, an HTTP callNo: not enlisted

Picture a consumer that writes a row and then completes the message. These are two separate commits:

  • If we commit, then complete fails: the message comes back and the row is written again
  • If we complete, then the commit fails: the message is gone, and the row is missing

You can't make the two atomic, so pick an order and make the other side safe:

  • Commit the work + its MessageId - one database transaction (an inbox table)
  • Then complete - a failure here means a redelivery
  • Redelivery finds the MessageId - skip the work, complete again

On the publisher side, it is the mirror problem: the database commits and the send fails. That is the transactional outbox, the next question.

The mistake I see

Wrapping a database call and Service Bus calls in one transaction scope, and assuming it's all or nothing. It isn't. Test it by killing the process between the steps.

Question 3: The order service writes to the database and then publishes, and sometimes the publish fails. How does the transactional outbox help?

It removes the dual write. The order service writes the order row and an outbox row in one database transaction, and never calls Service Bus. A separate relay publishes unsent rows and marks them sent only after the broker accepts them.

Here is the flow:

  • Order service - no Service Bus call
  • shop.db - order + outbox, 1 commit
  • Relay - publish, then mark sent
  • order-placed - duplicate detection

Let's run it. order_service.py commits orders 5001 and 5002 with their outbox rows. Then the relay runs with FAIL_ON=5002, which simulates a failed publish for 5002. Then the relay runs again, and we peek the queue. The commands are python order_service.py 5001 5002, FAIL_ON=5002 python relay.py, python relay.py and python peek.py order-placed.

order_service.py
import json, sqlite3, sys

db = sqlite3.connect("shop.db")
db.executescript("""
CREATE TABLE IF NOT EXISTS orders (order_id TEXT PRIMARY KEY,
                                   amount REAL);
CREATE TABLE IF NOT EXISTS outbox (message_id TEXT PRIMARY KEY,
                                   body TEXT, sent INT DEFAULT 0);""")
for order in sys.argv[1:]:
    event = (f"order-{order}-placed", json.dumps({"order": order}))
    with db:                     # ONE database transaction: both rows
        db.execute("INSERT INTO orders VALUES (?, ?)", (order, 49.90))
        db.execute("INSERT INTO outbox (message_id, body)"
                   " VALUES (?, ?)", event)
    print(f"order {order}: order row + outbox row committed")
relay.py
import os, sqlite3
from azure.servicebus import ServiceBusClient, ServiceBusMessage

fail_on = os.environ.get("FAIL_ON")        # simulate a failed publish
db = sqlite3.connect("shop.db")
rows = db.execute("SELECT message_id, body FROM outbox WHERE sent = 0"
                  " ORDER BY message_id").fetchall()
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_sender("order-placed") as sender:
    for message_id, body in rows:
        try:
            if fail_on and fail_on in message_id:
                raise ConnectionError("simulated publish failure")
            sender.send_messages(ServiceBusMessage(
                body, message_id=message_id))   # same id, every retry
        except ConnectionError as e:
            print(f"{message_id}: {e}, row stays unsent")
            continue
        with db:                            # only after the ack
            db.execute("UPDATE outbox SET sent = 1"
                       " WHERE message_id = ?", (message_id,))
        print(f"{message_id}: published, marked sent")
left = db.execute("SELECT COUNT(*) FROM outbox WHERE sent = 0")
print("unsent outbox rows:", left.fetchone()[0])
Terminal
$ python order_service.py 5001 5002
order 5001: order row + outbox row committed
order 5002: order row + outbox row committed
$ FAIL_ON=5002 python relay.py
order-5001-placed: published, marked sent
order-5002-placed: simulated publish failure, row stays unsent
unsent outbox rows: 1
$ python relay.py
order-5002-placed: published, marked sent
unsent outbox rows: 0
$ python peek.py order-placed
order-placed: peek shows 2
  order-5001-placed {"order": "5001"}
  order-5002-placed {"order": "5002"}

The first relay run publishes order-5001-placed, and order-5002-placed gets simulated publish failure, row stays unsent, so unsent outbox rows: 1. Nothing is lost. The next run publishes 5002 and marks it sent, unsent outbox rows: 0. Peek shows 2 messages, each once.

The relay can still crash between the publish and marking the row, and send again. So the message ID is the outbox ID, the queue has duplicate detection, and the consumer is idempotent.

Real project

Run the relay often and alert on old unsent rows. Microsoft's reference design uses the Cosmos DB change feed as the relay, with a Service Bus topic and duplicate detection.

Question 4: A service needs a reply to a request it sends through a queue. How does request-reply work with ReplyTo and sessions?

The requester sets reply_to, the queue for the answer, and a message ID. The responder sends its answer to that queue, with the correlation ID set to the request's message ID. That is the simple pattern.

With many requester instances sharing one reply queue, each needs only its own answers. So the reply queue is session enabled, each instance sets reply_to_session_id to its own name, and the responder copies it into the reply's session ID. price-replies requires sessions, and price-requests doesn't need them.

PropertySet byPurpose
reply_torequesterthe queue for the answer
message_idrequesteridentifies the request
correlation_idresponderthe request's message_id
reply_to_session_id → session_idrequester → responderroutes the answer to one caller

Let's try it. Two shop API instances ask for two prices with request.py. respond.py answers both on price-replies, one per session. Then each instance waits on its own session. We run python request.py, python respond.py, python await_reply.py shop-api-2 and python await_reply.py shop-api-1.

request.py
import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage

client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_sender("price-requests") as sender:
    for instance, sku in [("shop-api-1", "sku-42"),
                          ("shop-api-2", "sku-77")]:
        sender.send_messages(ServiceBusMessage(
            sku, message_id=f"req-{sku}",
            reply_to="price-replies",          # where to answer
            reply_to_session_id=instance))     # my own reply session
        print(f"{instance} sent req-{sku}")
respond.py
import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage

PRICES = {"sku-42": "19.99", "sku-77": "4.50"}
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_receiver("price-requests",
                                       max_wait_time=3) as rx:
    for m in rx.receive_messages(max_message_count=10):
        with client.get_queue_sender(m.reply_to) as sender:
            sender.send_messages(ServiceBusMessage(
                PRICES[str(m)],
                correlation_id=m.message_id,      # which request
                session_id=m.reply_to_session_id))  # which caller
        rx.complete_message(m)
        print(f"{m.message_id}: answered on {m.reply_to},"
              f" session {m.reply_to_session_id}")
await_reply.py
import os, sys
from azure.servicebus import ServiceBusClient

instance = sys.argv[1]
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_receiver(
        "price-replies", session_id=instance, max_wait_time=5) as rx:
    for m in rx.receive_messages(max_message_count=10):
        print(f"{instance} got {m} for {m.correlation_id}")
        rx.complete_message(m)
Terminal
$ python request.py
shop-api-1 sent req-sku-42
shop-api-2 sent req-sku-77
$ python respond.py
req-sku-42: answered on price-replies, session shop-api-1
req-sku-77: answered on price-replies, session shop-api-2
$ python await_reply.py shop-api-2
shop-api-2 got 4.50 for req-sku-77
$ python await_reply.py shop-api-1
shop-api-1 got 19.99 for req-sku-42

The responder answers req-sku-42 on session shop-api-1 and req-sku-77 on session shop-api-2. Shop API 2 receives from its own session and gets only its answer: 4.50 for req-sku-77. Shop API 1 gets 19.99 for req-sku-42.

In production

Use a unique reply session per instance, like a GUID, and wait with a timeout. A redelivery can produce a second answer, so the caller matches on correlation_id.

Question 5: Someone asks about the competing consumers pattern. How do you apply it?

Competing consumers means several instances of the same consumer read one queue. Each message goes to only one of them, because peek-lock hides a message from the others while it is being processed. You scale by adding instances, and the queue levels the load.

Let's run three. send_invoices.py sends 30 invoices to the invoices queue. workers.py 3 starts three workers that read it at once, each with its own client, because clients aren't thread safe. Prefetch is off, the Python default. We run python send_invoices.py and python workers.py 3.

send_invoices.py
import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage

client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_sender("invoices") as sender:
    sender.send_messages([ServiceBusMessage(f"invoice {i}")
                          for i in range(1, 31)])
print("sent 30 invoices")
workers.py
import os, sys, threading, time
from azure.servicebus import ServiceBusClient

done = {}                                   # worker -> invoices

def worker(name):
    client = ServiceBusClient.from_connection_string(
        os.environ["SERVICEBUS_CONNECTION_STRING"])  # one per thread
    done[name] = []
    with client, client.get_queue_receiver("invoices",
                                           max_wait_time=3) as rx:
        while batch := rx.receive_messages(max_message_count=1):
            for m in batch:
                time.sleep(0.2)             # process the invoice
                rx.complete_message(m)
                done[name].append(str(m))

n = int(sys.argv[1])
threads = [threading.Thread(target=worker, args=(f"w{i}",))
           for i in range(1, n + 1)]
for t in threads: t.start()
for t in threads: t.join()
work = [inv for invoices in done.values() for inv in invoices]
print(f"{n} competing consumers: {len(work)} invoices processed,",
      f"{len(set(work))} unique,",
      sum(1 for v in done.values() if v), "consumers got work")
Terminal
$ python send_invoices.py
sent 30 invoices
$ python workers.py 3
3 competing consumers: 30 invoices processed, 30 unique, 3 consumers got work

The result: 30 invoices processed, 30 unique, 3 consumers got work. No invoice was handled twice, and none was lost.

Two rules come with the pattern:

  • No order across consumers - sessions where order matters
  • Idempotent processing - a crashed worker's message moves on
Real project

Scale out until the database or the downstream API is the bottleneck, not the queue. And don't open thousands of receivers on one entity: Microsoft calls that an anti-pattern.

Question 6: A long business process spans several services. How do you coordinate it with messages?

With a saga. Each service does a local transaction, messages move the process forward, and if a later step fails, compensating steps undo the earlier ones. In choreography, services react to each other's events. In orchestration, one orchestrator sends commands and tracks the state.

ChoreographyOrchestration
Next step decided byeach service, reacting to eventsone orchestrator, sending commands
Process statespread across servicesin one place, per order
Good forshort flows, few servicesmany steps and compensations

Let's build an orchestrator. In orchestrator.py, a table maps each event to the next command: OrderPlaced means ReserveStock, StockReserved means TakePayment, PaymentTaken means BookShipping. The compensations sit in the same table. It keeps one state row per order in its own database, and it records every message ID it handled, so a repeated event is ignored. A small runner, run.py, pumps the four queues in turn, so the output stays in order. We place order 6001 for 120 in the EU and run it: python place_order.py 6001 120 eu and python run.py.

place_order.py
import os, sys
from azure.servicebus import ServiceBusClient, ServiceBusMessage

order, amount, region = sys.argv[1], int(sys.argv[2]), sys.argv[3]
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_sender("orders-saga") as sender:
    sender.send_messages(ServiceBusMessage(
        order, subject="OrderPlaced",
        message_id=f"{order}-OrderPlaced",
        application_properties={"amount": amount, "region": region}))
print(f"order {order} placed: amount {amount}, region {region}")
orchestrator.py
NEXT = {             # the saga: event received -> (queue, command)
    "OrderPlaced":     ("stock", "ReserveStock"),
    "StockReserved":   ("payment", "TakePayment"),
    "PaymentTaken":    ("shipping", "BookShipping"),
    "ShippingBooked":  (None, "Completed"),
    "PaymentDeclined": ("stock", "ReleaseStock"),      # compensate
    "ShippingFailed":  ("payment", "RefundPayment"),   # compensate
    "PaymentRefunded": ("stock", "ReleaseStock"),      # compensate
    "StockReleased":   (None, "Cancelled"),
}

def orchestrate(m, db, send):
    order, event = str(m), m.subject
    with db:                   # saga state per order, in our database
        if not db.execute("INSERT OR IGNORE INTO handled VALUES (?)",
                          (m.message_id,)).rowcount:
            return f"{order} {event}: duplicate, ignored"
        queue, step = NEXT[event]
        db.execute("INSERT OR REPLACE INTO saga VALUES (?, ?)",
                   (order, step))
        if queue:              # a failed send rolls back both rows
            send(queue, step, m)               # the next command
    return f"{order} {event} -> {step}"
services.py
def stock(command, props):
    if command == "ReserveStock":
        return "StockReserved"
    return "StockReleased"                     # ReleaseStock

def payment(command, props):
    if command == "RefundPayment":
        return "PaymentRefunded"
    if props["amount"] > 500:                  # over the card limit
        return "PaymentDeclined"
    return "PaymentTaken"

def shipping(command, props):
    if props["region"] not in ("eu", "us"):    # no carrier there
        return "ShippingFailed"
    return "ShippingBooked"
run.py
import os, sqlite3
from azure.servicebus import ServiceBusClient, ServiceBusMessage
from orchestrator import orchestrate
import services

db = sqlite3.connect("saga.db")
db.executescript("CREATE TABLE IF NOT EXISTS handled (message_id TEXT"
                 " PRIMARY KEY); CREATE TABLE IF NOT EXISTS saga"
                 " (order_id TEXT PRIMARY KEY, step TEXT);")
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])

def props(m):
    return {k.decode(): v.decode() if isinstance(v, bytes) else v
            for k, v in (m.application_properties or {}).items()}

def send(queue, subject, m):
    with client.get_queue_sender(queue) as sender:
        sender.send_messages(ServiceBusMessage(
            str(m), subject=subject, message_id=f"{m}-{subject}",
            application_properties=props(m)))

def service(queue):                  # run the command, reply an event
    def handle(m):
        event = getattr(services, queue)(m.subject, props(m))
        send("orders-saga", event, m)
        return f"{m} {m.subject} -> {event}"
    return handle

def pump(queue, handle):             # one queue, in sequence order
    with client.get_queue_receiver(queue, max_wait_time=1) as rx:
        got = []
        while batch := rx.receive_messages(max_message_count=10):
            got += batch
        for m in sorted(got, key=lambda m: m.sequence_number):
            print(f"{queue:<12}", handle(m))
            rx.complete_message(m)               # after the work
    return len(got)

with client:
    queues = ["stock", "payment", "shipping"]
    while pump("orders-saga", lambda m: orchestrate(m, db, send)) \
            + sum(pump(q, service(q)) for q in queues):
        pass                                 # until all are empty
state = db.execute("SELECT * FROM saga ORDER BY 1").fetchall()
print("saga state:", state)
Terminal
$ python place_order.py 6001 120 eu
order 6001 placed: amount 120, region eu
$ python run.py
orders-saga  6001 OrderPlaced -> ReserveStock
stock        6001 ReserveStock -> StockReserved
orders-saga  6001 StockReserved -> TakePayment
payment      6001 TakePayment -> PaymentTaken
orders-saga  6001 PaymentTaken -> BookShipping
shipping     6001 BookShipping -> ShippingBooked
orders-saga  6001 ShippingBooked -> Completed
saga state: [('6001', 'Completed')]

Order 6001 takes the happy path: ReserveStock, StockReserved, TakePayment, PaymentTaken, BookShipping, ShippingBooked, and the saga ends with saga state: [('6001', 'Completed')].

Choose orchestration for many steps or one place to see the state, and choreography for short flows.

Pitfall

There's no isolation across services. Others can see reserved stock before the payment is done.

Scenario 7: An order must reserve stock, take payment and book shipping, and roll back if any step fails. How do you design it with Service Bus?

As an orchestrated saga: one command queue per service, and one queue for the orchestrator's events. There is no transaction across stock, payment and shipping, so each step has a compensation: release the stock, refund the payment.

  • orders-saga - events for the orchestrator
  • Orchestrator - state per order
  • stock - reserve / release
  • payment - take / refund
  • shipping - book

The services in services.py from the previous step are tiny. Payment declines anything over 500, and shipping fails outside the EU and the US. Let's place two orders that fail: 6002 for 900 in the EU, and 6003 for 80 in Asia Pacific. We run python place_order.py 6002 900 eu, python place_order.py 6003 80 apac and python run.py.

Terminal
$ python place_order.py 6002 900 eu
order 6002 placed: amount 900, region eu
$ python place_order.py 6003 80 apac
order 6003 placed: amount 80, region apac
$ python run.py
orders-saga  6002 OrderPlaced -> ReserveStock
orders-saga  6003 OrderPlaced -> ReserveStock
stock        6002 ReserveStock -> StockReserved
stock        6003 ReserveStock -> StockReserved
orders-saga  6002 StockReserved -> TakePayment
orders-saga  6003 StockReserved -> TakePayment
payment      6002 TakePayment -> PaymentDeclined
payment      6003 TakePayment -> PaymentTaken
orders-saga  6002 PaymentDeclined -> ReleaseStock
orders-saga  6003 PaymentTaken -> BookShipping
stock        6002 ReleaseStock -> StockReleased
shipping     6003 BookShipping -> ShippingFailed
orders-saga  6002 StockReleased -> Cancelled
orders-saga  6003 ShippingFailed -> RefundPayment
payment      6003 RefundPayment -> PaymentRefunded
orders-saga  6003 PaymentRefunded -> ReleaseStock
stock        6003 ReleaseStock -> StockReleased
orders-saga  6003 StockReleased -> Cancelled
saga state: [('6001', 'Completed'), ('6002', 'Cancelled'), ('6003', 'Cancelled')]

For order 6002, stock is reserved, then TakePayment -> PaymentDeclined, so the orchestrator sends ReleaseStock, and the saga ends Cancelled.

For order 6003, stock and payment succeed, then BookShipping -> ShippingFailed. So the compensations run in reverse: RefundPayment, then ReleaseStock. The final state is [('6001', 'Completed'), ('6002', 'Cancelled'), ('6003', 'Cancelled')]. Nothing is half done.

For production, add three things. An outbox, so the orchestrator's state change and next command can't split. Duplicate detection plus a handled table in every service, so a repeated refund is skipped. And a dead-letter alert, because a compensation that keeps failing needs a human.

The result

Every order ends either completed or cancelled, with each step undone in reverse when a later one fails.

Key points to remember

  • A transaction covers Service Bus operations on one entity (or send via); no Python API
  • No database inside it: commit with the MessageId, then complete
  • Transactional outbox: one commit, a relay publishes, the MessageId dedupes
  • Request-reply: reply_to, correlation_id, a reply session per caller
  • Competing consumers scale out; sagas undo with compensations

The full script

This is a Python script that writes every file and runs every command above in order against the Service Bus emulator: start the emulator with SQL Server, run pip install azure-servicebus first, set SERVICEBUS_CONNECTION_STRING, and run it in an empty folder.

Show the full script that writes the files and runs every step
Full script - writes the files and runs every step against the Service Bus emulator (pip install azure-servicebus), run it with Python
# ========================================================================
# Service Bus Interview Q&A #8 - Transactions and reliable messaging patterns
# TechBrothersIT. Names and sample data are invented for teaching.
# Every step below was run on the official Azure Service Bus emulator (Docker image, version 2.0.1) with
# SQL Server 2022 and azure-servicebus 7.15.0; the outputs in the video and the blog post are the real outputs.
# To run it yourself:
#   1. Start the emulator with Microsoft's installer (github.com/Azure/azure-service-bus-emulator-installer),
#      using the Config.json this script writes first (Step 0) as the emulator's config file; it holds every
#      queue, topic, subscription, rule and setting (lock duration, max delivery count, duplicate detection...)
#      the steps depend on.
#   2. pip install azure-servicebus
#   3. Set SERVICEBUS_CONNECTION_STRING to the emulator connection string (Endpoint=sb://localhost;
#      SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;)
#      then run python ServiceBus_Interview_QA_08_Code.py.
# Run it in an empty folder and on a freshly started emulator: it creates its files (and any SQLite
# databases) there, and a second run on old files or old messages gives different output.
# Steps that restart the emulator say so in a comment: stop and start it by hand at that point.
# ========================================================================

import os, subprocess, time

def write(path, text):
    if text is None:
        if os.path.exists(path): os.remove(path)
        return
    os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        f.write(text.strip("\n") + "\n")

def run(cmd, expect_error=False, allow_error=False, env=None):
    print("\n$ " + cmd, flush=True)
    rc = subprocess.run(cmd, shell=True, env=dict(os.environ, **(env or {}))).returncode
    if rc != 0 and not (expect_error or allow_error):
        raise SystemExit("stopped: this command was expected to succeed")

# Step 0: the emulator config (mount it as ServiceBus_Emulator/ConfigFiles/Config.json, then start the emulator)
write('Config.json', """\
{
  "UserConfig": {
    "Namespaces": [
      {
        "Name": "sbemulatorns",
        "Queues": [
          {
            "Name": "order-placed",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT5M",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": true,
              "RequiresSession": false
            }
          },
          {
            "Name": "price-requests",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          },
          {
            "Name": "price-replies",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": true
            }
          },
          {
            "Name": "invoices",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          },
          {
            "Name": "orders-saga",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT5M",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": true,
              "RequiresSession": false
            }
          },
          {
            "Name": "stock",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          },
          {
            "Name": "payment",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          },
          {
            "Name": "shipping",
            "Properties": {
              "DeadLetteringOnMessageExpiration": false,
              "DefaultMessageTimeToLive": "PT1H",
              "DuplicateDetectionHistoryTimeWindow": "PT20S",
              "ForwardDeadLetteredMessagesTo": "",
              "ForwardTo": "",
              "LockDuration": "PT1M",
              "MaxDeliveryCount": 3,
              "RequiresDuplicateDetection": false,
              "RequiresSession": false
            }
          }
        ],
        "Topics": []
      }
    ],
    "Logging": {
      "Type": "File"
    }
  }
}
""")
# Step 0: the starting files
write('peek.py', 'import os, sys\nfrom azure.servicebus import ServiceBusClient\n\nqueue = sys.argv[1]\nclient = ServiceBusClient.from_connection_string(\n    os.environ["SERVICEBUS_CONNECTION_STRING"])\nwith client, client.get_queue_receiver(queue) as rx:\n    peeked = rx.peek_messages(50, sequence_number=1)\nprint(f"{queue}: peek shows {len(peeked)}", *sorted(\n    f"{m.message_id} {m}" for m in peeked), sep="\\n  ")\n')

# Step 1: What the Python SDK offers for transactions: nothing
write('check_tx.py', """\
import inspect
from importlib.metadata import version
import azure.servicebus as sb

print("azure-servicebus", version("azure-servicebus"))
for obj in [sb, sb.ServiceBusClient, sb.ServiceBusSender,
            sb.ServiceBusReceiver]:
    hits = [n for n in dir(obj) if "transaction" in n.lower()]
    print(f"{obj.__name__}: names with 'transaction': {hits}")
for cls, name in [(sb.ServiceBusSender, "send_messages"),
                  (sb.ServiceBusReceiver, "complete_message")]:
    params = inspect.signature(getattr(cls, name)).parameters
    print(f"{name}{tuple(params)}")
""")
run('python check_tx.py')

# Step 2: Transactional outbox: order + outbox row in one commit, a relay publishes, a failed publish stays
write('order_service.py', 'import json, sqlite3, sys\n\ndb = sqlite3.connect("shop.db")\ndb.executescript("""\nCREATE TABLE IF NOT EXISTS orders (order_id TEXT PRIMARY KEY,\n                                   amount REAL);\nCREATE TABLE IF NOT EXISTS outbox (message_id TEXT PRIMARY KEY,\n                                   body TEXT, sent INT DEFAULT 0);""")\nfor order in sys.argv[1:]:\n    event = (f"order-{order}-placed", json.dumps({"order": order}))\n    with db:                     # ONE database transaction: both rows\n        db.execute("INSERT INTO orders VALUES (?, ?)", (order, 49.90))\n        db.execute("INSERT INTO outbox (message_id, body)"\n                   " VALUES (?, ?)", event)\n    print(f"order {order}: order row + outbox row committed")\n')
write('relay.py', """\
import os, sqlite3
from azure.servicebus import ServiceBusClient, ServiceBusMessage

fail_on = os.environ.get("FAIL_ON")        # simulate a failed publish
db = sqlite3.connect("shop.db")
rows = db.execute("SELECT message_id, body FROM outbox WHERE sent = 0"
                  " ORDER BY message_id").fetchall()
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_sender("order-placed") as sender:
    for message_id, body in rows:
        try:
            if fail_on and fail_on in message_id:
                raise ConnectionError("simulated publish failure")
            sender.send_messages(ServiceBusMessage(
                body, message_id=message_id))   # same id, every retry
        except ConnectionError as e:
            print(f"{message_id}: {e}, row stays unsent")
            continue
        with db:                            # only after the ack
            db.execute("UPDATE outbox SET sent = 1"
                       " WHERE message_id = ?", (message_id,))
        print(f"{message_id}: published, marked sent")
left = db.execute("SELECT COUNT(*) FROM outbox WHERE sent = 0")
print("unsent outbox rows:", left.fetchone()[0])
""")
run('python order_service.py 5001 5002')
run('FAIL_ON=5002 python relay.py')
run('python relay.py')
run('python peek.py order-placed')

# Step 3: Request-reply: ReplyTo, a reply session per caller, CorrelationId
write('request.py', """\
import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage

client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_sender("price-requests") as sender:
    for instance, sku in [("shop-api-1", "sku-42"),
                          ("shop-api-2", "sku-77")]:
        sender.send_messages(ServiceBusMessage(
            sku, message_id=f"req-{sku}",
            reply_to="price-replies",          # where to answer
            reply_to_session_id=instance))     # my own reply session
        print(f"{instance} sent req-{sku}")
""")
write('respond.py', """\
import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage

PRICES = {"sku-42": "19.99", "sku-77": "4.50"}
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_receiver("price-requests",
                                       max_wait_time=3) as rx:
    for m in rx.receive_messages(max_message_count=10):
        with client.get_queue_sender(m.reply_to) as sender:
            sender.send_messages(ServiceBusMessage(
                PRICES[str(m)],
                correlation_id=m.message_id,      # which request
                session_id=m.reply_to_session_id))  # which caller
        rx.complete_message(m)
        print(f"{m.message_id}: answered on {m.reply_to},"
              f" session {m.reply_to_session_id}")
""")
write('await_reply.py', """\
import os, sys
from azure.servicebus import ServiceBusClient

instance = sys.argv[1]
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_receiver(
        "price-replies", session_id=instance, max_wait_time=5) as rx:
    for m in rx.receive_messages(max_message_count=10):
        print(f"{instance} got {m} for {m.correlation_id}")
        rx.complete_message(m)
""")
run('python request.py')
run('python respond.py')
run('python await_reply.py shop-api-2')
run('python await_reply.py shop-api-1')

# Step 4: Competing consumers: three workers on one queue
write('send_invoices.py', """\
import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage

client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_sender("invoices") as sender:
    sender.send_messages([ServiceBusMessage(f"invoice {i}")
                          for i in range(1, 31)])
print("sent 30 invoices")
""")
write('workers.py', """\
import os, sys, threading, time
from azure.servicebus import ServiceBusClient

done = {}                                   # worker -> invoices

def worker(name):
    client = ServiceBusClient.from_connection_string(
        os.environ["SERVICEBUS_CONNECTION_STRING"])  # one per thread
    done[name] = []
    with client, client.get_queue_receiver("invoices",
                                           max_wait_time=3) as rx:
        while batch := rx.receive_messages(max_message_count=1):
            for m in batch:
                time.sleep(0.2)             # process the invoice
                rx.complete_message(m)
                done[name].append(str(m))

n = int(sys.argv[1])
threads = [threading.Thread(target=worker, args=(f"w{i}",))
           for i in range(1, n + 1)]
for t in threads: t.start()
for t in threads: t.join()
work = [inv for invoices in done.values() for inv in invoices]
print(f"{n} competing consumers: {len(work)} invoices processed,",
      f"{len(set(work))} unique,",
      sum(1 for v in done.values() if v), "consumers got work")
""")
run('python send_invoices.py')
run('python workers.py 3')

# Step 5: A saga orchestrator: the happy path, driven by messages
write('place_order.py', """\
import os, sys
from azure.servicebus import ServiceBusClient, ServiceBusMessage

order, amount, region = sys.argv[1], int(sys.argv[2]), sys.argv[3]
client = ServiceBusClient.from_connection_string(
    os.environ["SERVICEBUS_CONNECTION_STRING"])
with client, client.get_queue_sender("orders-saga") as sender:
    sender.send_messages(ServiceBusMessage(
        order, subject="OrderPlaced",
        message_id=f"{order}-OrderPlaced",
        application_properties={"amount": amount, "region": region}))
print(f"order {order} placed: amount {amount}, region {region}")
""")
write('orchestrator.py', """\
NEXT = {             # the saga: event received -> (queue, command)
    "OrderPlaced":     ("stock", "ReserveStock"),
    "StockReserved":   ("payment", "TakePayment"),
    "PaymentTaken":    ("shipping", "BookShipping"),
    "ShippingBooked":  (None, "Completed"),
    "PaymentDeclined": ("stock", "ReleaseStock"),      # compensate
    "ShippingFailed":  ("payment", "RefundPayment"),   # compensate
    "PaymentRefunded": ("stock", "ReleaseStock"),      # compensate
    "StockReleased":   (None, "Cancelled"),
}

def orchestrate(m, db, send):
    order, event = str(m), m.subject
    with db:                   # saga state per order, in our database
        if not db.execute("INSERT OR IGNORE INTO handled VALUES (?)",
                          (m.message_id,)).rowcount:
            return f"{order} {event}: duplicate, ignored"
        queue, step = NEXT[event]
        db.execute("INSERT OR REPLACE INTO saga VALUES (?, ?)",
                   (order, step))
        if queue:              # a failed send rolls back both rows
            send(queue, step, m)               # the next command
    return f"{order} {event} -> {step}"
""")
write('services.py', """\
def stock(command, props):
    if command == "ReserveStock":
        return "StockReserved"
    return "StockReleased"                     # ReleaseStock

def payment(command, props):
    if command == "RefundPayment":
        return "PaymentRefunded"
    if props["amount"] > 500:                  # over the card limit
        return "PaymentDeclined"
    return "PaymentTaken"

def shipping(command, props):
    if props["region"] not in ("eu", "us"):    # no carrier there
        return "ShippingFailed"
    return "ShippingBooked"
""")
write('run.py', 'import os, sqlite3\nfrom azure.servicebus import ServiceBusClient, ServiceBusMessage\nfrom orchestrator import orchestrate\nimport services\n\ndb = sqlite3.connect("saga.db")\ndb.executescript("CREATE TABLE IF NOT EXISTS handled (message_id TEXT"\n                 " PRIMARY KEY); CREATE TABLE IF NOT EXISTS saga"\n                 " (order_id TEXT PRIMARY KEY, step TEXT);")\nclient = ServiceBusClient.from_connection_string(\n    os.environ["SERVICEBUS_CONNECTION_STRING"])\n\ndef props(m):\n    return {k.decode(): v.decode() if isinstance(v, bytes) else v\n            for k, v in (m.application_properties or {}).items()}\n\ndef send(queue, subject, m):\n    with client.get_queue_sender(queue) as sender:\n        sender.send_messages(ServiceBusMessage(\n            str(m), subject=subject, message_id=f"{m}-{subject}",\n            application_properties=props(m)))\n\ndef service(queue):                  # run the command, reply an event\n    def handle(m):\n        event = getattr(services, queue)(m.subject, props(m))\n        send("orders-saga", event, m)\n        return f"{m} {m.subject} -> {event}"\n    return handle\n\ndef pump(queue, handle):             # one queue, in sequence order\n    with client.get_queue_receiver(queue, max_wait_time=1) as rx:\n        got = []\n        while batch := rx.receive_messages(max_message_count=10):\n            got += batch\n        for m in sorted(got, key=lambda m: m.sequence_number):\n            print(f"{queue:<12}", handle(m))\n            rx.complete_message(m)               # after the work\n    return len(got)\n\nwith client:\n    queues = ["stock", "payment", "shipping"]\n    while pump("orders-saga", lambda m: orchestrate(m, db, send)) \\\n            + sum(pump(q, service(q)) for q in queues):\n        pass                                 # until all are empty\nstate = db.execute("SELECT * FROM saga ORDER BY 1").fetchall()\nprint("saga state:", state)\n')
run('python place_order.py 6001 120 eu')
run('python run.py')

# Step 6: The saga with failures: a declined payment and a failed shipment, compensated
run('python place_order.py 6002 900 eu')
run('python place_order.py 6003 80 apac')
run('python run.py')
Next in this series

Part 9: Security, Networking and Disaster Recovery

The sample data on this page is invented for teaching. Every terminal output is the real output of the command above it, run on the official Azure Service Bus emulator (2.0.1) with SQL Server and azure-servicebus 7.15.0. Features the emulator does not support are shown as code. Part of the Real Time Azure Service Bus Interview Questions and Answers series on TechBrothersIT. Features, limits and their status (GA or preview) are current as of October 2026; check the official Azure Service Bus documentation (learn.microsoft.com/azure/service-bus-messaging) for anything you quote in an interview.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.