Başar BallıözProduct Engineer
← All posts

Multi-currency expenses: exchange rates, caching and money in minor units

A driver in Istanbul pays for fuel in lira, rents a car abroad in euros and buys a part online in dollars. RPMBase has to add all of that into one honest number. That needs three things: a safe way to store money, a reliable source of exchange rates, and a rule for what to do when a rate is missing.

By Başar Ballıöz · · 5 min read

The short answer

  • Store every amount with its own currency, and keep an integer amount in minor units (cents, kuruş) next to it. Never convert at save time.
  • Convert only when you display a total, through one base currency, with a rate table fetched by your server.
  • Fetch rates from more than one free source, validate them, cache them for a day in a place every server instance can read, and let only one instance refresh at a time.
  • Never invent a rate. If one is missing, leave that item out of the total and say so.

Here is how that works across RPMBase's Node.js API and its React Native app.

Which costs are worth logging in the first place is covered in RPMBase's guide to what car expenses to track.

1. Storing money: original currency, minor units

RPMBase supports four currencies: Turkish lira, euro, US dollar and pound sterling, with lira as the default. Every service, fuel and expense record keeps:

  • currencyCode: what the owner actually paid in;
  • costMinor: the amount as an integer in minor units, so 1,250.50 is 125050;
  • cost: the same amount as a decimal rounded to two places, for convenience.

The server normalizes every amount on the way in: an unknown currency is rejected, a negative cost is rejected, a missing currency falls back to lira, and the integer form is computed from the decimal one when the client only sends a decimal. Floating point is fine for showing a number and dangerous for adding thousands of them; integers in minor units add up exactly.

The record is never rewritten into another currency. If someone switches their display currency from lira to euro, nothing in the database changes. Only the totals on screen do.

2. Where the rates come from: three free APIs, in order

The server fetches one rate table with US dollars as the base. It tries three free sources in order and takes the first usable answer:

  1. the free currency-api on its pages.dev host,
  2. the same dataset through the jsDelivr CDN, a mirror that keeps working when the first host does not,
  3. Frankfurter, which publishes European Central Bank reference rates.

Each request has a 3-second timeout, so a slow provider cannot hold up the app. "Usable" is strict: the table must contain a positive, finite rate for every supported currency. A response that is missing the lira, or has a zero in it, counts as a failure and the next provider is tried. A half-complete table would quietly produce wrong totals, which is worse than no table.

The same failover order runs in RPMVault's backend, so both apps behave the same way when a provider goes down.

3. A 24-hour cache that works on serverless

Exchange rates for a car log do not need to be live to the second. A table is good for 24 hours. The tricky part is where to keep it: the API runs as serverless functions, and each instance has its own memory. A cache that lives only in memory means every cold instance fetches again.

So there are two levels:

  • Memory, for the instance that already has a fresh table;
  • MongoDB, a single shared document every instance reads before considering a fetch.

When the shared copy has expired, an instance must take a short lease, a lock that expires on its own after a few seconds, before calling any provider. If another instance already holds it, this one does not wait and does not fetch. It serves the last good table, marked as stale. That keeps a burst of traffic after expiry from turning into a burst of calls to free APIs that would rather not be hammered.

If every provider fails, the last good table is served marked stale. Only if there has never been a good table does the server answer with a table containing the dollar alone, also marked stale, which simply means "cannot convert yet".

One small serverless lesson: the admin status page reads the MongoDB copy, not the instance's memory. On serverless, the admin request often lands on an instance that never served a single rate, and its memory would say "no rates" while users are converting happily.

4. Converting a total on the phone

The app receives the table, keeps a copy on the device for offline use, and adds up totals itself:

  1. For each item, take its amount in minor units and its currency.
  2. If its currency is the display currency, add it as it is.
  3. Otherwise convert through the base: to dollars with the item's rate, then to the display currency with that rate, and round back to whole minor units.
  4. If either rate is missing, skip the item and count it.

The result carries the total, the display currency and how many items were skipped. A total that silently dropped a euro bill would look complete and be wrong; with the count, the screen can say that the total is incomplete instead of pretending.

5. The trade-off I accepted

Totals use today's rates, not the rate on the day of payment. A 2024 invoice in euros counts at this week's euro rate when shown in lira. For a personal car log, that is the honest reading of "what has this car cost me, in today's money", and it keeps the records simple. It would be the wrong choice for accounting, where the rate on the transaction date is the one that matters.

Common questions

Is there a free exchange rate API without an API key?

Yes. The open currency-api dataset (also served through jsDelivr) and Frankfurter both work without a key. Use more than one and validate what they return.

Should I store money as a float or an integer?

Store an integer in minor units for anything you add up, together with the currency code. Use decimals only for display.

Should I convert expenses to one currency when saving them?

No. Save what was paid, in the currency it was paid in. Convert when you show totals, so a change of display currency or a better rate never rewrites history.

What I learned

Free APIs are fine if you plan for them to fail. Two hosts for one dataset plus an independent source, each with a short timeout and strict validation, mean one bad provider costs a few seconds at most, never a wrong total.

On serverless, a cache that only lives in memory is barely a cache. A shared copy and a lease made one fetch a day enough for every instance.

An incomplete total should say so. Counting skipped items costs one integer and saves a wrong number from looking right.

More posts

LLM API cost control per user: how RPMBase keeps the AI bill boundedHow RPMBase keeps OpenAI costs bounded per user: server-side limits, a worst-case cost reservation, atomic budgets in MongoDB, shared pools for free accounts and a hard cap. Keyword cannibalization in practice: one query, three moves, zero impressionsA real case from RPMVault: moving "motorcycle maintenance app" between pages three times cost its impressions. The query ownership rules, title limits and lock test I use now.