As an Azure Logic Apps user, you're likely familiar with the need for automation and decision-making in your workflows. The Azure Rules Engine is a game-changer for this purpose.
It allows you to create custom rules that can be applied to your workflows, enabling you to make decisions based on conditions and take actions accordingly. This can be a huge time-saver and improve the overall efficiency of your workflows.
With the Azure Rules Engine, you can define rules using a simple, intuitive syntax that's easy to understand and use. The engine is also highly scalable, making it suitable for large and complex workflows.
By integrating the Azure Rules Engine into your Azure Logic Apps workflows, you can automate decision-making and take your workflows to the next level.
Configure Rules Engine
You can configure the Rules Engine in the Azure portal or using the Azure CLI. To start, you'll need to navigate to your Azure Front Door (classic) resource and select Rule Engine configuration from under Settings.
In the Azure portal, you can add a new configuration by selecting + Add and giving it a name. You can then start creating your first Rules Engine configuration by entering a name for your first rule and selecting + Add condition or + Add action to define your rule.
The priority of the rules within your configuration is determined by using the Move up, Move down, and Move to top buttons. The priority is in ascending order, meaning the rule first listed is the most important rule.
To associate a rules engine configuration with a routing rule, go to the Azure Front Door (classic) designer and select a Route. Then select the Rules engine configuration to associate with the routing rule.
Alternatively, you can configure the Rules Engine using the Azure CLI. To do this, you'll need to install the Azure CLI and add the "front-door" extension. Once installed, you can create a Rules Engine with one rule using the az network front-door rules-engine rule create command.
Here are the steps to create a Rules Engine using the Azure CLI:
- Install the Azure CLI and add the "front-door" extension
- Create a Rules Engine with one rule using the az network front-door rules-engine rule create command
- List all the rules using the az network front-door rules-engine rule list command
- Add a forwarding route override action using the az network front-door rules-engine rule action add command
- Link a rules engine configuration to a routing rule using the az network front-door routing-rule update command
Understanding Rules
A rule identifies one or more types of requests and the set of actions that gets applied to them. It's a crucial part of the Azure Rules Engine.
A rule consists of a set of conditional expressions that define the logic through which requests are identified, and a set of match conditions that define the criteria used to identify requests. These elements are identified in the illustration of a rule.
Here are the key components of a rule:
- Conditional expressions
- Match conditions
Rule
A rule identifies one or more types of requests and the set of actions that gets applied to them. It's essentially a set of instructions that the Rules Engine follows to determine the next course of action.
A rule consists of a set of conditional expressions, match conditions, and features that define how the content delivery network handles requests. This is similar to a recipe, where you have specific ingredients and steps to follow.
A rule can have multiple conditions, but it's the match conditions that define the criteria used to identify requests. For example, a rule might match requests with a specific file extension, such as .jpg or .png.
Here are some common types of match conditions:
- RequestFilenameExtension
- RequestHeader
- RequestMethod
- RequestPath
These match conditions can be combined using logical operators, such as AND or OR, to create more complex rules. For instance, a rule might match requests with a specific file extension and a specific request method.
A rule also defines the actions that get applied to the matched requests. These actions can include things like rewriting headers, forwarding routes, or caching responses. The actions are executed in order, with higher-priority actions taking precedence over lower-priority ones.
Logical OR Operators
Logical OR operators can be a challenge to work with, especially when you have multiple conditions to evaluate.
Using too many logical OR operators in conditions can create a lot of permutations that expand the rules engine's analysis network, making it take a long time to normalize the rule.
The solution is to change the rule into a disjunctive normal form, where the OR operator is only at the top level. This can be tricky to do in the Microsoft Rules Composer, so you might find it easier to create the rule programmatically.
Consider developing a helper component that performs the OR operations and returns a Boolean value, which can then be used in the rule. This approach can simplify the rule and make it more efficient.
Alternatively, you can split the rule into multiple rules, where each rule checks for a flag set by a previously executed rule or uses an object that a previously executed rule asserted.
Condition Evaluation and Execution
The Azure Logic Apps Rules Engine's condition evaluation and execution process is a three-stage algorithm that efficiently runs rulesets. The first stage, Match, uses pattern matching across all rules in the ruleset to identify shared conditions, which are matched only once for efficiency.
The rules engine stores partial condition matches in its working memory to expedite subsequent pattern matching operations. This helps to speed up the matching process and reduce computational overhead.
In the Conflict Resolution stage, the rules engine examines candidate rules to determine the next set of rule actions to run, based on a predetermined resolution scheme. The default scheme is based on rule priorities, which can be configured in the Microsoft Rules Composer.
The priority is a rule property that you can configure, with higher numbers indicating higher priority. This means that if multiple rules are triggered, the rules engine will run the higher-priority actions first.
The Action stage simply runs the actions in the resolved rule, which can assert new facts into the rules engine, causing the cycle to continue and is also known as forward chaining.
Condition Evaluation and Execution
Condition Evaluation and Execution is a crucial step in the rules engine's algorithm. This stage involves matching facts against predicates defined in the rule conditions, using a three-stage algorithm.
The match stage is the first step, where the rules engine matches facts against the predicates. Pattern matching occurs across all the rules in the ruleset, and conditions that are shared across rules are matched only once.
The rules engine stores partial condition matches in working memory to expedite subsequent pattern matching operations. This allows for more efficient matching and reduces the number of iterations.
The output from the pattern matching phase contains updates to the rules engine's agenda. This agenda is a list of candidate rules found during the matching stage.
Here's a breakdown of the three stages:
- Match: The rules engine matches facts against predicates.
- Conflict resolution: The rules engine examines candidate rules to determine the next set of rule actions to run.
- Action: The rules engine runs the actions in the resolved rule.
The rules engine executes all actions in the currently running rule before the match phase repeats. This ensures that the current rule is completed before moving on to the next one.
The default conflict resolution scheme is based on rule priorities within a ruleset. The larger the number, the higher the priority. This means that higher-priority rules will run before lower-priority rules.
Instances and Selectivity
The Instances and Selectivity properties are used by the rules engine to optimize condition evaluation. These property values allow the engine to use the fewest possible instances first in condition evaluations and then use the remaining instances.
You can set the Instances property to the expected number of class instances in working memory to improve performance. If you know the number of object instances beforehand, do it.
The Selectivity property represents the percentage of class instances that successfully pass the rule conditions. You can set this property to the percentage of object instances that pass the conditions beforehand to improve performance.
You can only programmatically set these property values because the Microsoft Rules Composer doesn't expose them.
Priority
The priority of rules in the Azure Rules Engine is a crucial aspect to understand. Priority determines the order in which rules are executed, with higher numbers having higher priority.
By default, all rules are set to 0 as the priority for execution. However, you can change this priority on each individual rule. The priority can range to either side of 0 with larger numbers having higher priority.
The engine executes actions from highest priority to lowest priority. This means that if two rules have the same condition, the rule with the higher priority will execute first.
For example, if Rule 2 has a higher priority than Rule 1, Rule 2 will execute first even if Rule 1's conditions are met first.
Here's a summary of how priority affects the order of execution:
You can use the Move up, Move down, and Move to top buttons to determine the priority of rules within your configuration. The priority is in ascending order, meaning the rule first listed is the most important rule.
By understanding and controlling the priority of rules, you can optimize the execution of your Azure Rules Engine and achieve the desired outcomes.
Sources
- https://learn.microsoft.com/en-us/azure/frontdoor/front-door-tutorial-rules-engine
- https://learn.microsoft.com/en-us/azure/logic-apps/rules-engine/rules-engine-optimization
- https://learn.microsoft.com/en-us/azure/cdn/cdn-verizon-premium-rules-engine-reference
- https://www.nuget.org/packages/RulesEngine
- https://learn.microsoft.com/nb-no/azure/frontdoor/front-door-tutorial-rules-engine
Featured Images: pexels.com