Sending emails and generating PDF invoices makes your checkout API slow. How would you move this work to the background?
What the interviewer wants to hear
- Return quickly and enqueue a job to a queue
- Workers process jobs separately and scale independently
- Retries with backoff and a dead-letter queue
- Idempotent jobs because of at-least-once delivery
- Monitoring queue length and failures; job status for the user
Model answer
The checkout request should only do what must be done before replying: validate, save the order and charge payment. Then it pushes a job, such as SendInvoice with the order id, to a queue like SQS, RabbitMQ or BullMQ on Redis, and returns. To avoid losing the job if the app crashes after commit, I would use an outbox table. Separate worker processes consume jobs, generate the PDF, store it in object storage and send the email. Workers scale independently from the API. Failed jobs retry with exponential backoff, and after several failures go to a dead-letter queue for investigation. Because queues usually deliver at least once, jobs must be idempotent, for example by marking the invoice as sent. I monitor queue depth, job age and failure rate, and alert when they grow.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Backend Developer questions
- An API endpoint is slow because it runs a heavy database query. How would you add caching, and how do you keep the cache correct?
- You need to rename a column in a large production table that the live app uses. How do you do it without downtime?
- How do you change a public API without breaking existing clients, such as old mobile app versions?
- Users say one API endpoint has become slow in production. How do you find and fix the cause?
- How would you add rate limiting to a public API that runs on many servers?
- You are asked to review the security of a REST API before launch. What would you check?
- Your service receives webhooks from a payment provider. How do you make handling them secure and reliable?
- When would you choose Kafka and when RabbitMQ for messaging between services?
- How would you design an API that lets users upload large files, such as videos of several gigabytes?