Product Decisions This Supports
-
Feature Development:
- Implement audit trails for critical business operations (e.g., financial transactions, user actions) with immutable event logs.
- Build temporal queries (e.g., "Show me all state changes for this order in the last 30 days") without complex database joins.
- Enable real-time projections (e.g., dashboards, notifications) by subscribing to events via reactors or projectors.
- Support CQRS (Command Query Responsibility Segregation) by decoupling read and write models, improving scalability for high-traffic APIs.
-
Roadmap Alignment:
- Phase 1: Pilot event sourcing for a high-value but low-risk feature (e.g., user account history) to validate architectural benefits.
- Phase 2: Expand to core domains (e.g., orders, payments) where temporal consistency and replayability are critical.
- Phase 3: Integrate with event-driven microservices or serverless functions to extend event sourcing beyond Laravel.
-
Build vs. Buy:
- Buy: Avoid reinventing event sourcing infrastructure (e.g., event storage, projection management). This package abstracts complexity while providing Laravel-native tooling.
- Custom Build: Only consider if requirements exceed the package’s scope (e.g., multi-region event replication, custom event serialization).
- Hybrid: Use the package for core event sourcing but extend with custom logic for niche use cases (e.g., blockchain-based event validation).
-
Use Cases:
- Compliance/Auditing: Regulated industries (finance, healthcare) needing unalterable logs for SOX, GDPR, or HIPAA.
- Complex Workflows: Multi-step processes (e.g., e-commerce orders, loan approvals) where state transitions must be traceable.
- Real-Time Systems: Chat apps, live collaborations, or IoT where event-driven updates reduce latency.
- Data Migration: Safely replay events to backfill projections during schema changes or system upgrades.
When to Consider This Package
Adopt This Package If:
- Your Laravel app requires temporal data integrity (e.g., "I need to prove this state was valid at time T").
- You’re building a domain where history matters (e.g., financial ledgers, medical records, legal documents).
- Your team lacks event sourcing expertise but wants a Laravel-friendly starting point (avoids low-level complexity).
- You need audit trails without manual logging (e.g., tracking every change to a resource via events).
- Your read models (e.g., dashboards, reports) can be rebuilt from events (projections) instead of syncing with a primary database.
- You’re using Laravel Queues or Horizon and want to extend event processing with reactors (e.g., triggering notifications on events).
Look Elsewhere If:
- Your use case is simple CRUD with no need for event logs or temporal queries.
- You require event sourcing at scale (e.g., millions of events/sec)—this package may need optimization or a dedicated solution like EventStoreDB or Kafka.
- Your team lacks PHP/Laravel expertise—event sourcing adds architectural complexity that may slow development.
- You need multi-language support (this is PHP/Laravel-specific; consider Axoni or Eventuous for polyglot persistence).
- Your events require custom serialization (e.g., Protocol Buffers) or off-chain validation (e.g., smart contracts).
How to Pitch It (Stakeholders)
For Executives (Business/Strategy):
*"This package lets us adopt event sourcing—a pattern where every state change in our system is recorded as an immutable event. Think of it like a time machine for our data:
- Audit-ready: Every action is logged, so we can prove compliance with regulations like GDPR or SOX without manual tracking.
- Future-proof: We can rebuild our entire database from events if needed (e.g., after a migration or disaster).
- Real-time insights: By projecting events into dashboards or notifications, we can surface critical updates instantly (e.g., fraud alerts, order status changes).
- Low risk: Spatie’s package handles the heavy lifting in Laravel, so we avoid years of custom infrastructure work.
Why now? If we’re building [Feature X] or scaling [System Y], event sourcing could reduce technical debt and unlock features like temporal queries or decision replay. Let’s pilot it for [Low-Risk Use Case] to validate the ROI."*
For Engineering (Technical):
*"This package provides a batteries-included way to implement event sourcing in Laravel, covering:
- Aggregates: Encapsulate business logic and emit events (e.g.,
OrderCreated, PaymentFailed).
- Projectors: Rebuild read models (e.g.,
orders table) from events for queries.
- Reactors: Side-effect handlers (e.g., send email on
OrderShipped).
- Event Storage: Persist events in a PostgreSQL/MySQL table with Laravel’s query builder.
Key Benefits:
- No event store vendor lock-in: Uses Laravel’s DB, but can be swapped later.
- Seamless with Laravel: Works with Eloquent, Queues, and existing tooling.
- Performance: Optimized for Laravel’s ecosystem (e.g., event batching, projection caching).
Trade-offs:
- Learning curve: Event sourcing changes how we think about state (e.g., no direct DB writes).
- Projection maintenance: Read models must stay in sync with events.
Proposal: Start with a single aggregate (e.g., UserAccount) to test the pattern. If successful, expand to [High-Impact Domain]."*
For Developers (Implementation):
*"Here’s how you’d use it in code:
// 1. Define an Aggregate (e.g., Order)
class Order extends Aggregate
{
public function createOrder(OrderCreated $event)
{
$this->recordThat($event);
}
public function markAsPaid(OrderPaid $event)
{
$this->recordThat($event);
}
}
// 2. Project events into a read model
Event::projector()
->for(OrderCreated::class)
->create(function (OrderCreated $event) {
Order::query()->create($event->toArray());
});
// 3. React to events (e.g., send notifications)
Event::reactor()
->for(OrderShipped::class)
->perform(fn (OrderShipped $event) => Notification::send(...));
Docs: Spatie’s Guide covers aggregates, projections, and testing.
Next Steps:
- Set up the package (
composer require spatie/laravel-event-sourcing).
- Design your first aggregate (start simple!).
- Build a projector for your read model.
- Test event replay to ensure data integrity."*