Skip to content

Interview PrepData across services · Advanced

Events and Consistency ​

9. What is event-driven architecture? ​

The short answer

In event-driven architecture, services communicate by publishing events, facts about something that already happened, such as order.placed or payment.failed, to a broker such as Kafka or RabbitMQ. Other services subscribe and react in their own time: the warehouse reserves stock, email sends a receipt, analytics records the sale. The publisher does not know or wait for its consumers. This decouples services, absorbs traffic spikes and lets you add new consumers without changing the publisher. The costs are eventual consistency, harder debugging, and the need to handle duplicate and out-of-order events.

orderspublishesbrokerorder.placedwarehouse: reserve stockemail: send receiptanalytics: record saleloyalty: added later
Announce, do not instruct. Orders states a fact once. Each consumer decides what to do with it. Adding the loyalty service needs no change to orders, which is the main benefit of the style.

An event, as it travels:

json
{
  "id": "evt_01JB7X3K9Q",
  "type": "order.placed",
  "version": 1,
  "occurredAt": "2026-10-27T10:02:11Z",
  "data": { "orderId": "ord_8812", "customerId": "u_42", "totalPence": 8550 }
}

Reading it: id lets consumers ignore duplicates. type and version let consumers handle the format changing over time. occurredAt is when it happened, not when it was delivered. data carries enough for consumers to act, without them calling back for more.

CommandsEvents
"Reserve stock""Order placed"
Sent to one specific receiverBroadcast to whoever subscribes
Can be refusedA fact; already happened
The sender expects a resultThe publisher expects nothing

10. What is eventual consistency? ​

The short answer

Eventual consistency means that after an update, different parts of a system may disagree for a short time, but if no new updates arrive, they will all converge to the same value. A customer's order shows "paid" in the orders service a second before the warehouse service has heard about it. It is the normal state of any system that copies data between services, caches or replicas asynchronously. You design for it by deciding where brief staleness is acceptable, showing users honest in-progress states, and making sure every change is eventually delivered.

Making it work in practice:

TechniqueExample
Show progress honestly"Payment received. Preparing your order."
Read your own writesAfter saving, read from the source, not a replica or cache
Guarantee deliveryOutbox pattern, so the event is never lost (question 15)
Handle duplicatesIdempotent consumers (idempotency)
Handle orderVersion numbers; ignore an older version than the one you have
ReconcileA nightly job compares systems and fixes drift

Where it is not acceptable: decisions that must not be made on stale data, such as "is this seat still free?" or "is the balance enough?". Those must check the source of truth, inside a transaction.

11. What is a distributed transaction? ​

The short answer

A distributed transaction is a single all-or-nothing operation that spans more than one database or service, such as debiting an account in one database and crediting it in another. Ordinary database transactions cannot cross that boundary. Making it atomic needs a coordination protocol, classically two-phase commit, which locks resources in every participant until all agree, or a saga, which runs local transactions one by one and undoes completed steps if a later one fails. In microservices, sagas are almost always the choice.

Why it is hard, in one example. Placing an order touches three services, each with its own database:

  1. Orders creates the order.
  2. Payments charges the card.
  3. Warehouse reserves stock.

If step 3 fails because the last item was just sold, steps 1 and 2 have already committed in other databases. There is no ROLLBACK that reaches across them. You need an explicit plan for undoing, and for what happens if the undo itself fails. That plan is the saga (question 13).

12. How do you manage transactions across multiple services? ​

The short answer

First, avoid needing them: draw service boundaries so that data which must change together lives in one service and one database. When a business process truly spans services, use a saga: a sequence of local transactions, each publishing an event or command that triggers the next, with a compensating action to undo each completed step if a later step fails. Make every step idempotent, publish events reliably with the outbox pattern, and design the user experience for "pending" states. Use two-phase commit only inside one trusted system that supports it.

The checklist, in the order you apply it:

  1. Re-draw the boundary. If orders and payments always change together, perhaps they belong in one service.
  2. Saga for the process, orchestrated or choreographed (question 13).
  3. Compensations for each step that can be undone: refund the payment, release the stock.
  4. Outbox so each local transaction and its event commit together (question 15).
  5. Idempotent handlers so retried messages do no harm.
  6. Timeouts and a final state for every saga, so nothing is stuck in "pending" forever.
  7. Monitoring of stuck and failed sagas, with a way for a person to fix them.

13. What is the Saga pattern? ​

The short answer

A saga splits a business transaction that spans services into a series of local transactions, one per service. Each step commits in its own database and then triggers the next step. If a step fails, the saga runs compensating transactions for the steps already done, in reverse order, such as refunding a payment or releasing reserved stock. There are two styles. In orchestration, a central coordinator tells each service what to do next. In choreography, each service listens for events and reacts. Sagas give eventual consistency, not isolation, so other users can see intermediate states.

