Ordering backend project · Chapter 8 of 12
Slot holds
By the end of this chapter you can keep a place in a delivery slot for a member inside one transaction; watch two transactions race for the last place and overfill the slot; and stop it with a row lock, SELECT ... FOR UPDATE, so the second transaction waits, counts again and gets a clean 409 slot_full. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Taking orders
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.
Why you need this
This is the chapter DabbaDrop's backend exists for. On Monday 19 October the sheet took 52 orders for the 12:30 Goregaon slot, whose two riders carry 40 dabbas. Twelve lunches reached people after 2:15 pm, cold, and Rafiq refunded all twelve. For launch week Farah has one rule: a slot never takes more dabbas than its riders can carry.
Ordering works like a cinema seat. A member taps the 12:30 slot, and a place is held for them for 10 minutes while they choose dishes and pay; nobody else can take it. Chapter 7 already counts live holds in the places left. What is missing is the code that makes a hold, and it has three ways to go wrong:
- It counts holds that expired long ago and says full when it is not.
- Tara taps twice and holds two places.
- Tara and Advait tap the last place in the same second, both see one place left, and both get it: 41 dabbas for 40 places.
At a cinema, a unique index on (show, seat) stops two people holding seat E7: the second insert of the same key fails. A dabba has no seat number: the rule is "at most 40 rows", and no unique constraint can count. The unique slot_holds_slot_user on (slot_id, user_id) only stops one member holding the same slot twice.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
holds/service.py: keep a place in a slot
Write hold_slot(db, user_id, slot_id) in app/modules/holds/service.py. It keeps a place in a delivery slot for this member for settings.hold_minutes (10) minutes, in one transaction, in this order: no such slot is not_found(...) (404); a slot whose status is not open is conflict("slot_closed", ...) (409); then delete this slot's holds that are this member's own (a new hold replaces the old one) or that have expired (expires_at at or before now); then count what is taken, the slot's holds left plus its orders whose status is pending or confirmed, and raise conflict("slot_full", ...) (409) when that is at least the capacity; then add a SlotHold with expires_at = datetime.now(MUMBAI) plus 10 minutes, and commit. On any error, roll back and raise it on.
Return {"slot_id", "expires_at" (the datetime you stored), "places_left": capacity minus taken minus 1}.
The Check holds slot 17 for Tara (39 left), holds it again (still one hold, 39 left), tries slot 999 and the closed slot 18, fills slot 19 (2 places) with a hold and a pending order (409 slot_full, and no hold for Tara), cancels the order (now Tara gets the last place, 0 left), holds slot 20 whose only hold expired (the expired one is gone), then sends POST /holds as Tara (201 with { hold }).
- Task 2 · interview question
Interview: holds/service.py, the last place, once
When two members hold the last place of a slot at the same moment, both can count one place left and both insert: no unique constraint can say "at most 40 rows". Make hold_slot lock the slot's row before it counts: select it with .with_for_update() (SELECT ... FOR UPDATE). A second transaction that locks the same row waits until the first commits or rolls back, then counts again and sees the first one's hold, so it raises conflict("slot_full", ...).
The Check makes slot 22 one place and opens a transaction of its own that locks slot 22's row and inserts Advait's hold, keeping it open while your hold_slot runs for Tara; then it commits. Your call must wait and then raise the AppError 409 slot_full, and slot 22 must have one hold, Advait's. Then Tara and Advait hold slot 23 (one place) at the same moment: exactly one gets it, the other gets the 409. Then POST /holds as Bhavna for a slot Tara holds the only place of must answer 409 with the code slot_full.
Kunal will ask: Tara's call waited at the select until Advait's transaction ended. What was it waiting for, and why did it say slot_full afterwards instead of taking the place?
- Task 3
holds/service.py: let go of my place
Write release_holds(db, user_id, slot_id) in app/modules/holds/service.py. It deletes this member's hold on that slot, and nobody else's, commits, and returns how many holds it deleted as an int (1, or 0 when there was none).
The Check gives Tara holds on slots 25 and 26 and Advait one on slot 25: release_holds(db, 2, 25) must return 1 and leave Advait's hold and Tara's hold on slot 26; a second call returns 0. Then DELETE /holds/26 as Tara must answer 200 with {"released": 1}.
What you will learn
- A hold: a row that keeps a place for one member until expires_at.
- One transaction, rolled back on any error: the session's transaction, and why except: rollback; raise.
- What a transaction sees: Postgres's read committed, and why a count cannot see a hold that is not committed.
- The race, for real: two transactions overfilling a one-place slot, step by step.
- Row locks: SELECT ... FOR UPDATE with SQLAlchemy's .with_for_update(), who waits, and when the lock is released.
- returning: getting back the rows a delete or an update changed.
More in Taking orders
Build "Slot holds" yourself
DabbaDrop delivers tiffins across Mumbai. You build its ordering API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.