Event-driven Architecture, State machines

The code of nanomsg to use state machines internally, passing asynchronous events around instead of using random callbacks between the components. To solve The Callback Hell Problems.

Sústrik, Martin - Event-driven Architecture, State machines

Combining Events and Functions:

The solution has two nice features:

  1. Most of the invocations in any system are root-to-leave-directed which means that most invocations in the codebase are simple function calls. The events are relatively rare and don't overwhelm the system.
  2. Given that posting an event is somehow more complex than invoking a function it makes the developer stop and think twice before adding a new leave-to-root-directed invocation. That in turn helps keeping interaction patterns inside the codebase as simple and tree-like as possible.

State Machines:

  • Generic Object
struct {
    int productId
    int price
    int processing_stage
}
  • state machine
struct {
    int productId
    int price
    int state
}

Nothing changes from the technical point of view. That being said, the state machines are an instrument for turning abstract processing mechanisms (objects) into narratives.

Event is deeply related to the narratives.

All you have to do is to draw all the possible states as boxes and connect them by arrows representing valid state transitions. Like TCP protocol state machine:

When using events with state machines can eliminate confusion and maintain consistency.