Video in progress

Staff 🌱 Spring

In a multi-service system, what common standards do you set for working with money amounts and time?

Money is BigDecimal or a long in minor units, always together with the currency code and with an explicit rounding rule; time is a UTC Instant (ISO-8601) when stored and transmitted, with the user's time zone applied only for display. These rules must be the same in the API, events and the database.

📖 Answer

These two topics look "small" in every single service, but inconsistency across services produces the hardest bugs to find: differences of 1 tiyin, transactions lost at the day boundary.

Money:

  • double/float are forbidden: 0.1 + 0.2 != 0.3.
  • Internal calculation uses BigDecimal (explicit scale, and RoundingMode always given explicitly) or a long in the smallest unit (tiyin). For transmission I prefer an integer in minor units, so it can't be read as a float in JSON.
  • An amount is never sent without its currency: {"amount": 1250000, "currency": "UZS"}. Currencies differ in the number of fraction digits (ISO 4217).
  • When and by what rule to round (banker's rounding, HALF_UP) is a business rule and gets documented. When splitting a commission, it must be clear where the remainder goes.
  • In the database, numeric(19,4) or bigint, never float.
public record Money(BigDecimal amount, Currency currency) {
    public Money {
        amount = amount.setScale(currency.getDefaultFractionDigits(), RoundingMode.HALF_EVEN);
    }
}

Time:

  • Store and transmit a UTC Instant in ISO-8601 format (2026-03-15T09:30:00Z). In the database, timestamptz.
  • LocalDateTime is only for concepts that truly have no zone ("every day at 09:00"; a birthday is a LocalDate).
  • The user's zone (Asia/Tashkent) is kept in the profile and used only for display and for computing day boundaries. "Today's sales": today in which zone? This must be written down explicitly in the reporting requirements.
  • Services get a Clock injected rather than calling Instant.now() directly, so tests can control time.
  • The server and JVM zone is UTC (-Duser.timezone=UTC, and hibernate.jdbc.time_zone=UTC in Hibernate), so there are no unexpected shifts that depend on the container's zone.

How to enforce it: a Money type and a Jackson module in a shared library; rules in the API style guide; an ArchUnit rule (amount fields of type double are forbidden); and for event schemas, a check at the registry level. If a standard stays in a document, it is forgotten in half a year: it has to live in code and CI.