What interviewers ask next
Once the levels pass, expect questions about the real system behind the class. The reports name the parts and the second-part dependencies. The questions below are the ones the design itself raises, so be ready for them:
- How long should a lease be? And what about a task that takes longer?
- A worker's lease ran out, but it's still running. What stops it from writing a stale result?
- What if the queue itself crashes?
- What about priorities?
- Many workers on many threads? Put one lock around each public method. Every call reads and writes the same heaps, so it's all or nothing. A lock per task doesn't help, because reserve has to look at all of them.
Start with the lease length. Too short, and slow tasks time out while they're still working, so they run twice and use up attempts they never failed. Too long, and a crashed worker's task sits hidden for that long before anyone retries it.
The way out is a heartbeat. Set a short timeout, and let a worker that's still busy extend its lease every so often. Amazon's SQS guide recommends this for long jobs: a heartbeat that keeps extending the visibility timeout until the work is done. A crashed worker stops sending heartbeats, so its lease ends soon after the crash.
Two rules keep heartbeats safe. Only the holder can extend. And a lease that has already ended can't be brought back, because by then the task may belong to someone else.
The batch inference lesson shows the same leases and heartbeats in a system design, with a worker that dies partway through a long job.
The function extend in the editor is a heartbeat. Write it, so the holder of a live lease moves its deadline to now + timeout, and every other call returns False.