Design a Currency Conversion Service
Calculate repeatable currency conversions from coherent, freshness-labeled rate snapshots without moving money.
Problem statement
For this exercise, assume clients need indicative currency conversions without moving money. A trusted publisher supplies complete immutable rate snapshots for 100 supported currency identifiers, about once per minute.
Each snapshot contains a source as-of time, one base currency, a positive exact ratio r[C] represented by integer numerator and denominator each from 1 through 10^12, meaning C major units per one base major unit, and each supported currency's minor-unit precision from 0 to 3 digits.
Convert a nonnegative integer source amount in minor units to target minor units using one snapshot's rates and precision metadata, preserving exact intermediate arithmetic and rounding only the final result to nearest with ties to even.
A latest conversion uses one observed complete active snapshot and returns its ID and source as-of time. When that source time is over five minutes old, latest conversion is explicitly unavailable; a successful poll must not make old rates look fresh.
A caller may request a specific snapshot retained within 24 hours for repeatable historical calculation, clearly labeled historical. Retrying latest may observe a newer snapshot; replaying the returned snapshot ID must reproduce the original amount.
Unknown currencies, malformed amounts, unavailable/expired snapshots and incomplete feed updates have explicit outcomes. Design snapshot ingestion and validation, coherent publication, caching, conversion APIs, precision, freshness and recovery.
Trading, money transfers, executable-price guarantees, fees/spreads, billing and aggregating multiple providers are outside scope.
What to build
- A trusted publisher can supply a complete validated immutable rate/precision snapshot that becomes active without exposing partial or backward state.
- A client can convert an integer minor-unit amount using latest or an explicit retained snapshot, with deterministic rounding and the selected snapshot identity.
- Clients can distinguish fresh latest, explicit historical, stale latest, missing/expired snapshot, invalid amount and unsupported currency outcomes.
At scale
- For this exercise, assume 100 supported currencies and 100,000 conversions per second; target 100 ms p95 calculation latency under healthy load.
- For this exercise, a complete snapshot arrives about once per minute; latest conversion is unavailable when the selected source as-of time is more than five minutes old. Retain immutable snapshots for 24 hours.
- For this exercise, input amounts are integers from 0 through 10^12 minor units, supported precision is 0 to 3 digits and all published rate ratios are positive exact ratios represented by integer numerators and denominators from 1 through 10^12 that the service validates before activation.
- For this exercise, a single-zone failure may degrade reads but must not expose a partial snapshot, change an immutable snapshot's meaning or relabel old rates as fresh.