Case study: exchange rates and reference data replication
The exchange rates in this post are a stand-in. The real project used this pattern for a different kind of data, which a non-disclosure agreement keeps me from describing. The architecture, the failure modes and the trade-offs are the ones we worked through; the domain is made up.
Most systems I've worked on have a component that everything else quietly depends on. In this one it was an exchange rate service behind an HTTP API. It owned the rates: one per currency pair and country, built from a market feed plus the margins and overrides the treasury team set. Product pages, the cart, checkout, refunds and invoicing all called it, on nearly every request, to ask what a currency was worth.
Rates changed every couple of minutes: externally, when the market feed moved, and internally, when someone adjusted a margin for one country. Whenever they changed, two things had to be true:
- Everyone uses the latest rate. A stale rate is money lost on every conversion.
- Everyone uses the same rate. If the product page quotes €40 and checkout charges €42, that's worse than either rate being stale. It's a support ticket, a reconciliation problem, and sometimes a legal one.
The second requirement mattered as much as the first. Keep it in mind; it's where the interesting problems are.
Why scaling and caching didn't fix it
As traffic grew, the rates API became a bottleneck. Adding instances helped for a while. Then we added caches, on the callers and on the API itself, and those helped for a while too.
What neither fixed was availability. When the API had a bad few minutes, which became more frequent, every service that called it had a bad few minutes too. The caches had expiry times, so they emptied exactly when we needed them, and the whole platform degraded together.
The caches also made the second requirement worse. Each one expired at its own moment, so two services could hold rates two minutes apart and neither knew it.
The problem wasn't capacity. It was that one service sat on the request path of all the others.
The observation
There were a lot of rates, but not much data: about 20 MB in total, small enough to fit comfortably in every service's memory. And most consumers needed the dataset as a whole, not a few entries from it.
That changes the question. Instead of asking the rates service on every request, every service could hold every rate itself and convert from memory. The rates service's job would shrink to telling everyone when something changed.
Requests stop depending on the network at all. If everything else is down, a service still converts with the last rates it loaded.
The design
There are four parts.
The Rates API publishes. When a rate changes, it builds a snapshot of every rate, validates it,
and writes it as an immutable, numbered object: rates/v43.json. It writes to S3 first, then moves
a small current pointer. S3 is the canonical copy. Then it copies the same snapshot to Redis and
moves Redis's pointer.
Redis is the fast path, and the broadcast. We put Redis next to S3 because we expected it to be faster to read across the network, and because it can serve part of a snapshot as well as the whole thing. (S3 can read byte ranges too, so that difference is smaller than it sounds.) A Pub/Sub message tells every subscriber that v43 exists. The message carries only the version number; the snapshot stays in Redis for each instance to fetch. A thousand instances receiving 20 MB each through Pub/Sub, every two minutes, would be its own outage.
A shared SDK does the rest. We didn't want every team writing its own subscriber, cache and retry logic, and getting it slightly differently wrong. So every service embeds one library. It:
- subscribes to the broadcast, and also polls the version key every 10 seconds;
- fetches a new snapshot from Redis, falls back to S3, and as a last resort asks the Rates API;
- checks the snapshot's hash, then swaps it in with a single pointer write, so a request never sees half of v42 and half of v43;
- keeps the old snapshot if anything about the new one is wrong, and raises an alert;
- reports the version it holds, as a metric.
New instances start from S3. An instance that boots has nothing in memory. It subscribes to the broadcast first, so a change that lands while it loads can't slip past, then loads the current snapshot from S3 and only then passes its readiness check. Redis is a cache, and a cache can lose things. S3 is the one place a snapshot is guaranteed to be.
What each piece buys
Walk through the scenarios in the figure above and one pattern shows up: every failure degrades freshness, never availability.
- Redis misses a subscriber. Pub/Sub keeps nothing for later; a disconnected instance simply doesn't hear the message. The poll catches it within 10 seconds. The broadcast is a speed-up; the poll is the guarantee.
- Redis is down. No broadcast, and polls fail over to S3. Changes take up to one poll interval to land instead of a second.
- An instance restarts. It loads from S3, not Redis, and takes no traffic until it has rates.
- S3 and Redis are both down. Running instances don't notice. A booting instance falls back to the Rates API, the original design, rate-limited.
And because every instance reports its version, "is everyone on the same rates?" is a dashboard query, not a guess. An alert fires if instances disagree for longer than two poll intervals.
Where I expect pushback
I don't think this design is finished, and some of these I'd argue either way.
"The same rate" isn't guaranteed. It's eventually consistent. For a few hundred milliseconds after a change, and up to 10 seconds for an instance that missed the broadcast, two services can disagree. The reporting makes that window visible; it doesn't close it. The stronger fix is the one payment systems already use: stamp the rates version on the quote, and pass it along with the cart, with each SDK keeping the last few versions in memory. Then checkout charges the rate the customer was quoted, whenever the switch happened.
Serving from memory forever isn't always right. For most data, the last good copy is better than nothing. A rate from three hours ago, on a day the market moves 4%, is a liability. The SDK should expose the age of what it holds, and each caller should decide: a product page might show an old rate with a note, while checkout refuses to convert after ten minutes.
Falling back to the Rates API brings the old problem back. For one instance, it's fine. For a whole fleet rebooting while S3 is down, it's the original bottleneck, at the worst moment. A reasonable alternative is to bake the last known snapshot into the image, or to refuse to start.
Maybe Redis isn't needed at all. Polling a tiny version file on S3 every few seconds is cheap, and reading 20 MB from S3 in the same region is often fast enough. We assumed Redis would be faster; today I'd measure that before keeping a second store. With rates changing every two minutes, I'd keep the broadcast, but it's a fair argument.
20 MB to every instance, on every change, adds up. A thousand instances pulling a full snapshot is 20 GB out of Redis each time a rate moves. The usual answer is deltas: ship only what changed since the last version, which is what Netflix's Hollow does. We shipped whole snapshots because consumers used the dataset as a whole (because of the nature of the data), and a full snapshot is easy to validate and can't be applied out of order. At a bigger fleet, I'd revisit that.
A shared SDK is a shared coupling. It has to exist in every language the services are written in, and every fix ships through every team's next deploy. If the SDK also does the conversion, applying margins and rounding, then the SDK's version matters as much as the rates' version: the same snapshot read by two SDK versions can give two totals.
A bad rate reaches everyone at once. A feed glitch that misplaces a decimal point goes
everywhere in seconds. That's the requirement, not an accident: a canary rollout would contradict
"everyone on the same version". The protection has to come before publishing, with validation (no
rate moves 20% in two minutes without a human looking), and after it, with a rollback that is only
moving the current pointer back.
None of this is new. It's usually called reference data replication: copying a small, read-heavy dataset into every service that reads it. Hollow, service mesh control planes and feature flag SDKs all work this way. The property it buys has a name too: static stability, a system that keeps working on its last known state when its dependencies fail.
What made it right here was noticing that the "API" was really 20 MB of data that every service needed as a whole. The service didn't need to be on the request path to provide it.