Saga Pattern Interview Prep - Mind Map

๐ŸŽฏ Saga Pattern Interview Prep

Click on any node to expand/collapse details

    <div class="legend">
        <div class="legend-item">
            <div class="color-box" style="background: #e0e7ff;"></div>
            <span>Concept</span>
        </div>
        <div class="legend-item">
            <div class="color-box" style="background: #fecaca;"></div>
            <span>Important</span>
        </div>
        <div class="legend-item">
            <div class="color-box" style="background: #dbeafe;"></div>
            <span>Code/Implementation</span>
        </div>
    </div>

    <div class="mindmap">
        <div class="central-node">
            ๐Ÿ” Food Delivery Saga Pattern
        </div>

        <div class="branch">
            <!-- Core Concepts -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">๐Ÿง </span>
                    Core Concepts
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>What is Saga?</strong><br>
                        A pattern for managing distributed transactions across microservices using a sequence of local transactions
                    </div>
                    <div class="sub-item">
                        <strong>Why Saga?</strong><br>
                        โ€ข No 2-phase commit (distributed locks)<br>
                        โ€ข Better availability & scalability<br>
                        โ€ข Services stay independent
                    </div>
                    <div class="sub-item">
                        <strong>Two Types:</strong><br>
                        <span class="tag important">Orchestration</span> - Central coordinator (used here)<br>
                        <span class="tag">Choreography</span> - Decentralized, event-driven
                    </div>
                    <div class="sub-item">
                        <strong>Key Feature:</strong><br>
                        Compensating transactions = Rollback in distributed systems
                    </div>
                </div>
            </div>

            <!-- Architecture -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">๐Ÿ—๏ธ</span>
                    Architecture
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>5 Microservices:</strong><br>
                        <span class="tag code">Order (8081)</span> - Orchestrator<br>
                        <span class="tag code">Payment (8082)</span> - Process payments<br>
                        <span class="tag code">Restaurant (8083)</span> - Inventory<br>
                        <span class="tag code">Delivery (8084)</span> - Rider assignment<br>
                        <span class="tag code">Notification (8085)</span> - Alerts
                    </div>
                    <div class="sub-item">
                        <strong>Communication:</strong><br>
                        Apache Kafka for async messaging<br>
                        Topics: requests, responses, compensations
                    </div>
                    <div class="sub-item">
                        <strong>Tech Stack:</strong><br>
                        Spring Boot 3.2, Java 17, Maven, H2 DB, Spring Cloud Stream
                    </div>
                </div>
            </div>

            <!-- Happy Path Flow -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">โœ…</span>
                    Happy Path Flow
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>Step 1:</strong> Client creates order โ†’ Order Service (PENDING)
                    </div>
                    <div class="sub-item">
                        <strong>Step 2:</strong> Payment request โ†’ Payment Service โ†’ SUCCESS
                    </div>
                    <div class="sub-item">
                        <strong>Step 3:</strong> Restaurant request โ†’ Check inventory โ†’ CONFIRMED
                    </div>
                    <div class="sub-item">
                        <strong>Step 4:</strong> Delivery request โ†’ Assign rider โ†’ ASSIGNED
                    </div>
                    <div class="sub-item">
                        <strong>Step 5:</strong> Notification โ†’ Send confirmation โ†’ COMPLETED โœ…
                    </div>
                    <div class="sub-item">
                        <strong>Order Status:</strong><br>
                        PENDING โ†’ PAYMENT_PROCESSING โ†’ PAYMENT_COMPLETED โ†’ RESTAURANT_CONFIRMED โ†’ DELIVERY_ASSIGNED โ†’ COMPLETED
                    </div>
                </div>
            </div>

            <!-- Compensation Flow -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">โš ๏ธ</span>
                    Compensation Flow
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>Scenario 1: Payment Fails</strong><br>
                        Order โ†’ PENDING โ†’ PAYMENT_FAILED โ†’ CANCELLED<br>
                        No compensation needed (first step)
                    </div>
                    <div class="sub-item">
                        <strong>Scenario 2: Restaurant Fails</strong><br>
                        Payment โœ… โ†’ Restaurant โŒ<br>
                        <span class="tag important">Compensate: Refund payment</span><br>
                        Order โ†’ COMPENSATING โ†’ CANCELLED
                    </div>
                    <div class="sub-item">
                        <strong>Scenario 3: Delivery Fails</strong><br>
                        Payment โœ… โ†’ Restaurant โœ… โ†’ Delivery โŒ<br>
                        <span class="tag important">Compensate: Release inventory + Refund</span><br>
                        Order โ†’ COMPENSATING โ†’ CANCELLED
                    </div>
                    <div class="sub-item">
                        <strong>Key Point:</strong> Each successful step must have a compensating transaction
                    </div>
                </div>
            </div>

            <!-- Code Implementation -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">๐Ÿ’ป</span>
                    Code Implementation
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>Orchestrator Pattern:</strong><br>
                        OrderSagaOrchestrator coordinates everything<br>
                        Uses StreamBridge to send events
                    </div>
                    <div class="sub-item">
                        <strong>Event Listeners:</strong><br>
                        <span class="tag code">@Bean Consumer&lt;OrderEvent&gt;</span><br>
                        paymentResponses(), restaurantResponses(), deliveryResponses()
                    </div>
                    <div class="sub-item">
                        <strong>Kafka Topics:</strong><br>
                        payment-requests, payment-responses<br>
                        payment-compensations (for refunds)<br>
                        restaurant-requests, restaurant-responses<br>
                        restaurant-compensations (release inventory)
                    </div>
                    <div class="sub-item">
                        <strong>State Management:</strong><br>
                        Order entity with @Enumerated OrderStatus<br>
                        Stored in H2 database
                    </div>
                </div>
            </div>

            <!-- Interview Questions -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">โ“</span>
                    Interview Questions
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>Q: Why Saga over 2-phase commit?</strong><br>
                        A: Avoids distributed locks, better availability, services don't block each other
                    </div>
                    <div class="sub-item">
                        <strong>Q: Orchestration vs Choreography?</strong><br>
                        A: Orchestration = central coordinator (easier to debug), Choreography = each service reacts to events (more decoupled)
                    </div>
                    <div class="sub-item">
                        <strong>Q: How do you handle failures?</strong><br>
                        A: Compensating transactions - undo previous steps in reverse order
                    </div>
                    <div class="sub-item">
                        <strong>Q: What if compensation fails?</strong><br>
                        A: Retry mechanism, dead letter queue, manual intervention, idempotency ensures safe retries
                    </div>
                    <div class="sub-item">
                        <strong>Q: How to ensure idempotency?</strong><br>
                        A: Check if orderId already processed before executing, store transaction IDs
                    </div>
                    <div class="sub-item">
                        <strong>Q: Eventual consistency?</strong><br>
                        A: Yes - saga provides eventual consistency, not immediate consistency like ACID
                    </div>
                </div>
            </div>

            <!-- Design Patterns Used -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">๐ŸŽจ</span>
                    Design Patterns
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>1. Saga Pattern:</strong> Distributed transaction management
                    </div>
                    <div class="sub-item">
                        <strong>2. Event Sourcing:</strong> Events drive state changes
                    </div>
                    <div class="sub-item">
                        <strong>3. CQRS:</strong> Separate read/write (Order Service tracks state)
                    </div>
                    <div class="sub-item">
                        <strong>4. Idempotency:</strong> Safe to process same event multiple times
                    </div>
                    <div class="sub-item">
                        <strong>5. Compensating Transaction:</strong> Undo/rollback pattern
                    </div>
                    <div class="sub-item">
                        <strong>6. Observer Pattern:</strong> Services listen to events
                    </div>
                </div>
            </div>

            <!-- Key Interview Points -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">๐ŸŽฏ</span>
                    Key Interview Points
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <span class="tag important">Say This:</span><br>
                        "I implemented orchestration-based saga where Order Service coordinates 4 services via Kafka events"
                    </div>
                    <div class="sub-item">
                        <span class="tag important">Highlight:</span><br>
                        Each step has a compensating transaction for rollback (refund payment, release inventory)
                    </div>
                    <div class="sub-item">
                        <span class="tag important">Mention:</span><br>
                        Built with Spring Cloud Stream, ensures idempotency, handles duplicate messages
                    </div>
                    <div class="sub-item">
                        <span class="tag important">Trade-offs:</span><br>
                        Saga = Eventual consistency (not immediate)<br>
                        Simpler than 2PC but more complex error handling
                    </div>
                </div>
            </div>

            <!-- Testing -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">๐Ÿงช</span>
                    Testing Strategy
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>Happy Path Test:</strong><br>
                        customerId: "customer-123" โ†’ All steps succeed
                    </div>
                    <div class="sub-item">
                        <strong>Payment Failure:</strong><br>
                        customerId: "FAIL_PAYMENT" โ†’ Order cancelled
                    </div>
                    <div class="sub-item">
                        <strong>Restaurant Failure:</strong><br>
                        restaurantId: "FAIL_RESTAURANT" โ†’ Compensate payment
                    </div>
                    <div class="sub-item">
                        <strong>Delivery Failure:</strong><br>
                        deliveryAddress: "FAIL_DELIVERY" โ†’ Compensate all
                    </div>
                    <div class="sub-item">
                        <strong>Integration Tests:</strong><br>
                        Start Kafka, test end-to-end flow, verify compensations
                    </div>
                </div>
            </div>

            <!-- GitHub Actions CI/CD -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">๐Ÿš€</span>
                    CI/CD Pipeline
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>Build Job:</strong> Matrix strategy builds all 5 services in parallel
                    </div>
                    <div class="sub-item">
                        <strong>Test Job:</strong> Runs unit tests, uploads artifacts
                    </div>
                    <div class="sub-item">
                        <strong>Integration Job:</strong> Starts Kafka with Docker Compose, tests connectivity
                    </div>
                    <div class="sub-item">
                        <strong>Docker Job:</strong> Builds images for each service (on main branch)
                    </div>
                    <div class="sub-item">
                        <strong>Triggers:</strong> Push to main/develop, Pull requests
                    </div>
                </div>
            </div>

            <!-- Production Considerations -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">โš™๏ธ</span>
                    Production Considerations
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>Monitoring:</strong> Track saga execution, failure rates, compensation success
                    </div>
                    <div class="sub-item">
                        <strong>Timeouts:</strong> Add timeout handling for each step (if payment takes >30s, compensate)
                    </div>
                    <div class="sub-item">
                        <strong>Dead Letter Queue:</strong> For failed compensations that need manual intervention
                    </div>
                    <div class="sub-item">
                        <strong>Observability:</strong> Distributed tracing (Spring Cloud Sleuth), Prometheus metrics
                    </div>
                    <div class="sub-item">
                        <strong>Database:</strong> Replace H2 with PostgreSQL/MySQL for persistence
                    </div>
                    <div class="sub-item">
                        <strong>Message Broker:</strong> Kafka partitioning for scalability, consumer groups
                    </div>
                </div>
            </div>

            <!-- Real-world Use Cases -->
            <div class="node" onclick="toggleNode(this)">
                <div class="node-title">
                    <span class="node-icon">๐ŸŒ</span>
                    Real-world Examples
                    <span class="expand-indicator">โ–ถ</span>
                </div>
                <div class="sub-items">
                    <div class="sub-item">
                        <strong>E-commerce:</strong> Order โ†’ Payment โ†’ Inventory โ†’ Shipping
                    </div>
                    <div class="sub-item">
                        <strong>Banking:</strong> Transfer money between accounts across services
                    </div>
                    <div class="sub-item">
                        <strong>Travel Booking:</strong> Flight + Hotel + Car rental (cancel all if one fails)
                    </div>
                    <div class="sub-item">
                        <strong>Food Delivery:</strong> Uber Eats, DoorDash use saga-like patterns
                    </div>
                    <div class="sub-item">
                        <strong>Companies Using:</strong> Netflix, Uber, Amazon, Airbnb
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<button class="print-button" onclick="expandAll()">๐Ÿ“‹ Expand All for Review</button>

<script>
    function toggleNode(node) {
        node.classList.toggle('expanded');
    }

    function expandAll() {
        const nodes = document.querySelectorAll('.node');
        nodes.forEach(node => {
            node.classList.add('expanded');
        });
    }

    // Expand first few nodes by default
    window.addEventListener('DOMContentLoaded', () => {
        const nodes = document.querySelectorAll('.node');
        if (nodes.length > 0) {
            nodes[0].classList.add('expanded'); // Core Concepts
            nodes[5].classList.add('expanded'); // Interview Questions
        }
    });
</script>