Thread-Safe FX Rate Cache With Expiry and Single-Flight Loading
Last updated: November 29, 2026
Quick Overview
Implement a cache of FX rates shared by many request threads. Entries expire after a TTL, and when a rate is missing or stale only one thread should call the slow rate source while the others wait for its result.
Deutsche Bank
November 29, 20260
0
0 solved
Implement a cache of FX rates shared by many request threads. Entries expire after a TTL, and when a rate is missing or stale only one thread should call the slow rate source while the others wait for its result.
Pricing and payments services at Deutsche Bank read FX rates on every request, and a cache stampede on the rate service during market open can cause latency spikes. Java-heavy technical screens at the bank often test concurrency fundamentals; this version checks locking discipline, not holding locks during I/O, and failure handling.
What the Interviewer Expects
- Protect shared state with a lock and keep the critical section short
- Never call the slow loader while holding the lock
- Ensure only one in-flight load per key, others wait
- Handle loader failure so waiters do not hang
- Make time injectable for testing
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you serve a slightly stale rate while refreshing in the background?
- How does this map to ConcurrentHashMap.computeIfAbsent or CompletableFuture in Java, and what is the pitfall of computeIfAbsent with slow loaders?
- How would you cap memory if the number of currency pairs or tenors grows?
- What would you monitor in production?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
get(pair) returns a rate. If cached and younger than TTL, return immediately. Otherwise exactly one caller per pair loads from the source; concurrent callers for the same pair wait and then reuse th...
High-Level Design
_data: pair to(rate, fetched_at)._inflight: pair to athreading.Eventowned by the leader thread.- One lock guards both dicts. Under the lock a thread either returns a fresh value, beco...