Interview question
What is Event Sourcing and how do you implement it in Spring Boot? Event Sourcing क्या है और Spring Boot में इसे कैसे implement करते हैं?
Answer
Event Sourcing stores application state as immutable sequence of events. Instead of storing current state, store all state changes. Enables time-travel, audit trails, and event replay. Combine with Kafka and event store database for implementation.
// Event Sourcing Implementation
// Event definitions
public abstract class DomainEvent {
private String aggregateId;
private long timestamp;
private int version;
// getters
}
public class AccountCreatedEvent extends DomainEvent {
private String accountNumber;
private String accountHolder;
public AccountCreatedEvent(String aggregateId, String accountNumber) {
this.aggregateId = aggregateId;
this.accountNumber = accountNumber;
}
}
public class MoneyDepositedEvent extends DomainEvent {
private double amount;
public MoneyDepositedEvent(String aggregateId, double amount) {
this.aggregateId = aggregateId;
this.amount = amount;
}
}
// Event Store
@Repository
public class EventStore {
@Autowired
private EventRepository eventRepository;
public void saveEvent(DomainEvent event) {
eventRepository.save(event);
}
public List<DomainEvent> getEvents(String aggregateId) {
return eventRepository.findByAggregateId(aggregateId);
}
}
// Aggregate (Account) built from events
@Service
public class AccountService {
@Autowired
private EventStore eventStore;
@Autowired
private KafkaTemplate<String, DomainEvent> kafkaTemplate;
public void createAccount(String accountNumber) {
AccountCreatedEvent event = new AccountCreatedEvent(
UUID.randomUUID().toString(), accountNumber);
// Save to event store
eventStore.saveEvent(event);
// Publish event
kafkaTemplate.send('account-events', event);
}
public void deposit(String accountId, double amount) {
// Verify account exists by replaying events
List<DomainEvent> events = eventStore.getEvents(accountId);
Account account = rebuildAccount(accountId, events);
if (account == null) {
throw new AccountNotFoundException();
}
// Create deposit event
MoneyDepositedEvent event = new MoneyDepositedEvent(accountId, amount);
eventStore.saveEvent(event);
kafkaTemplate.send('account-events', event);
}
// Rebuild current state from events
private Account rebuildAccount(String accountId,
List<DomainEvent> events) {
Account account = null;
for (DomainEvent event : events) {
if (event instanceof AccountCreatedEvent) {
account = new Account();
account.setId(accountId);
} else if (event instanceof MoneyDepositedEvent) {
account.deposit(((MoneyDepositedEvent) event).getAmount());
}
}
return account;
}
}
// Read Model (Materialized View)
@Component
public class AccountProjection {
@KafkaListener(topics = 'account-events', groupId = 'account-projection')
public void handleEvent(DomainEvent event) {
if (event instanceof AccountCreatedEvent) {
// Update read model
} else if (event instanceof MoneyDepositedEvent) {
// Update read model
}
}
}
// Event Sourcing benefits:
// 1. Complete audit trail - all changes tracked
// 2. Time travel - reconstruct past states
// 3. Debugging - replay events to understand issue
// 4. Scalability - separate read/write models
// 5. Event-driven - react to domain events
// Challenges:
// 1. Event schema evolution
// 2. Event store maintenance
// 3. Eventual consistency
// 4. Debugging complexityEvent Sourcing:
Concept: State को events के sequence से store करना
Instead of: Current balance = 1000
Store: Deposit 500, Withdraw 200, Deposit 700
Benefit:
- Complete history
- Audit trail
- Time travel (replay)
- Event-driven
Implementation:
1. Define events (AccountCreated, MoneyDeposited)
2. Event store - store सभी events
3. Rebuild state - replay से current state बनाना
4. Publish events - Kafka से notify करना
Challenges:
- Event versioning
- Schema evolution
- Read model complexity
Stack:
Spring Boot + Kafka + Event Store DB
CQRS pattern with event sourcingWas this answer clear?