order saga orchestrator① create orderstatus: pending② charge paymentpayments service③ reserve stockfails: sold out④ confirm ordernot reachedundo ②: refundcompensationundo ①: cancel ordercompensation
Go forward, or walk back. Each box is a local transaction in one service. When step ③ fails, the saga does not roll back the database; it runs new transactions that undo the effects of ② and ①, in reverse order.

An orchestrator, simplified:

ts
type Step = { name: string; run: (ctx: SagaCtx) => Promise<void>; undo?: (ctx: SagaCtx) => Promise<void> }

const placeOrderSaga: Step[] = [
  { name: 'createOrder',  run: (c) => orders.create(c.orderId, c.items),          undo: (c) => orders.cancel(c.orderId) },
  { name: 'chargeCard',   run: (c) => payments.charge(c.orderId, c.totalPence),   undo: (c) => payments.refund(c.orderId) },
  { name: 'reserveStock', run: (c) => warehouse.reserve(c.orderId, c.items),      undo: (c) => warehouse.release(c.orderId) },
  { name: 'confirm',      run: (c) => orders.confirm(c.orderId) },
]

export async function runSaga(steps: Step[], ctx: SagaCtx) {
  const done: Step[] = []
  for (const step of steps) {
    try {
      await step.run(ctx)                                                       // (1)
      done.push(step)
      await sagaLog.record(ctx.orderId, step.name, 'done')                      // (2)
    } catch (err) {
      for (const d of done.reverse()) await d.undo?.(ctx)                       // (3)
      await sagaLog.record(ctx.orderId, step.name, 'compensated')
      throw err
    }
  }
}
  1. Each step is a call to one service, which commits its own local transaction. Each must be idempotent, because it may be retried.
  2. Record progress durably, so if the orchestrator crashes, it can resume or compensate from where it stopped.
  3. On failure, undo completed steps in reverse. A compensation must also be idempotent and retried until it succeeds.
OrchestrationChoreography
Who decides the next stepA central orchestratorEach service, reacting to events
Seeing the whole flowEasy: one placeHard: spread across services
CouplingOrchestrator knows every stepServices know only events
Good forComplex flows with many stepsSimple flows with 2 or 3 steps

Production tools. Durable workflow engines, such as Temporal or AWS Step Functions, store each step's state for you and retry and resume automatically, which is hard to build well yourself.

14. How do Two-Phase Commit and the Saga pattern differ? ​

The short answer

Two-phase commit, 2PC, gives true atomicity across participants: a coordinator first asks every participant to prepare, which means locking their changes and promising they can commit, and only if all say yes does it tell them to commit. Nobody sees intermediate states, but locks are held across the network, every participant must support the protocol, and a coordinator crash can leave participants blocked. A saga commits each step immediately and compensates on failure: no long locks and it works with any service, but intermediate states are visible and compensations must be designed.

coordinatordatabase Arows locked, voted yesdatabase Brows locked, voted yes① prepare① prepare② all yes: commitcoordinator crashes here?both wait, locks held
Ask everyone, then tell everyone. Prepare locks the changes and makes each participant promise it can commit. Commit makes them permanent. Between the two, locks are held, and a lost coordinator leaves everyone waiting.
Two-phase commitSaga
AtomicityTrue: all or nothingEventually: forward or compensated
IsolationOthers never see partial workPartial states are visible
LocksHeld across the network until commitOnly inside each local step
ParticipantsMust all support XA or a 2PC protocolAny service with an API
Failure of the coordinatorParticipants may blockResume or compensate from the saga log
FitsOne organisation's databases, low volumeMicroservices, third-party APIs

You cannot 2PC a third party. Stripe, a shipping company's API, or an email provider will not join your two-phase commit. That alone decides most real designs in favour of sagas.

15. What is the transactional outbox pattern? ​

The short answer

The outbox pattern solves the dual write problem: a service must update its database and publish an event, and either can fail without the other. Instead of publishing directly, the service writes the event into an outbox table in the same database transaction as the business change, so both commit or neither does. A separate relay process reads the outbox and publishes each event to the broker, marking it sent. Events may be published more than once, so consumers must be idempotent, but no event is ever lost and none is published for a change that rolled back.

ordersserviceone transactionINSERT INTO orders …INSERT INTO outboxorder.placedrelaypoll, publishbrokerthen mark the outbox row published
Write the event where the data lives. The order and its event commit together in one local transaction. Publishing happens afterwards, from the outbox, and is retried until it succeeds.
ts
await withTransaction(async (tx) => {                                           // (1)
  await tx.query('INSERT INTO orders (id, customer_id, total_pence, status) VALUES ($1, $2, $3, $4)',
    [order.id, order.customerId, order.totalPence, 'placed'])
  await tx.query('INSERT INTO outbox (id, type, payload) VALUES ($1, $2, $3)',
    [randomUUID(), 'order.placed', JSON.stringify({ orderId: order.id, totalPence: order.totalPence })])   // (2)
})

