QA
Senior LevelPython

How do you architect decoupled Event-Driven Domain-Driven Design (DDD) systems in Python using internal Pub/Sub Event Buses?

❓ Interview Prompt: Explain Event-Driven Architecture, Domain-Driven Design (DDD), Domain Events, Event Bus, subscriber handlers, and decoupling side-effects.

In enterprise Domain-Driven Design (DDD), core domain operations emit Domain Events (e.g. 'OrderCreatedEvent'):

Event Bus Decoupling:

An internal 'EventBus' dispatches domain events to registered subscriber handlers ('SendEmailReceipt', 'UpdateInventory') asynchronously, decoupling core business transactions from secondary side-effects!

Code Implementation Example

python
from dataclasses import dataclass
from typing import Callable

@dataclass(frozen=True)
class OrderCreated:
    order_id: int
    customer_email: str

class EventBus:
    def __init__(self):
        self._subscribers: dict[type, list[Callable]] = {}
        
    def subscribe(self, event_type: type, handler: Callable):
        self._subscribers.setdefault(event_type, []).append(handler)
        
    def publish(self, event):
        for handler in self._subscribers.get(type(event), []):
            handler(event)

bus = EventBus()
bus.subscribe(OrderCreated, lambda e: print(f'Sending email to {e.customer_email}'))
bus.publish(OrderCreated(1052, 'alice@example.com'))
💡Decoupling domain side-effects using an Event-Driven Pub/Sub EventBus.
🎯 Interview Key Takeaway

Use an internal EventBus to decouple core domain transactions from secondary side-effects in DDD architectures.

#EventBus#DDD#Event-Driven#Architecture#Senior
Back to all Python Questions→

More Python Interview Questions

Beginner LevelPython

What is the Python Global Interpreter Lock (GIL) and how does it affect multi-threading?

Beginner LevelPython

What is the difference between a List, Tuple, Set, and Dictionary in Python?

Beginner LevelPython

Why are mutable default arguments (like def func(items=[])) dangerous in Python and how do you fix them?