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

Interbank Fund Settlement Ledger

hard—No data yet

Problem statement

Implement the settlement path of an interbank fund transfer ledger: both legs of a transfer and its two ledger rows survive together or not at all, and concurrent settlements never lose an update.

Requirements

TransferService

settle(TransferRequest request)

  • Settle amountCents from the account holding fromIban to the account holding toIban as one all-or-nothing unit of work: the money leaves the source through DebitService, reaches the destination through CreditService, and the ledger rows are appended, all together or not at all
  • Mint a transferId that is new for every settlement and append exactly two ledger rows carrying it: one for the source account holding amountCents as a negative number and one for the destination account holding amountCents as a positive number, each row recording the moment it was appended
  • Return the minted transferId together with fromIban, toIban and amountCents
  • If either leg fails, nothing survives the attempt: both balances read back exactly as they were and the ledger gains no rows at all
  • A failing leg's own error is left alone rather than replaced, so the caller is answered with 500 and the message that leg raised, which is "Injected failure on the debit leg" or "Injected failure on the credit leg"
  • Reject an amountCents below 1 with 400 and the message "Amount must be at least 1", where an absent amount counts as 0, so an amount of exactly 1 is accepted. This is checked before anything else
  • Reject a request whose fromIban equals its toIban with 400 and the message "Source and destination must differ". This is checked next
  • Reject an unknown fromIban or toIban with 404 and the message "Account not found with iban: " followed by the unknown iban; the source is looked up before the destination
  • Reject an amount greater than the source balance with 409 and the message "Insufficient funds". This is checked last, so settling the entire source balance is allowed and leaves that account at 0
  • A rejected request writes nothing: every balance and the whole ledger stay untouched
  • The ledger is append-only: a row that has been written keeps its id, transferId, accountId, amountCents and createdAt for good, the row count only ever grows, and no route changes or removes a row
  • Every transferId in the ledger sums to 0 and the whole ledger sums to 0, whatever mix of settled and rejected requests came before
  • Settlements running at the same time between the same pair of accounts never lose an update: whichever attempts succeed, each account ends at its starting balance adjusted by exactly those settlements, no balance ever drops below 0, and the ledger holds exactly two rows per successful settlement
  • An attempt that loses such a race is turned away for that attempt only, so a caller that submits it again settles it normally as long as the funds are still there, and every settlement in a concurrent batch that the source balance can cover ends up applied

TransferController

createTransfer(TransferRequest request)

  • Answer POST /transfers with 201 Created and the settlement result as the response body
  • A rejected settlement answers with the status and the message listed above

Provided settlement machinery

  • The two settlement legs are separate provided beans: DebitService takes money off an account and CreditService puts money onto one; a support FaultInjector can script either leg to fail the instant after it has moved the money
  • GET /accounts and GET /ledger are provided read-only listings; three accounts are seeded at startup for manual play
  • The entities, repositories, request and response types, domain exceptions and the error handler rendering bodies with error, timestamp, and path fields are provided

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /transfers

Request:

{
   "fromIban": "DE21STAR0000000001",
   "toIban": "FR76STAR0000000002",
   "amountCents": 12500
}

Response (201 Created):

{
   "transferId": "f6269bdf-9afe-4a07-9ec1-7811fb630ea2",
   "fromIban": "DE21STAR0000000001",
   "toIban": "FR76STAR0000000002",
   "amountCents": 12500
}

GET /accounts

Response (200 OK):

[
   {
      "iban": "DE21STAR0000000001",
      "balanceCents": 487500
   },
   {
      "iban": "FR76STAR0000000002",
      "balanceCents": 262500
   },
   {
      "iban": "GB33STAR0000000003",
      "balanceCents": 0
   }
]

GET /ledger

Response (200 OK):

[
   {
      "id": 1,
      "transferId": "f6269bdf-9afe-4a07-9ec1-7811fb630ea2",
      "accountId": 1,
      "amountCents": -12500,
      "createdAt": "2026-08-11T09:07:36.779679Z"
   },
   {
      "id": 2,
      "transferId": "f6269bdf-9afe-4a07-9ec1-7811fb630ea2",
      "accountId": 2,
      "amountCents": 12500,
      "createdAt": "2026-08-11T09:07:36.821027Z"
   }
]

POST /transfers

Request:

{
   "fromIban": "GB33STAR0000000003",
   "toIban": "DE21STAR0000000001",
   "amountCents": 1
}

Response (409 Conflict):

{
   "error": "Insufficient funds",
   "timestamp": "2026-08-11T09:07:36.937772Z",
   "path": "/transfers"
}

POST /transfers

Request:

{
   "fromIban": "DE21STAR0000000001",
   "toIban": "NL99STAR0000000009",
   "amountCents": 100
}

Response (404 Not Found):

{
   "error": "Account not found with iban: NL99STAR0000000009",
   "timestamp": "2026-08-11T09:07:36.986842Z",
   "path": "/transfers"
}

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 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 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.