How to improve Drools performance of rule execution

Solution Verified - Updated -

Environment

  • JBoss Enterprise Business Rule Management System Platform (BRMS)
    • 5
    • 6
  • Red Hat Decision Manager
    • 7

Issue

  • Using a profiler, I observed that StatefulKnowledgeSession.insert() is the bottle-neck. How can I tune it?
  • Converting drl/xls to java/class files can be a bottle-neck?
  • Are there any white papers or other sources that detail the likely performance characteristics of a BRMS system?

Resolution

Please follow the performance tips strewn in docs.

http://docs.redhat.com/docs/en-US/JBoss_Enterprise_BRMS_Platform/5/html-single/JBoss_Rules_5_Reference_Guide/index.html

Cross-products can become huge and, therefore, have the potential to cause performance problems. To prevent this, use variable constraints to eliminate nonsensical results:
Literal Restrictions using the operator '==' provide for faster execution as we can index using hashing to improve performance. 
One can bind variables to facts and their fields and then use them in subsequent field constraints. A bound variable is called a declaration. The type of the field being constrained determines which operators are valid; coercion will be attempted where possible. Use the == operator to bind variable restrictions for very fast performance. 
Use nested accessors carefully as they have a much greater performance impact than direct field accesses. 
Do not overuse eval because it reduces the declarativeness of the rules which can lead to a poorly performing engine. 
Evals cannot be indexed and, thus, are not as efficient as field constraints.
Put the most restricting condition in the LHS as first.

In addition,

  • if you call a heavy task (e.g. querying database) in LHS, it could be a major bottle-neck because it may be called multiple times. Preparing data before calling insert() would give you better performance.

  • Converting drl/xls is done by KnowledgeBuilder for the first time. KnowledgeBase will be cached in an application so it wouldn't be a bottle-neck.

  • Drools 5 evaluates rule condition during the insert stage (ksession.insert). So if the rules are not written in an optimized way, even this phase can take longer time. In Drools 6, the evaluation is rather moved to fireAllRules().

  • A large and complex kbase could consume memory. It may result in lots of short-living objects so frequent minor gc could be a bottle-neck. In that case, splitting to multiple smaller kbases could help. (but it's possible only when you can split the rules into logically separated groups)

  • If the execution is slow only at the first request, check this article : https://access.redhat.com/solutions/2790361

  • If you use "from" in LHS, directly inserting facts is faster.

Diagnostic Steps

  • Capture thread dumps, gc logs as usual performance investigation. Profiler tool would be helpful as well.
    • "Resolution" section describes various general knowledge but identifying bottle-neck is the most important

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments