Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 93

    Backend Design Patterns

    Patterns are a shared vocabulary, not a scavenger hunt.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. Keep persistence behind repositories; inject interfaces into services.
    2. Extract a PricingStrategy when discount rules branch.
    3. Publish domain events; upgrade to transactional outbox when consumers must not miss messages.
    4. Add Resilience4j on outbound Inventory calls; consider CQRS only if read models diverge hard.

    Informative example

    Strategy + transactional outbox sketch for Orders.

    java
    public interface PricingStrategy {
    Money price(List<OrderLine> lines);
    }
    @Service
    public 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;
    }
    @Transactional
    public 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.

    3 questions
    1IntermediateQuestionWhat problem does the outbox pattern solve?+

    Answer

    Atomic business write + reliable event intent without dual-write races between DB and broker.
    2AdvancedQuestionWhen is CQRS justified?+

    Answer

    When read and write models differ sharply in shape or scale — not by default for every API.
    3BeginnerQuestionWhy use Strategy for pricing?+

    Answer

    Swap algorithms without rewriting the place-order flow; keeps Orders open for extension.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.