// relay, running on its own
async function relayOnce() {
  await withTransaction(async (tx) => {
    const { rows } = await tx.query(
      'SELECT id, type, payload FROM outbox WHERE published_at IS NULL ORDER BY created_at LIMIT 100 FOR UPDATE SKIP LOCKED')   // (3)
    for (const e of rows) await broker.publish(e.type, e.payload, { messageId: e.id })   // (4)
    await tx.query('UPDATE outbox SET published_at = now() WHERE id = ANY($1)', [rows.map((r) => r.id)])
  })
}
  1. One local transaction for the business change and the event.
  2. The event row commits only if the order commits.
  3. Several relays can run safely: SKIP LOCKED gives each a different batch (SKIP LOCKED).
  4. Publish with the event's own ID, so consumers can drop duplicates. If the relay crashes after publishing but before marking, the event is published again, which is why delivery is "at least once".

The wrong way, and why it fails: await db.insert(order); await broker.publish(event). If the process dies between the two lines, the order exists and nobody is told. Reversing the order, publish then insert, tells everyone about an order that may never be saved.

A variation: change data capture. Instead of polling, a tool such as Debezium reads the database's write-ahead log and publishes new outbox rows, with lower latency and no polling load.

16. What is Command Query Responsibility Segregation (CQRS)? ​

The short answer

CQRS separates the model you use to change data, commands, from the model you use to read it, queries. Writes go through a model designed for rules and consistency, such as normalised tables and domain objects. Reads come from one or more read models, denormalised views shaped exactly for each screen or API, often in a different store such as a search index or a cache, kept up to date from events. It lets reads and writes scale and evolve separately. The cost is eventual consistency between the two and more moving parts. Use it where read and write needs really differ, not everywhere.

commandsquerieswrite modelrules, normalised tableseventsprojectororder summarytablesearchindexreads from views built for each screen
Two shapes for two jobs. The write side protects the rules. The read side is copied into whatever shape makes each screen fast. Events keep the read side up to date, a moment behind.

A light version you can use without events: keep writes in normalised tables, and serve a heavy dashboard from a materialized view refreshed every minute (materialized views). That is already the CQRS idea.

Use CQRS whenAvoid it when
Reads vastly outnumber writes and need different shapesSimple CRUD screens
Several very different read views of the same dataThe team is small and the domain is simple
Reads need a different store, such as full-text searchUsers must always see their own change instantly, everywhere

17. What is Event Sourcing? ​

The short answer

Event sourcing stores every change as an event, in order, instead of storing only the current state. A bank account is not a row with balance = 70; it is the history opened, deposited 100, withdrew 30. The current state is computed by replaying the events, often sped up with snapshots. You get a complete audit trail, the ability to see the state at any past moment, and the freedom to build new read models from old events. The costs: events can never be changed, so evolving their format is hard; queries need projections; and the approach is complex. It is usually paired with CQRS.

ts
type AccountEvent =
  | { type: 'opened'; accountId: string; at: string }
  | { type: 'deposited'; amountPence: number; at: string }
  | { type: 'withdrew'; amountPence: number; at: string }

function balanceOf(events: AccountEvent[]): number {
  return events.reduce((bal, e) =>                                              // (1)
    e.type === 'deposited' ? bal + e.amountPence :
    e.type === 'withdrew'  ? bal - e.amountPence : bal, 0)
}

const history: AccountEvent[] = [
  { type: 'opened', accountId: 'acc_1', at: '2026-10-01T09:00:00Z' },
  { type: 'deposited', amountPence: 10_000, at: '2026-10-02T09:00:00Z' },
  { type: 'withdrew', amountPence: 3_000, at: '2026-10-05T12:00:00Z' },
]
console.log(balanceOf(history))                       // 7000
console.log(balanceOf(history.slice(0, 2)))           // 10000, the balance on 2 October
  1. State is a fold over the events. Replaying a prefix of the history gives the state at that moment, which is the audit feature for free.
GoodCosts
Full history and auditEvents are permanent; schema changes need versioning and upcasting
Time travel and debuggingRebuilding state needs snapshots when histories are long
New read models from old eventsQueries need projections; eventual consistency
Natural fit for event-driven systemsA big shift in thinking for the team

Event sourcing is not the same as event-driven architecture. Event-driven means services talk by events. Event sourcing means one service stores its own state as events. You can do either without the other.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Architecture & Distributed Systems
Structure
Data across services
Resilience
Communication
Security between services
Algorithms
Other topics
Nothing picked yet