APIs with FastAPI · Chapter 11 of 26

Transactions and concurrency

By the end of this chapter you can make several changes save together or not at all, turn a unique constraint into a 409, find the gap between a check and an act that two requests can both pass, close it with a row lock or a conditional update, and make a retried request safe with an idempotency key. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Structure and data
  • 4 tasks
  • FastAPI
  • In the all labs plan

The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.

Why you need this

The festive rush has started, and Sunita has a list. On Tuesday two riders arrived at Neelkanth Blue Pottery for the same pickup: two dispatchers had assigned the parcel within the same second, and both were told "done". On Wednesday a parcel showed as picked up with no pickup in its history, because the API crashed between saving the one and the other. And Imran has a merchant whose app retries any request that takes more than a second; on a slow evening it booked two pickups for one batch of parcels, and two riders went.

Everything you have written so far is correct for one request at a time. In production, requests arrive together, on several servers, and sometimes twice. Nothing in Python stops two requests from reading the same row at the same moment. The database can, if you ask it.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    pickup_scan.py: all or nothing

    db.py is written for you: read it first. When a rider scans a parcel at pickup, two things change together: the parcel and its history. Make an app (tables created at startup) with POST /parcels/{awb}/pickup taking {"rider_id": ..., "hub": ...}. For a booked parcel it sets the status to picked_up and the rider_id, adds an event (that awb, status picked_up, that hub), and answers 200 with {"awb": ..., "status": "picked_up", "rider_id": ...}. A parcel that is not booked answers 409; one the database does not have, 404.

    The check then makes the database refuse one parcel's event: that request must fail (any 4xx or 5xx), and the parcel must be left exactly as it was. The next request must work.

  2. Task 2

    book.py: 409 for a taken AWB

    Make an app (tables created at startup) with POST /parcels taking chapter 9's booking (awb DKG and five digits, merchant_id, to_pincode six digits, weight_grams more than 0, cod_amount 0 or more, default 0), saving it and answering 201 with the booking and status booked. An AWB that is already in the database answers 409 with a detail, and the next booking still works.

    The check also runs your file on two uvicorn servers at once, as production runs several, and sends the same new AWB to both at the same moment: one must answer 201 and the other 409, never a 500.

  3. Task 3

    assign.py: one parcel, one rider

    Sunita's dispatchers assign riders to pickups, and twice this week two riders went for the same parcel. Make an app (tables created at startup) with POST /parcels/{awb}/assign taking {"rider_id": ...}: if the parcel has no rider yet, save this one and answer 200 with {"awb": ..., "rider_id": ...}; if it already has one, answer 409 and change nothing; an AWB the database does not have answers 404.

    The check runs your file on two servers and sends two assignments of one parcel, to two different riders, at the same moment, with the database made to let both read before either writes: exactly one must win.

  4. Task 4 · interview question

    Interview: pickups.py, a retried request

    A merchant's app asks for a pickup, its request times out, and it sends the same request again. Make an app (tables created at startup) with POST /pickups taking {"merchant_id": ..., "slot": "morning" or "evening", "parcel_count": 1 to 50} and an optional Idempotency-Key header, saving a Pickup and answering 201 with {"id": ..., "merchant_id": ..., "slot": ..., "parcel_count": ...}. Without a key, each request makes a pickup. With a key, the first request makes one, and any later request with the same key and the same body makes nothing and gets exactly the first answer again; the same key with a different body answers 422.

    The check also sends a request and its retry to two servers at the same moment: one pickup, and both answers 201 with the same id. The interviewer will ask: a merchant's app retries after a timeout; how many pickups exist now, and how does the key change that?

What you will learn

  • Transactions: all of a change saved, or none of it.
  • Unique constraints turned into a 409.
  • The race: a check and an act with a gap, and two ways to close it.
  • Idempotency keys: a retried request that changes nothing twice.

More in Structure and data

Build "Transactions and concurrency" yourself

DakiyaGo, a Jaipur courier startup, takes bookings on WhatsApp. Merchants want an API, and you build it. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.