Asynchronous BE Architecture Options
Architectures
Sync vs Async Comparison
| Feature | Sync | Async - On the fly | Async - Operation | Async - Queued | Async - New process |
|---|---|---|---|---|---|
| Pros | Simpler implementation, Immediate result | Simple to implement, Execution can be postponed, Can scale horizontally, Can be completely isolated | All request data in the message, Can keep track of process & result, Can be undone | Simpler implementation | Execution can be postponed, Does not block request |
| Cons | Blocking request, Can't be deferred | Can't return result directly (events), Can't be undone | Complex to implement, Cumbersome | Can't be scaled, Execution cannot be postponed | Complex to implement, Needs external logging of result |
MG Uses
Applications of async processing in MG. Gantt and Notifications are already in place.
| Service Type | Processing | Dependencies | Trigger |
|---|---|---|---|
| Cloning Grids | High | DB & Events | Operation |
| Deleting entities | Low | DB & Events | Operation |
| Restoring entities | Low | DB & Events | Operation |
| EOL DB delete | High | DB & Events | Cron (fly) |
| Exporting data | High | DB & Mail? | Operation |
| Importing data | High | DB & Events | Operation |
| Gantt scheduler | High | DB & Events | On the Fly |
| Notifications | Low | DB & Events | On the Fly |
PHP Libraries for Async Implementation
PHP libraries to implement same server on the fly async:
Spatie/Async (^7.4|^8.0)
Similar to ReactPHP, very elegant interface.
ReactPHP
Low-level library to write asynchronous code using an event-driven approach. It provides components to handle common tasks like HTTP clients/servers and file system operations.
Swoole (>=7.4)
Is an extension to PHP that enables async, coroutines, and fibers. It's particularly well-suited for building microservices or task schedulers.
Amp (>=8.1)
Is a library that uses generators to make asynchronous code feel like synchronous code, simplifying the way developers write and think about async PHP.
Fibers (>=8.0)
With the introduction of fibers in PHP 8.1, async code can be written in a more straightforward manner without diving deep into callbacks or promises. Fibers can interrupt their execution and return the control to the event loop.