Job interviews3:23
Job interviews · Software developer · Advanced
Backend Developer: APIs, Retries and a Slow Queryمقابلة مطوّر خلفية: واجهات برمجية وإعادة المحاولة واستعلام بطيء
- Walid
- Rana
0:00 / 3:23
Transcript
- WalidThanks for joining, Rana. I'll ask about real situations, not trick questions. Please think aloud, even if you're not sure.
- RanaSounds good. I'll say my assumptions as I go.
- WalidLet's start with a payment endpoint. A mobile client sends a request to charge a customer, the network is slow, and the client sends the same request again. What could go wrong?
- RanaThe customer could be charged twice, because a POST request isn't idempotent by default. The server doesn't know the second request is a retry of the first one.
- WalidHow would you fix it?
- RanaI'd ask the client to send a unique idempotency key with each charge request. The server stores the key and the result. If the same key arrives again, it returns the saved result instead of charging again. I'd keep the keys for a limited time, say twenty-four hours.
- WalidWhat if two identical requests arrive at the same moment, before the first is saved?
- RanaGood point. I'd use a unique constraint on the key in the database. The second insert would fail, and I'd return a 'request in progress' response or wait for the first one to finish.
- WalidNext topic. We have a table of fifty million orders. The app lists orders page by page. The query uses a limit and an offset, and it gets very slow on later pages. Why?
- RanaWith a large offset, the database still has to read and skip all the earlier rows before it returns the page. So the cost grows with the page number.
- WalidWhat would you do instead?
- RanaI'd use cursor, or keyset, pagination. The client sends the last order ID it saw, and the query asks for orders with an ID smaller than that, sorted by ID from newest to oldest, with a limit. With an index on ID it stays fast on every page. The trade-off is that users can't jump to page five hundred directly.
- WalidIs there another benefit to the cursor approach?
- RanaYes. With offset, if new orders are added while the user is paging, items can move between pages, so they may see duplicates or miss one. A cursor is more stable.
- WalidLast scenario. A query that filters by customer ID and date takes eight seconds. Walk me through how you'd investigate.
- RanaFirst I'd look at the query's execution plan to see whether it scans the whole table. If it does, I'd check which indexes exist. A composite index on customer ID first and then date would probably help, because the query uses both columns.
- WalidAny downside to adding indexes?
- RanaYes. Every index uses storage and makes writes slower, because the database must update it on each insert or update. So I wouldn't add one for every query. I'd measure first.
- WalidAnd if the plan already uses the index but it's still slow?
- RanaThen I'd check how many rows match, whether statistics are outdated, and whether the query returns more columns than needed. I'm less sure about tuning statistics in your database, so I'd read its documentation or ask a colleague.
- WalidI appreciate you saying what you're unsure about. Do you have questions for me?
- RanaYes. How does your team handle incidents when a payment fails in production?
- WalidWe have an on-call rotation, a runbook, and a review after every incident. I'll walk you through it in the next round.