When I was new to drools I went thru some forums and developed an application with drools configured using KnowledgeBuilder api's and StatefulKnowledgeSessions. At that time the drools files are less in number and packaged with application. When profiling I found the drools are consuming much memory and the memory allocations rates (TLAB's) are high. It's making me think if I need to have a caching solution in place to NOT create KnowledgeSessions everytime there is a request to application. My application supports almost more than 100 type of events and for every event I have 3 different types of drools files which I have execute one at a time to filter results intermediately. Also the number of drools files in the application and frequency to change/configure new is increasing and I have to externalize drools from application package. I have different drools file for different needs and not all required every time, so I am thinking to keep under a REST service backed up a NO-SQL database in which these are pumped into whenever we want to change. By thus I am thinking the application can GET the ONLY needed/required drool files (I will have naming conventions of the drools files as such) from the service and if required cache them too locally (will use Guava caching/any Inprocess cache to evict accordingly if there is no need), by thus the memory consumption/allocation rate may come down (guessing) Am I right with above design? If such how to read drools from cache/stringbuilder/memory? Currently I read them from file system by the below api
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(ResourceFactory.newClassPathResource("drools_conventions.drl"), ResourceType.DRL);
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
StatefulKnowledgeSession kSession = knowledgeBase.newStatefulKnowledgeSession();
List filter = new ArrayList<>();
kSession.insert(dispatchFactInput);
kSession.setGlobal("Rule", filter);
kSession.fireAllRules();
kSession.dispose(); ```
Can I cache Knowledgebase/StatefulKnowledgeSession?
Since I am new to drools I would like to take opinions/suggestions, learn to implement the best/possible solution to above situation.
I think what you could take a look on are stateless sessions. I think if you just insert facts, fire rules and then immediately dispose the KieSession, that is the best option for you. Other than that there is already a caching mechanism of sessions available in newer Drools versions.