Increasingly I am being asked about the difference between using rules to implement decisions and using rules to detect situations. Sometimes people confuse or conflate the two but I believe the separation is valuable and you should try to preserve it in your architecture if possible. Here are some of the differences.
Decisions:
- Predominantly stateless functions: given an input, they product an output
- Rules are predominantly used to validate and transform inputs to outputs. Often Sequential or FastPath execution algorithm is used.
- Usually called synchronously from a transactional system, or process. May be idempotent.
- Externalizes some critical business logic from existing applications
- Relatively easy to test. You can create test cases in spreadsheets and run them through the system to check your rules are functioning correctly.
- Relatively easy to reuse the same decision in many applications and contexts: batch, transactional, process, mobile etc.
- High-availability and load-balancing is relatively easy to implement
Situations:
- Predominantly stateful functions that detect interesting situations in an event stream. Rarely idempotent.
- Rules are relatively more complex as they correlate and aggregate timestamped event information.
- Usually run alongside existing systems, listening to event streams and asynchronously emitting events when interesting situations are detected.
- Challenging to test as you have to set up the correct context and inject events at the right frequency.
- The passage of time (and missing events) can be a crucial component in the definition of a situation
- In some industries the places/locations for events is an important component in the definition of situations. E.g. physical security applications.
- Detecting a situation may require making one or more decisions
- Deciding on the optimal response once a situation is detected may require making subsequent decisions
- Relatively hard to reuse, in fact it is not usually clear what reuse means in this context.
- High availability and load-balancing is challenging
In many ways this is the old Process vs. Decision debate from 10 years ago, just resurfaced in a new form. Rules are a powerful technique to implement both decisions and to detect situations, but the two are not the same, and typically serve different purposes in your enterprise IT architecture.