StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 11 · Transactions and Concurrency

Seat Hold Reservation Lock

hard—No data yet

Problem statement

Complete the seat hold core of a ticketing API: a crowd racing for one seat produces exactly one holder, and a hold lapses on its own when its lifetime runs out.

Requirements

SeatRepository

  • Add the seat lookup the hold flow needs: while one request is deciding about a seat, every other request asking for that same seat waits until the first request's unit of work has finished, so two requests can never decide about the same seat at the same moment

SeatHoldService

placeHold(Long seatId, String holderId)

  • Unknown seat id: report the missing seat, answered as 404 with the message "Seat not found with id: " followed by the requested id, and store nothing
  • The seat already carries an active hold: report the clash, answered as 409 with the message "Seat already held", and leave the stored hold exactly as it was; this also applies when the request comes from the holder that already holds the seat
  • Otherwise store a new hold for holderId with heldAt set to the current time and expiresAt set to the current time plus the hold lifetime, and return it
  • The hold lifetime is read from the seats.hold-ttl-ms property in milliseconds and is never written into the code; the shipped configuration gives a hold a five-minute lifetime
  • Every time value comes from the application's clock bean, never from the system clock directly
  • A hold is active for as long as the current time is before its expiresAt; from that instant onwards it holds nothing at all, with no release step of any kind, so the seat can be held again and reads as available in the seat listing
  • One millisecond before its expiresAt a hold is still active and still turns a new holder away
  • Each hold expires on its own schedule: a hold placed later stays active after an earlier hold on another seat has expired
  • While a seat carries an active hold it reads as unavailable in the seat listing, and the other seats keep reading as available
  • When several requests reach the same free seat at the same moment, exactly one of them stores a hold and answers 201 while every other request answers 409, and the seat is left with exactly one stored hold; this holds whether the seat had never been held or had just fallen free because its hold expired
  • Requests aimed at different seats do not block one another's outcome: requests that arrive at the same moment for two different seats all succeed, and a crowd split across two seats produces exactly one winner per seat

releaseHold(Long seatId, String holderId)

  • Unknown seat id: report the missing seat, answered as 404 with the message "Seat not found with id: " followed by the requested id, and leave every stored hold alone
  • The seat has no active hold, whether it was never held, was already released, or its hold has expired: report the missing hold, answered as 404 with the message "No active hold for seat " followed by the requested id
  • The active hold belongs to a different holder: report the mismatch, answered as 403 with the message "Hold belongs to another holder", and leave the hold in place so its owner can still release it
  • Otherwise remove the hold; the seat is available again straight away and any holder, including a different one, can take it
  • Releasing one seat leaves the holds on other seats untouched

SeatHoldController

holdSeat(Long seatId, HoldRequest request)

  • Serve the hold at POST /seats/{seatId}/hold with a JSON body carrying holderId, and answer 201 with seatId, holderId and expiresAt when the hold is stored

releaseHold(Long seatId, HoldRequest request)

  • Serve the release at DELETE /seats/{seatId}/hold with a JSON body carrying holderId, and answer 204 with an empty body when the hold is removed

Provided seats endpoint and harness

  • GET /seats lists every seat with a computed available flag, evaluated against the application clock; four seats are seeded at startup
  • Time comes from a provided clock bean that the tests can pin and move forward; a provided ContentionHarness releases many requests at the same instant
  • The error responses carry error, timestamp, and path fields with the exact messages above

Sample Requests & Responses

Expand to view details on sample requests and responses.

GET /seats

Response (200 OK):

[
   {
      "id": 1,
      "seatNumber": "A1",
      "available": true
   },
   {
      "id": 2,
      "seatNumber": "A2",
      "available": true
   },
   {
      "id": 3,
      "seatNumber": "A3",
      "available": true
   },
   {
      "id": 4,
      "seatNumber": "A4",
      "available": true
   }
]

POST /seats/1/hold

Request:

{
   "holderId": "guest-42"
}

Response (201 Created):

{
   "seatId": 1,
   "holderId": "guest-42",
   "expiresAt": "2026-08-11T09:14:23.455460Z"
}

POST /seats/1/hold while the seat is held

Request:

{
   "holderId": "guest-99"
}

Response (409 Conflict):

{
   "error": "Seat already held",
   "timestamp": "2026-08-11T09:09:23.480218Z",
   "path": "/seats/1/hold"
}

GET /seats while seat 1 is held

Response (200 OK):

[
   {
      "id": 1,
      "seatNumber": "A1",
      "available": false
   },
   {
      "id": 2,
      "seatNumber": "A2",
      "available": true
   },
   {
      "id": 3,
      "seatNumber": "A3",
      "available": true
   },
   {
      "id": 4,
      "seatNumber": "A4",
      "available": true
   }
]

DELETE /seats/1/hold from a different holder

Request:

{
   "holderId": "guest-99"
}

Response (403 Forbidden):

{
   "error": "Hold belongs to another holder",
   "timestamp": "2026-08-11T09:09:23.502797Z",
   "path": "/seats/1/hold"
}

DELETE /seats/1/hold from the holder

Request:

{
   "holderId": "guest-42"
}

Response (204 No Content) with an empty body.

DELETE /seats/2/hold for a seat nobody holds

Request:

{
   "holderId": "guest-42"
}

Response (404 Not Found):

{
   "error": "No active hold for seat 2",
   "timestamp": "2026-08-11T09:09:23.528681Z",
   "path": "/seats/2/hold"
}

POST /seats/99/hold for a seat that does not exist

Request:

{
   "holderId": "guest-42"
}

Response (404 Not Found):

{
   "error": "Seat not found with id: 99",
   "timestamp": "2026-08-11T09:09:23.537100Z",
   "path": "/seats/99/hold"
}

More in Transactions and Concurrency

See the module
Wallet Top-Up Retry GuardThis problem covers making a repeated top-up request credit the wallet only once—No data yethardTicket Counter Race GuardThis problem covers claiming tickets while other writers change the same pool, and starting over when someone got there first.—No data yethardInterbank Fund Settlement LedgerThis problem covers moving money between two accounts so that either both sides happen or neither does.—No data yethard

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.