StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 12 · Microservices and Resilience

Fallback Cache for Downstream Outage

hard0%0 of 1 solved

Problem statement

Complete the rate lookup of a currency service that has to keep answering while the rate feed it depends on is unavailable, without ever serving a rate that has gone too stale.

Requirements

RateController

rate(String currency)

  • Answer GET /rates/{currency} with the rate RateService produces for that currency

RateService

getRate(String currency)

  • On every request, ask the peer for that currency at /peers/rates/ followed by the currency, over HTTP, through the provided HTTP client, on the address the provided directory resolves at the moment of the call; a remembered rate is never a reason to skip the peer
  • When the peer answers, serve that answer as 200 carrying currency, rateBasisPoints and asOf exactly as the peer reported them, plus degraded false, and nothing else
  • Remember the peer's most recent answer for each currency, its rate and its asOf together, replacing whatever was remembered for that currency before
  • The peer counts as unavailable when it answers with an error status, when it does not answer inside the configured call time limit, or when its address accepts no connection
  • While the peer is unavailable, serve the remembered rate for that currency as 200 with degraded true and the asOf the peer reported when that rate was fetched, never a freshly stamped one; failed attempts leave the remembered rate in place, however many of them there are
  • Refuse a remembered rate that has aged past the staleness window rather than serve it: when the time between its asOf and the current time on the provided clock is longer than rates.max-stale-ms milliseconds, report the missing fresh rate, answered as 503 with the message "No fresh rate available for " followed by the currency. A remembered rate whose age is exactly the window is still served
  • A currency with nothing remembered yet is refused the same way while the peer is unavailable
  • Remembered rates are kept per currency: one currency's remembered rate never answers a request for another, and every currency keeps its own rate and its own asOf
  • As soon as the peer answers again, answers go back to live with degraded false and the peer's new asOf, and the remembered rate is replaced, so a later outage serves the newer rate and a currency that was being refused starts answering again
  • The staleness window rates.max-stale-ms and the call time limit rates.peer-timeout-ms are both read from configuration in milliseconds and are never written into the code; the provided HTTP client already carries the call time limit

Provided rate feed and support

  • GET /peers/rates/{currency} is the rate feed: it reports a currency's rate in basis points together with the asOf instant it was priced, answers for any currency code, and records every inbound call it receives
  • PeerScript makes that peer answer normally, answer with an error, answer far too slowly, or drop off the network; POST /peers/control/mode/{mode}, POST /peers/control/slow/{millis}, POST /peers/control/reset and GET /peers/control/calls/{currency} drive it while you play with the running application
  • PeerDirectory resolves the peer's address, which is only known once the application is running and changes when the peer drops off the network
  • An HTTP client bean carrying the configured call time limit, a clock bean supplying the current time against which staleness is measured, the two response records, the error body and NoFreshRateException are all provided

Sample Requests & Responses

Expand to view details on sample requests and responses.

GET /rates/USD

Response (200 OK):

{
   "currency": "USD",
   "rateBasisPoints": 10850,
   "degraded": false,
   "asOf": "2026-08-11T10:02:34.975942Z"
}

GET /rates/JPY

Response (200 OK):

{
   "currency": "JPY",
   "rateBasisPoints": 68,
   "degraded": false,
   "asOf": "2026-08-11T10:02:34.992579Z"
}

POST /peers/control/mode/SERVER_ERROR

Response (200 OK):

{
   "mode": "SERVER_ERROR",
   "delayMillis": 0
}

GET /rates/USD while the peer is unavailable

Response (200 OK):

{
   "currency": "USD",
   "rateBasisPoints": 10850,
   "degraded": true,
   "asOf": "2026-08-11T10:02:34.975942Z"
}

GET /rates/CHF while the peer is unavailable and nothing is remembered for CHF

Response (503 Service Unavailable):

{
   "error": "No fresh rate available for CHF",
   "timestamp": "2026-08-11T10:02:35.030088Z",
   "path": "/rates/CHF"
}

GET /peers/control/calls/USD

Response (200 OK):

{
   "currency": "USD",
   "calls": 2
}

GET /rates/USD after the peer is set back to HEALTHY

Response (200 OK):

{
   "currency": "USD",
   "rateBasisPoints": 10850,
   "degraded": false,
   "asOf": "2026-08-11T10:02:35.068013Z"
}

GET /rates/USD while the peer is set to answer 5000 ms too slowly

Response (200 OK):

{
   "currency": "USD",
   "rateBasisPoints": 10850,
   "degraded": true,
   "asOf": "2026-08-11T10:02:35.068013Z"
}

More in Microservices and Resilience

See the module
Service Registry Lookup ClientThis problem covers looking a service address up before each call, then reaching that address over the network.—No data yethardEdge Router with Rate LimitingThis problem covers passing calls on to backing services by their path, while holding each caller to a call allowance.—No data yethardRide Hailing Dispatch PlatformThis problem covers coordinating three network calls, backing off from one that keeps failing, and undoing work that cannot be completed.50%1 of 2 solvedhard

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.