Quick revision
The short answer
Constructors, compact ones included, run every time an event is deserialized, so validation that calls external services or reads live configuration makes loading data depend on those systems, and an outage elsewhere stops consumers and replays. The fix is to keep compact constructors to local invariants such as nulls, ranges and formats, move business validation that needs other systems into the service layer at the point where a command is accepted, and keep constructor rules compatible with every event already stored.
What to remember
In this answer, we saw that compact constructors run during every deserialization, so external calls and live configuration in them turn another system's outage into consumers that cannot read their own events. We kept only local invariants, nulls, ranges and formats, in the constructors, moved business validation to the service layer where commands are accepted, and made sure constructor rules stay compatible with historical events through versioning, upcasting and a replay test.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Constructors, compact ones included, run every time an event is deserialized, so validation that calls external services or reads live configuration makes loading data depend on those systems, and an outage elsewhere stops consumers and replays. The fix is to keep compact constructors to local invariants such as nulls, ranges and formats, move business validation that needs other systems into the service layer at the point where a command is accepted, and keep constructor rules compatible with every event already stored.
Why this question is asked
Event-driven systems built on records often start with good intentions: validate everything as early as possible, so put the checks in the constructor. That works until the first time a dependency is down and the consumers cannot even read their own messages. Interviewers ask this scenario to see whether we understand when constructors run, what an event really is, and how to separate the kinds of validation. In this answer, we will reproduce the failure, see why the constructor is the wrong place for these checks, split validation into two kinds, look at the history problem with stored events, and walk through the change step by step.
1A Quick Recap: Events Are Facts About the Past
An event records something that has already happened: an order was placed, a payment was captured. Once it is published to a topic or stored in an event store, it is a fact. Consumers read it, sometimes hours later, and event-sourced services replay every stored event to rebuild their state after a restart or a new deployment.
A record's compact canonical constructor, the constructor without a parameter list that runs before the fields are assigned, is executed for every instance. That includes every message Jackson turns into a record and every event Java serialization reads back. So whatever the constructor does, every consumer and every replay does too.
2Reproducing the Failure
Here is a simplified version of the design in the question. The compact constructor checks the customer against a customer service and the currency against a live configuration service.
6 messages in order. Play them, or step through them one at a time.
The event is fine; the constructor makes reading it depend on another system.
Nothing is wrong with the message. It was valid when it was produced and is still a correct record of what happened. But the consumer cannot read it, so it retries, the partition stops making progress, the consumer lag grows, and if the service restarts and replays its history, it cannot rebuild its state at all. One dependency's outage has spread to every consumer of the event.
3Two Kinds of Validation
The way out is to notice that the constructor is doing two different jobs. Local invariants are rules that can be checked from the values alone: the id is not null, the total is not negative, the currency is three uppercase letters. They are always true for a well-formed event, cost microseconds, and never change their answer. Business validation asks questions about the world: does this customer exist, is this currency currently allowed, does the customer have credit. Its answer depends on other systems and on time.
| Check | Depends on | Place |
|---|---|---|
orderId not null | the values only | compact constructor |
| total >= 0 | the values only | compact constructor |
| currency has 3 letters | the values only | compact constructor |
| customer exists and is active | customer service | service layer, on the command |
| currency allowed today | live configuration | service layer, on the command |
So business validation moves to the moment a decision is made, when a command such as PlaceOrder is accepted. At that point a failing dependency is reported to the caller, who can retry, and only events that passed the checks are ever published. Consumers then trust the event as a fact and run only the cheap local checks while reading it.
4Old Events Must Still Load
There is a second, quieter version of the same problem. Even local rules change over time. Suppose the team later decides that totals above 10,000 need a different event type and adds that limit to the constructor. Every OrderPlaced stored before the change with a total of 12,000 now fails to load, and the next replay breaks, even though no system is down.
So constructor rules on stored data must stay compatible with history. New rules are applied when new commands are accepted, not when old facts are read. If the shape of an event really changes, the usual answers are versioning (OrderPlacedV2 alongside the old type), an upcaster that converts old payloads when they are read, or a one-off migration of the stored events, done deliberately and tested against a copy of the real store. A useful habit is a test that deserializes a sample of real historical events from every version on every build.
5The Change, Step by Step
Putting it together, I would first remove every remote call and configuration lookup from event constructors and move them into the command handlers that produce the events. Second, I would review the remaining constructor checks against stored history, for example by replaying the production event store into a test environment, and relax any rule that old events violate. Third, for consumers that truly need to react to the current state of the world, such as refusing to ship to a customer who was blocked after ordering, I would add that check in the consumer's business logic, where a failure can be handled with a retry or a dead-letter topic, instead of breaking deserialization. Finally, I would add the history test to the pipeline so a future rule change cannot quietly break replays.
6Follow-up questions
Is it ever acceptable to read configuration in a constructor?
Static, build-time constants are fine, such as a maximum field length compiled into the code. Live configuration is not, because the same stored event could be valid under one setting and invalid under the next, and because loading data then depends on the configuration source being reachable.
What should a consumer do with an event that fails local validation?
Send it to a dead-letter topic with the error and continue with the next message, then alert. A malformed event means a producer bug, and stopping the whole partition behind it only adds an outage to the bug. Spring Kafka's DefaultErrorHandler with a DeadLetterPublishingRecoverer is a common way to do this.
Does this mean the event model should not validate at all?
No. Local invariants in the constructor are valuable: they make malformed events impossible to create and catch producer bugs early. The point is only to keep checks in the constructor that depend on nothing but the values.
7Conclusion
In this answer, we saw that compact constructors run during every deserialization, so external calls and live configuration in them turn another system's outage into consumers that cannot read their own events. We kept only local invariants, nulls, ranges and formats, in the constructors, moved business validation to the service layer where commands are accepted, and made sure constructor rules stay compatible with historical events through versioning, upcasting and a replay test.
Open with
The root cause is that constructors, including compact constructors, run on every deserialization. Jackson and Java serialization both create records through the canonical constructor, so if the constructor calls the customer service or reads live configuration, reading a message from Kafka or replaying the event store depends on those systems being up.
Then, if they want more
During the outage, perfectly valid events could not be deserialized, consumers retried, lag grew, and replays failed. My change would be to split validation into two kinds.
Local invariants, things I can check from the values alone, like non-null ids, a non-negative total or a currency code format, stay in the compact constructor, because they are cheap and never change their answer.
Business validation, like whether the customer exists or whether a currency is currently allowed, moves to the service layer, to the command handler that decides whether to create the event in the first place. A dependency failure there goes back to the caller, and only checked events get published. Consumers treat events as facts.
I would also make sure constructor rules stay compatible with historical data, because even a local rule change can make old stored events fail to load. So rule changes apply to new commands, real shape changes go through versioned event types or upcasters, and I add a build test that deserializes a sample of real historical events.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A records-heavy event model has validation in compact constructors that calls external...": https://scrivio-interview-library.netlify.app/java/records/0595d180cc/