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

Ticket Counter Race Guard

hard—No data yet

Problem statement

Complete the claim endpoint of a box-office ticket counter: concurrent claims never lose an update, a collision is retried invisibly, and a request that keeps colliding gives up cleanly.

Requirements

ClaimController

claim handles POST /pools/{id}/claim

  • Take the pool id from the path and a JSON body holding quantity, and answer 200 OK with poolId, the remaining tickets left after the claim, and attempts
  • Reject a body that carries no quantity with 400 Bad Request and error message: "Quantity is required"
  • Reject a quantity below 1 with 400 Bad Request and error message: "Quantity must be at least 1"; a quantity of 1 is accepted
  • A rejected body takes no tickets and starts no attempt at all

TicketClaimService

claim(Long poolId, int quantity)

  • Give one request at most 3 attempts at the whole read-check-decrement of the pool
  • When an attempt fails because another writer changed that pool between the read and the write, start a fresh attempt that reads the pool again
  • Report in attempts how many attempts this request made, counting the one that succeeded, so a claim that meets no interference reports 1; each request reports only its own attempts, and interference with one pool never inflates the count of a claim against another pool
  • A retried claim still looks like a plain success to the caller: one response, a success status, and no error field
  • Pause before every fresh attempt: at least the number of milliseconds held in the tickets.claim.retry-backoff-ms configuration property before the second attempt, and at least twice that before the third. The pause must always be derived from whatever value that property holds, never from a fixed number. Vary the exact length within that growing window, so two callers that collided once do not line up again
  • When all 3 attempts fail this way, answer 409 Conflict with error message: "Ticket pool is busy, please retry", and leave the pool holding exactly the tickets it held before

ClaimAttemptService

applyClaim(Long poolId, int quantity)

  • Record the attempt with the provided AttemptLog as the attempt starts, before the pool is read, so an attempt against a pool that turns out to be unknown is recorded too
  • An unknown pool id is reported as 404 Not Found with error message: "Pool not found with id: " followed by the requested id
  • A pool holding fewer tickets than quantity is reported as 409 Conflict with error message: "Not enough tickets remaining", and the pool keeps every ticket it had; a quantity equal to what remains is served and empties the pool
  • Otherwise reduce the pool's remaining by quantity and store it, as one all-or-nothing unit of work, so an attempt that does not complete leaves nothing behind
  • Concurrent claims against one pool never lose an update: every stored decrement counts exactly once, the pool never drops below zero, and the tickets handed out plus the tickets left always add up to the stock the pool started with

Provided pool machinery

  • The TicketPool entity, its repository, and GET /pools/{id} are provided; three pools are seeded at startup for manual play
  • TicketPool already carries the bookkeeping the persistence layer needs in order to notice that another writer changed a pool row first
  • AttemptLog stamps every attempt you record with the moment it started; CollisionInjector lets the tests slip a competing committed write into a pool row just before your own write reaches the database, so interference can be scripted instead of waited for

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /pools/1/claim

Request:

{
   "quantity": 2
}

Response (200 OK):

{
   "poolId": 1,
   "remaining": 8,
   "attempts": 1
}

GET /pools/1

Response (200 OK):

{
   "id": 1,
   "eventName": "Riverside Jazz Night",
   "remaining": 8
}

POST /pools/2/claim

Request:

{
   "quantity": 5
}

Response (409 Conflict):

{
   "error": "Not enough tickets remaining",
   "timestamp": "2026-08-11T09:12:14.382714Z",
   "path": "/pools/2/claim"
}

POST /pools/1/claim

Request:

{
}

Response (400 Bad Request):

{
   "error": "Quantity is required",
   "timestamp": "2026-08-11T09:12:14.420021Z",
   "path": "/pools/1/claim"
}

POST /pools/99/claim

Request:

{
   "quantity": 1
}

Response (404 Not Found):

{
   "error": "Pool not found with id: 99",
   "timestamp": "2026-08-11T09:12:14.441522Z",
   "path": "/pools/99/claim"
}

More in Transactions and Concurrency

See the module
Seat Hold Reservation LockThis problem covers holding a seat for a while, letting the hold lapse on its own, and making sure a crowd produces only one winner.—No data yethardWallet Top-Up Retry GuardThis problem covers making a repeated top-up request credit the wallet only once—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.