‘Rule Engine’ In the name itself it has a word ‘Engine’ and it may be because many times it handles complex processing to produce the desired output from the provided inputs. Now why I choose this topic to write a blog on??? Because recently I came across one scenario in which the client loss the revenue only because of one small flaw in the Rule Engine, and many time it is better to learn from the other’s mistake and hence I thought to wright something on the ‘Rule Engine’!
So, the analogy behind the word Engine (which we normally use for the mechanical objects) is, both Mechanical Engines and Rule Engines converts some inputs into the desired outputs. The mechanical Engine converts fuels into the motion, and Rule Engine converts the data into the decision or in simple word Mechanical engine powers machines and Rule Engine powers decisions.
Rule Engine is treated as a ‘Brain’ of the business process and hence it is an important part if the software system. It consists of the rule on the provided input to process the data and give the output. There are many different types of rule engines with complex definitions you will find on the internet, but I would like to talk about it in the simple words. So as per my understanding we can divide the Rule Engines in two types based on the place it is written in, ‘SQL Based Rule Engine’ and ‘Code Based Rule Engine’.
SQL Based Rule Engine
In this type of rule engine, most of the business rules are written on the database level using Stored Procedures, SQL Functions, Triggers etc. The data comes into the database tables and based on the data and using different conditions using filters, joins, case statements the system produces the output. This is the most common approaches where the database is used heavily.
Some of the advantages of the SQL Based Rule engines are,
- Faster execution as the logic works directly on the database side
- Centralized rules handling on the database level
- The rules are easy to manage and handle when they are more related to the data Along with the advantages, there are disadvantages as well,
- When the number of rules get increases, it becomes difficult to handle, and the queries get increasingly complex and hence becomes difficult to maintain
- One change in the query sometimes impact on multiple rules
Code Based Rule Engine
As the name says, in the code-based rule engines, the business rules are written the application code programming languages like C#, Python, Java etc. Here the rule conditions are written using the programming language syntax for conditions like If-Else, Case Statements, Configurations etc. This approach is mostly used when the rules are more complex and need frequent changes and need to be flexible.
Some of the advantages of the Code Based Rule engines are,
- It has a better readability
- The rules are written in the structured way
- We can write the unit test cases to test the rules and the changes
- It is flexible to implement new changes The disadvantage for this approach is, if the rules are hidden in the assembly, it is difficult to see as they are hidden in the code.
Irrespective of type of the Rule Engine, the biggest change with the Rule Engine is managing the rules properly. Over the time, the business keeps adding the new conditions, exceptions and new cases. And because of this, the rule engine becomes increasingly complex. Hence below are some best practices we can implement while building or managing the rule engines,
- Keep the rules modular – Avoid writing rules in one query on in one code block, rather divide them into the logical blocks
- Documentation – Document the Rule Engine rule and changes properly. You can use of the technic like ADR (Architecture Decision Record) etc.
- Maintaining the Priority/Order – Keep the Priority/Order of the rules in mind as rules are mainly condition based
- Test the Edge Cases – Make sure to test all the edge cases and negative scenarios along with the other affecting applications (regression testing).
- Maintain the Logs – Logging is helpful in tracing the issues or the flow
Conclusion
A rule engine is a Brain of the application; it is a decision-making code part of the application. One small mistake in the rule can cause the major financial or functional impact. So whenever, we design or modify a rule engine, we must be very careful, test is thoroughly and design it in a way that it is clear, modular, maintainable and reliable. Because at the end of the day it is not a SQL Based or Code Based – It is a core logic that drives the business decisions!!
Happy Coding!
Top comments (1)
Very informative, thanks for sharing! 🔥