Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is Event-Driven Architecture and why is it important in microservices? Event-Driven Architecture क्या है और microservices में यह महत्वपूर्ण क्यों है?

Answer

Event-driven architecture is a paradigm where components communicate through events rather than direct calls. Services emit events when state changes, enabling loose coupling, scalability, and asynchronous processing across distributed systems.

ConceptTraditionalEvent-Driven
CouplingTightly coupledLoosely coupled
CommunicationSynchronous RPCAsynchronous events
ScalabilityLimitedHighly scalable
Real-timePolling neededImmediate updates
// Event class
public class OrderCreatedEvent extends ApplicationEvent {
    private String orderId;
    private double amount;
    
    public OrderCreatedEvent(Object source, String orderId, double amount) {
        super(source);
        this.orderId = orderId;
        this.amount = amount;
    }
}

// Publisher
@Service
public class OrderService {
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    
    public void createOrder(String orderId, double amount) {
        // Business logic
        eventPublisher.publishEvent(
            new OrderCreatedEvent(this, orderId, amount));
    }
}

// Listener
@Component
public class OrderListener {
    @EventListener
    public void onOrderCreated(OrderCreatedEvent event) {
        System.out.println('Processing order: ' + event.getOrderId());
        // Send email, update inventory, etc.
    }
}

// Benefits:
// 1. Loose coupling - no direct dependencies
// 2. Asynchronous - non-blocking execution
// 3. Scalability - easy to add new listeners
// 4. Event sourcing - audit trail of all events
// 5. Time travel - replay events to reconstruct state

Event-driven में components events के through communicate करते हैं, न कि direct calls से।

Was this answer clear?