Spokalyالإنجليزية تبدأ من السمع
مقابلات العمل · مبرمج · متقدّم

مقابلة مطوّر خلفية: واجهات برمجية وإعادة المحاولة واستعلام بطيءBackend Developer: APIs, Retries and a Slow Query

  • Walid
  • Rana
0:00 / 3:23
الشريط الملوّن مقسوم حسب المتكلّم، فترى من يتحدّث ومتى.

النص

  1. Walid
    Thanks for joining, Rana. I'll ask about real situations, not trick questions. Please think aloud, even if you're not sure.
  2. Rana
    Sounds good. I'll say my assumptions as I go.
  3. Walid
    Let'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?
  4. Rana
    The 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.
  5. Walid
    How would you fix it?
  6. Rana
    I'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.
  7. Walid
    What if two identical requests arrive at the same moment, before the first is saved?
  8. Rana
    Good 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.
  9. Walid
    Next 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?
  10. Rana
    With 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.
  11. Walid
    What would you do instead?
  12. Rana
    I'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.
  13. Walid
    Is there another benefit to the cursor approach?
  14. Rana
    Yes. 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.
  15. Walid
    Last scenario. A query that filters by customer ID and date takes eight seconds. Walk me through how you'd investigate.
  16. Rana
    First 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.
  17. Walid
    Any downside to adding indexes?
  18. Rana
    Yes. 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.
  19. Walid
    And if the plan already uses the index but it's still slow?
  20. Rana
    Then 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.
  21. Walid
    I appreciate you saying what you're unsure about. Do you have questions for me?
  22. Rana
    Yes. How does your team handle incidents when a payment fails in production?
  23. Walid
    We have an on-call rotation, a runbook, and a review after every incident. I'll walk you through it in the next round.