Video in progress

How do you manage "hidden" dependencies like system time, ID generation and random numbers in a design, and why are they an architecture issue?

I inject them as explicit dependencies: Clock, IdGenerator, RandomGenerator. Then the code can be tested deterministically, and the time zone and ID format are controlled in one place and consistent across services.

📖 Answer

LocalDate.now(), UUID.randomUUID() and new Random() look small, but they are hidden coupling to global state. The consequences:

  • to test "end of month" or "overdue" rules, tests depend on the clock and sometimes fail (flaky);
  • the server is on UTC and the business is on Tashkent time: LocalDate.now() returns the wrong day around midnight;
  • each module creates IDs its own way: UUIDv4 in one, a DB sequence in another, timestamp+random in a third, so log correlation and index efficiency differ.

Design decisions:

@Configuration
class TimeConfig {
    @Bean Clock clock() { return Clock.system(ZoneId.of("Asia/Tashkent")); }
}

@Service
class LoanService {
    private final Clock clock;
    private final IdGenerator ids;
    LoanService(Clock clock, IdGenerator ids) { this.clock = clock; this.ids = ids; }

    Loan open(Application a) {
        return Loan.open(ids.next(), a, LocalDate.now(clock));
    }
}

// in a test
Clock fixed = Clock.fixed(Instant.parse("2026-01-31T19:30:00Z"), ZoneId.of("Asia/Tashkent"));

What to agree on across the organization:

  • Time: store and transmit over the network as Instant/UTC; the business day uses an explicit zone, calculated in one place.
  • IDs: one standard for the organization. Time-ordered IDs (UUIDv7, ULID) are better for a B-tree index than UUIDv4.
  • Randomness: for security, only SecureRandom; in simulations and tests, a generator with a seed so it's reproducible.

These aren't small details: without agreement on them, after a few years comparing data between services and reconciling reports becomes an expensive project.