Allowed R6 · Within policy
{
"slug": "idempotent-bill-payment",
"title": "Idempotent Payment",
"difficulty": "medium",
"tags": [
"coding",
"technical",
"backend"
],
"is_free": true,
"locked": false,
"frequency": 1,
"last_reported_at": "2026-08-08T20:54:15.530427Z",
"url": "https://www.mokabla.com/en/c/fawry/idempotent-bill-payment",
"agent": {
"page": "https://www.mokabla.com/agent/en/c/fawry/idempotent-bill-payment",
"question": "https://www.mokabla.com/agent/_call/get_question?company=fawry&question=idempotent-bill-payment"
},
"company": {
"slug": "fawry",
"name_en": "Fawry"
},
"body_md": "## Problem\nA POS terminal pays an electricity bill through our Spring service. Terminals sit on flaky 3G, so they\nretry the same `POST /payments` whenever they do not get a response. Implement `PaymentService.pay(request)`\nso a bill is charged at most once no matter how many copies of the request arrive, including two copies\nhitting different pods at the same time. Each request carries a terminal-generated `requestId`, a\n`billRef` and an `amount`. The first call performs the debit and stores the outcome; every later call with\nthe same `requestId` returns the same outcome without touching the biller. Reusing a `requestId` with a\ndifferent `amount` must be rejected.\n\n## Examples\n**Example 1**, `pay({requestId: \"t9-0012\", billRef: \"EL-4471\", amount: 350})` → `{status: \"PAID\",\ntxnId: \"TX1\"}`; the identical call again → `{status: \"PAID\", txnId: \"TX1\"}` and the biller was called\nexactly once.\n\n**Example 2**, `pay({requestId: \"t9-0012\", billRef: \"EL-4471\", amount: 400})` after Example 1 →\n`{status: \"REJECTED\", reason: \"IDEMPOTENCY_MISMATCH\"}`.\n\n## Constraints\n- Two identical requests may hit two pods within the same millisecond; the database is the only shared state\n- The biller call can take up to 8 s; do not hold a DB lock for its duration\n- Idempotency records expire after 24 h\n\n## What they look for\nA unique constraint on `request_id` used as the lock (insert an `IN_PROGRESS` row first and catch the\nduplicate-key exception), a state machine `IN_PROGRESS → PAID / FAILED`, what to return while a twin\nrequest is still in progress (409 or wait), and how `@Transactional` boundaries interact with the slow\nbiller call. Bonus: hash the payload to detect mismatches instead of comparing fields one by one.",
"follow_ups_md": "- The biller is itself not idempotent. What do you send it?\n- A terminal replays a request after 25 hours. Charge again or refuse?",
"access": "Question bodies are paid content: a person buys 30 days of access per company (200 EGP or 200 points) in the human app. Agents get titles, tags and difficulty; bodies only for free questions."
}