Scrivio Interview library

Domain-driven design

21 questions in 6 subtopics.

Entities and value objects

ConceptFoundations7 min read
What is the difference between an entity and a value object in domain-driven design? Give Java examples and explain how equals and hashCode differ for each.
CodingIntermediate7 min read
Design a Money value object in Java for a payments service. Which representation, operations and checks do you include, and which mistakes do you avoid?
DebuggingIntermediate7 min read
A JPA entity implements equals and hashCode using its database-generated id. A new entity is added to a HashSet before being persisted; after persist and flush, set.contains(entity) returns false. Explain and fix.
LeadershipPrincipal6 min read
Your company has tried twice to build a single canonical enterprise data model, with one Customer and one Product shared by all systems. Both efforts stalled. Why do these efforts usually fail, and what should you do instead?

Aggregates

ConceptFoundations6 min read
What is an aggregate and an aggregate root in DDD? Why should one aggregate refer to another only by id?
ScenarioIntermediate7 min read
Two users add items to the same shopping cart at the same moment. The rule is that a cart holds at most 50 items. Using a Cart aggregate with JPA, how do you guarantee the rule under concurrency?
DesignIntermediate7 min read
An Order aggregate includes its line items, payments, shipments and the full history of status changes. Some orders have 10,000 line items, and loading an order for any change is slow and conflicts often. How would you redesign the boundaries?

Repositories

ConceptFoundations6 min read
How does a repository in DDD differ from a DAO, and how does Spring Data fit into this?
DesignIntermediate7 min read
Should a Spring service annotate its domain classes directly with JPA annotations, or keep a separate persistence model and map between them? Discuss the trade-offs and when you would choose each.
ScenarioAdvanced7 min read
Several services read and write the same database tables directly, so no service clearly owns the data. Plan the move to one owner per aggregate, accessed through that owner's repository and API.

Domain events

ConceptFoundations6 min read
What is a domain event? How should it be named, and how does Spring Data help an aggregate publish events?
DesignIntermediate7 min read
Order service must publish OrderPlaced to Kafka whenever an order is saved, with no lost or phantom events. Design this.
DesignAdvanced6 min read
Teams publish fine-grained domain events from inside their services, and other teams have started consuming them directly. Now any internal change breaks consumers. How do you separate internal domain events from integration events?
LeadershipPrincipal6 min read
A chief architect proposes event sourcing as the company-wide architecture for all new services. Evaluate this as a multi-year bet.

Bounded contexts

ConceptIntermediate7 min read
What is a bounded context? Explain how the word Customer can mean different things in Sales, Billing and Support, and how bounded contexts map to services or modules.
LeadershipAdvanced7 min read
You are splitting a large monolith across eight teams. How do you find bounded contexts and align them with team ownership?
LeadershipPrincipal7 min read
Over a ten-year horizon, how do you govern bounded context boundaries as the business changes through new products, acquisitions and reorganisations?

Anti-corruption layer

ConceptFoundations6 min read
What is an anti-corruption layer, and why should domain code not use an external system's API classes directly?
DesignIntermediate7 min read
Your Spring service must integrate with a legacy ERP that uses cryptic codes, overloaded fields and inconsistent error handling. Design an anti-corruption layer for it.
ScenarioAdvanced7 min read
During a strangler fig migration from a legacy system to new services, where do you place anti-corruption layers, and how do you avoid them becoming permanent?
LeadershipPrincipal7 min read
Your company acquires another business whose systems use different models for customers, orders and products. Over several years, how do you integrate the acquired platform?