In a microservices system, placing an order touches the Order, Payment and Inventory services, each with its own database. How do you keep data consistent?
What the interviewer wants to hear
- Avoid two-phase commit across services; explain why
- Saga: sequence of local transactions with compensating actions
- Choreography vs orchestration trade-offs
- Reliable events via outbox; idempotent steps
- Eventual consistency, intermediate states shown to users, timeouts
Model answer
There is no single ACID transaction across three databases, and two-phase commit is slow, couples services and blocks when the coordinator fails, so I would use a saga. A saga is a series of local transactions, each with a compensating action. For example: Order service creates the order as PENDING, Inventory reserves stock, Payment charges the card, then the order becomes CONFIRMED. If payment fails, Inventory releases the reservation and the order becomes CANCELLED. With choreography, services react to each other events, which is simple for few steps but hard to follow. With orchestration, a central orchestrator, for example using Temporal or a state machine, tells each service what to do, which is easier to monitor. Each step publishes events via an outbox and must be idempotent. The system is eventually consistent, so the UI shows states like pending, and timeouts trigger compensation.
Can you answer it under time pressure?
Take a timed hard round and get scored on every answer.
More hard Backend Developer questions
- Design a URL shortener like bit.ly that handles 100 million new links per month and many more redirects. Cover IDs, storage, caching and scale.
- A product manager says each payment event must be processed exactly once. How do you achieve that with real message queues and networks?
- Your single PostgreSQL database is reaching its limits. How would you scale reads and then writes, and what problems does each step bring?
- You are building a B2B SaaS backend used by many companies. How would you design multi-tenancy and make sure one tenant can never see another tenant data?
- Design a notification service that sends email, SMS and push messages for many products, up to tens of millions of messages a day.
- Your company wants to split a large monolith into microservices. How would you approach it, and when would you argue against it?
- Design the backend for a real-time chat app with one-to-one and group chats, online status and message history.
- One slow downstream dependency caused your whole platform to go down. How would you handle the incident, and what would you change afterwards?
- How would you design a ledger for a wallet or payments product so balances are always correct?