Design an idempotent request processor that replays cached results by request ID

Quick Overview

Implement an idempotent request processor that parses JSON requests, caches each result by request ID, and returns the original result when a request is replayed. It tests hash map design, idempotency semantics, and edge cases such as a reused ID with a new payload or a lookup for an unknown ID.

Design an idempotent request processor that replays cached results by request ID

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design an idempotent request processing system. The system receives incoming requests as JSON strings. Each request contains a `request_id` (a unique identifier string) and a `payload` (a data string). Processing a request produces a result of the form `"processed:<payload>"`. The system must guarantee idempotency: if a request with the same `request_id` is submitted more than once, the system returns the original result from the first processing without processing the request again. The system should also support querying whether a specific `request_id` has been processed, retrieving the cached result for a given `request_id`, and reporting the total number of unique requests processed. Implement a class `IdempotentRequestProcessor` with the following methods: - `processRequest(request_json: str) -> str` parses the JSON request string and extracts `request_id` and `payload`. If the `request_id` was already processed, it returns the original cached result. Otherwise it computes `"processed:<payload>"`, caches the result, and returns it. - `isProcessed(request_id: str) -> bool` returns `True` if a request with the given `request_id` has already been processed, `False` otherwise. - `getResult(request_id: str) -> str` returns the cached result for the given `request_id`, or an empty string if that `request_id` has not been processed. - `getProcessedCount() -> int` returns the total number of unique requests processed so far. For example, a request that is submitted twice is processed once: ```text p = IdempotentRequestProcessor() p.processRequest('{"request_id": "r1", "payload": "charge_100"}') -> "processed:charge_100" p.processRequest('{"request_id": "r1", "payload": "charge_100"}') -> "processed:charge_100" (replayed from the cache) p.isProcessed("r1") -> True p.getProcessedCount() -> 1 ``` A replay that reuses a `request_id` with a different payload still gets the first result, and unknown ids report nothing: ```text p = IdempotentRequestProcessor() p.processRequest('{"request_id": "a", "payload": "x"}') -> "processed:x" p.processRequest('{"request_id": "a", "payload": "y"}') -> "processed:x" p.getResult("b") -> "" p.isProcessed("b") -> False p.processRequest('{"request_id": "b", "payload": "y"}') -> "processed:y" p.getProcessedCount() -> 2 ``` ```hint What a replay needs A replay has to return exactly the string the first call returned. Decide what you store and which field you look it up by, so that each of the four methods becomes a single lookup. ``` ### Constraints & Assumptions - `1 <= request_id.length <= 100` - `1 <= payload.length <= 200` - Every request string is valid JSON containing `"request_id"` and `"payload"` fields. - At most 10,000 calls in total across all methods. ### Clarifying Questions to Ask - If a replay reuses a `request_id` with a different payload, should it return the first result or report an error? - Can two requests with the same `request_id` arrive at the same time, or are calls made one at a time? - Do results need to survive a process restart, or is in-memory storage enough? - Is there a limit on how long a `request_id` must be remembered? ### What a Strong Answer Covers - Which field the cache is keyed on, what value it stores, and why that guarantees a replay returns the original result. - The time cost of each method, including the JSON parse in `processRequest`, and the memory used by the cache. - How the design behaves for a replay with a different payload and for lookups of ids that were never processed. - How `getResult` signals a missing id and whether that value can be confused with a real result. - How the request is parsed so that escaped characters in the payload come through intact. ### Follow-up Questions - How would you make `processRequest` safe when two threads submit the same `request_id` at the same moment? - The cache grows without bound. How would you expire old entries, and what happens when a client retries after its entry is gone? - How would you store results so they survive a restart and are shared by several server instances? - If processing could fail partway through, which outcomes would you cache and which would you let the client retry?

Overview: Implement an idempotent request processor that parses JSON requests, caches each result by request ID, and returns the original result when a request is replayed. It tests hash map design, idempotency semantics, and edge cases such as a reused ID with a new payload or a lookup for an unknown ID.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an idempotent request processing system. The system receives incoming requests as JSON strings. Each request contains a request_id (a unique identifier string) and a payload (a data string). Processing a request produces a result of the form "processed:<payload>".

The system must guarantee idempotency: if a request with the same request_id is submitted more than once, the system returns the original result from the first processing without processing the request again.

The system should also support querying whether a specific request_id has been processed, retrieving the cached result for a given request_id, and reporting the total number of unique requests processed.

Implement a class IdempotentRequestProcessor with the following methods:

  • processRequest(request_json: str) -> str parses the JSON request string and extracts request_id and payload . If the request_id was already processed, it returns the original cached result. Otherwise it computes "processed:<payload>" , caches the result, and returns it.
  • isProcessed(request_id: str) -> bool returns True if a request with the given request_id has already been processed, False otherwise.
  • getResult(request_id: str) -> str returns the cached result for the given request_id , or an empty string if that request_id has not been processed.
  • getProcessedCount() -> int returns the total number of unique requests processed so far.

For example, a request that is submitted twice is processed once:

p = IdempotentRequestProcessor()
p.processRequest('{"request_id": "r1", "payload": "charge_100"}')  -> "processed:charge_100"
p.processRequest('{"request_id": "r1", "payload": "charge_100"}')  -> "processed:charge_100"   (replayed from the cache)
p.isProcessed("r1")                                                -> True
p.getProcessedCount()                                              -> 1

A replay that reuses a request_id with a different payload still gets the first result, and unknown ids report nothing:

p = IdempotentRequestProcessor()
p.processRequest('{"request_id": "a", "payload": "x"}')  -> "processed:x"
p.processRequest('{"request_id": "a", "payload": "y"}')  -> "processed:x"
p.getResult("b")                                         -> ""
p.isProcessed("b")                                       -> False
p.processRequest('{"request_id": "b", "payload": "y"}')  -> "processed:y"
p.getProcessedCount()                                    -> 2

Constraints & Assumptions

  • 1 <= request_id.length <= 100
  • 1 <= payload.length <= 200
  • Every request string is valid JSON containing "request_id" and "payload" fields.
  • At most 10,000 calls in total across all methods.

Clarifying Questions to Ask Guidance

  • If a replay reuses a request_id with a different payload, should it return the first result or report an error?
  • Can two requests with the same request_id arrive at the same time, or are calls made one at a time?
  • Do results need to survive a process restart, or is in-memory storage enough?
  • Is there a limit on how long a request_id must be remembered?

What a Strong Answer Covers Guidance

  • Which field the cache is keyed on, what value it stores, and why that guarantees a replay returns the original result.
  • The time cost of each method, including the JSON parse in processRequest , and the memory used by the cache.
  • How the design behaves for a replay with a different payload and for lookups of ids that were never processed.
  • How getResult signals a missing id and whether that value can be confused with a real result.
  • How the request is parsed so that escaped characters in the payload come through intact.

Follow-up Questions Guidance

  • How would you make processRequest safe when two threads submit the same request_id at the same moment?
  • The cache grows without bound. How would you expire old entries, and what happens when a client retries after its entry is gone?
  • How would you store results so they survive a restart and are shared by several server instances?
  • If processing could fail partway through, which outcomes would you cache and which would you let the client retry?