Day 46: Domain Events: Dispatching PostCreated from the storage layer.
Day 46: Domain Events: Dispatching PostCreated from the storage layer.
Welcome back, architects and engineers! Today, we're diving into a concept that separates robust, high-scale systems from those struggling with spaghetti code and data inconsistencies: Domain Events. Specifically, we'll focus on the critical, often overlooked detail of where and when these events are dispatched, especially in the context of our high-scale PHP Content Management System.
Agenda: The Silent Guardians of Consistency
Why Domain Events? Beyond just "decoupling."
The "Bootload Tax" and Event-Driven Architecture: How events alleviate pressure on our persistent runtime.
The Critical Decision: Dispatching from the Storage Layer. Why this isn't just a good idea, but a mandatory practice for transactional integrity in high-scale systems.
Core Concepts: Event, Event Dispatcher, Listener, and the Unit of Work.
Fitting it In: How
PostCreatedevents integrate into our CMS.Hands-on Build-Along: Implementing a basic event system.
Why Domain Events? Beyond Just "Decoupling."
You've likely heard that Domain Events "decouple" components. While true, that's like saying a car "moves." It doesn't capture the full picture. For ultra-high-scale systems like ours, handling hundreds of millions of requests, Domain Events are fundamental to:
Asynchronous Processing: Imagine creating a post. Besides saving it, we might need to:
Notify 100,000 followers.
Update a search index.
Generate a new entry in a recommendation engine.
Clear related caches across a CDN.
Send data to an analytics platform.
If all these operations happen synchronously within the single
PostServicecall, the user waits. At scale, this latency is unacceptable, and the chances of one of these external calls failing (and rolling back the entire transaction) increase dramatically. Events allow us to offload these tasks to background workers, making the primary request fast and resilient.System Reactivity: Events turn our system into a reactive organism. Instead of components explicitly calling each other, they simply react to "things that happened." This makes our system far more flexible to evolving business requirements. Need a new feature that reacts to
PostCreated? Just add a new listener, no changes to thePostServiceorPostRepositoryneeded.Auditing and Replayability: Events are a historical record of "what happened" in our domain. This is invaluable for auditing, debugging, and even rebuilding system state (event sourcing) if needed.
The "Bootload Tax" and Event-Driven Architecture
In our high-performance PHP runtime, we're striving to minimize the "bootload tax" β the overhead of initializing the application for each request. Event-driven architecture plays a crucial role here. By decoupling concerns, we keep our core request path lean. Complex, long-running operations (like sending mass notifications) are pushed off the critical path, allowing our persistent PHP processes to quickly handle the next incoming request without being bogged down.
The Critical Decision: Dispatching from the Storage Layer
Now, for the truly crucial insight: When an entity like a Post is created, the PostCreated event <em style="color: #475569; font-style: italic;">must</em> be dispatched from the storage layer, <em style="color: #475569; font-style: italic;">after</em> the data has been successfully committed to the database.
Why?
Keep going with the full lesson
The rest of this lesson β plus its implementation guide, runnable scripts and source code β is included with the course or an All Access plan. Create a free account to start the free lessons in every course.
Already a member? Sign in