Appearance
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.
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.
| Commands | Events |
|---|---|
| "Reserve stock" | "Order placed" |
| Sent to one specific receiver | Broadcast to whoever subscribes |
| Can be refused | A fact; already happened |
| The sender expects a result | The 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:
| Technique | Example |
|---|---|
| Show progress honestly | "Payment received. Preparing your order." |
| Read your own writes | After saving, read from the source, not a replica or cache |
| Guarantee delivery | Outbox pattern, so the event is never lost (question 15) |
| Handle duplicates | Idempotent consumers (idempotency) |
| Handle order | Version numbers; ignore an older version than the one you have |
| Reconcile | A 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:
- Orders creates the order.
- Payments charges the card.
- 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:
- Re-draw the boundary. If orders and payments always change together, perhaps they belong in one service.
- Saga for the process, orchestrated or choreographed (question 13).
- Compensations for each step that can be undone: refund the payment, release the stock.
- Outbox so each local transaction and its event commit together (question 15).
- Idempotent handlers so retried messages do no harm.
- Timeouts and a final state for every saga, so nothing is stuck in "pending" forever.
- 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.
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
}
}
}- Each step is a call to one service, which commits its own local transaction. Each must be idempotent, because it may be retried.
- Record progress durably, so if the orchestrator crashes, it can resume or compensate from where it stopped.
- On failure, undo completed steps in reverse. A compensation must also be idempotent and retried until it succeeds.
| Orchestration | Choreography | |
|---|---|---|
| Who decides the next step | A central orchestrator | Each service, reacting to events |
| Seeing the whole flow | Easy: one place | Hard: spread across services |
| Coupling | Orchestrator knows every step | Services know only events |
| Good for | Complex flows with many steps | Simple 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.
| Two-phase commit | Saga | |
|---|---|---|
| Atomicity | True: all or nothing | Eventually: forward or compensated |
| Isolation | Others never see partial work | Partial states are visible |
| Locks | Held across the network until commit | Only inside each local step |
| Participants | Must all support XA or a 2PC protocol | Any service with an API |
| Failure of the coordinator | Participants may block | Resume or compensate from the saga log |
| Fits | One organisation's databases, low volume | Microservices, 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.
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)])
})
}- One local transaction for the business change and the event.
- The event row commits only if the order commits.
- Several relays can run safely:
SKIP LOCKEDgives each a different batch (SKIP LOCKED). - 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.
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 when | Avoid it when |
|---|---|
| Reads vastly outnumber writes and need different shapes | Simple CRUD screens |
| Several very different read views of the same data | The team is small and the domain is simple |
| Reads need a different store, such as full-text search | Users 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- 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.
| Good | Costs |
|---|---|
| Full history and audit | Events are permanent; schema changes need versioning and upcasting |
| Time travel and debugging | Rebuilding state needs snapshots when histories are long |
| New read models from old events | Queries need projections; eventual consistency |
| Natural fit for event-driven systems | A 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.