I want to implement a configurable system where a user can define some event-based triggers, and then something happens after that.
Example:
If a customer’s contract will expire next month, then send them a reminder.
There are two conditions: the customer has a contract with expiration X, and the current time is >= x – 1 month.
This means:
- If we create a contract, and it will already expire in a month, we want to notify the customer.
- If we already have a contract, and the time is now less than a month until it expires, then we want to notify the customer.
My question is, how do systems, such as Zapier, implement a DSL that scales well to checking when the antecedent of these rules is true for specific data (e.g., contracts)? That’s possibly millions of potential rule invocations that need to be checked (thousands of rules * thousands of contracts).
I thought that:
- Maybe it’s similar to logic programming (e.g., Datalog), where the predicates are indexed. But that would mean constructing and interpreting an enormous logic program in an enterprise system.
- Maybe the high-level conditions correspond to conditions on a database query, and periodically a DB query is constructed (from the high-level conditions) and then executed – this has the downside of having a delay – but presumably could be efficient with the right indexing.
- Events come into the system (e.g., a contract is created), and somehow are used to instantiate rules (e.g., an instance of the general rule above for an X expiry date, with a specific one), these instances are persisted in a DB with some indexing. And when further events come in the DB is checked for any rules that should be triggered <- sorry if this is vague, but is similar to the Datalog approach.
But what is the common approach taken?