Backend Design Patterns
Patterns are a shared vocabulary, not a scavenger hunt.
Introduction
Patterns are a shared vocabulary, not a scavenger hunt. In TechLearningPRO Orders on Boot 3 you will meet Repository, Strategy, Factory/Builder, Specification, Observer/events, Outbox, Saga, resilience patterns, and occasionally CQRS — each when the problem shapes fit.
Spring already embeds many of these: JpaRepository, ApplicationEventPublisher, and Resilience4j circuit breakers. The skill is knowing when to reach for Outbox versus a best-effort event publish, or when CQRS is premature.
Prefer the simplest pattern that protects correctness under failure. Distributed sagas and CQRS earn their complexity only when reads/writes or multi-service workflows demand them.
This lesson catalogues the weekly patterns before enterprise API product design.
Purpose of this lesson
Apply the right backend patterns in Orders — repository through outbox/saga/CQRS — without cargo-culting.
Understanding the topic
Patterns you will see weekly in real Spring Boot codebases.
- Repository: abstract persistence behind interfaces.
- Strategy: interchangeable algorithms (
PricingStrategy). - Factory / Builder: complex object construction.
- Specification: composable query criteria.
- Observer / Event: Spring
ApplicationEventPublisher. - Outbox: reliable events alongside DB writes.
- Saga: multi-service workflows with compensations.
- Circuit breaker / retry / bulkhead: resilience.
- CQRS: separate read and write models at scale.
Step-by-step explanation
- Keep persistence behind repositories; inject interfaces into services.
- Extract a
PricingStrategywhen discount rules branch. - Publish domain events; upgrade to transactional outbox when consumers must not miss messages.
- Add Resilience4j on outbound Inventory calls; consider CQRS only if read models diverge hard.
Informative example
Strategy + transactional outbox sketch for Orders.
public interface PricingStrategy {Money price(List<OrderLine> lines);}@Servicepublic class OrderService {private final PricingStrategy pricing;private final OrderRepository orders;private final OutboxWriter outbox;public OrderService(PricingStrategy pricing, OrderRepository orders, OutboxWriter outbox) {this.pricing = pricing;this.orders = orders;this.outbox = outbox;}@Transactionalpublic OrderView place(CreateOrderRequest cmd) {Money total = pricing.price(cmd.lines());OrderEntity saved = orders.save(OrderEntity.from(cmd, total));outbox.enqueue("OrderPlaced", saved.getId());return OrderView.from(saved);}}
Real-world use
Orders once published Kafka events after commit in a separate step. A crash between commit and publish dropped OrderPlaced, and inventory never reserved. Introducing an outbox table written in the same transaction made delivery reliable without distributed transactions.
Security implications
Patterns can hide or help security controls.
- Strategy implementations must not bypass authorization checks.
- Outbox payloads should omit secrets and PII where possible.
- Retry/circuit breakers must not amplify credential stuffing against dependencies.
Best practices
- Name patterns in code reviews so the team shares vocabulary.
- Prefer Outbox + consumers over dual writes.
- Use Saga compensations deliberately; document each step.
Common mistakes
- Forcing every class into a GoF pattern diagram.
- CQRS for a simple admin CRUD screen.
- Fire-and-forget events without outbox when delivery must be reliable.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionWhat problem does the outbox pattern solve?+
Answer
2AdvancedQuestionWhen is CQRS justified?+
Answer
3BeginnerQuestionWhy use Strategy for pricing?+
Answer
Hands-on exercise
Lab. Implement two PricingStrategy beans selected by profile, and an outbox row written in the same @Transactional place-order method. Prove a consumer can relay outbox events after restart.
Summary
Backend patterns keep Orders correct under growth and failure. Next: treat the HTTP surface as an enterprise API product.