elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
5.88k stars 1.06k forks source link

[PERF] Optimize WorkflowDefinitionActivity Loading with Caching #5249

Open sfmskywalker opened 3 weeks ago

sfmskywalker commented 3 weeks ago

Problem

Currently, when handling workflows that involve WorkflowDefinitionActivity, which links to other workflows, 1 to 3 SQL queries are executed to fetch the linked workflow definition per WorkflowDefinitionActivity instance. This repetitive querying occurs regardless of whether the same workflow definition was previously loaded, leading to inefficiencies. This issue is exacerbated in complex workflow graphs with deep nesting and multiple instances of WorkflowDefinitionActivity, significantly impacting both application startup time and workflow execution performance when caching is not enabled.

Proposed Solution

To address this performance bottleneck, it is proposed to implement a caching mechanism. By introducing a dictionary to cache previously loaded workflow definitions, we can avoid redundant SQL queries during the initialization of the workflow graph. This dictionary would store workflow definitions that have already been retrieved, thereby reducing the number of database hits.

Expected Impact

Notes

glime-ai[bot] commented 3 weeks ago

To address the performance bottleneck you've described, implementing a caching mechanism for workflow definitions is indeed a viable solution. The proposed caching strategy would involve the following steps:

  1. Caching Workflow Definitions: Create a dictionary cache that stores workflow definitions keyed by their definition ID. When a WorkflowDefinitionActivity is encountered, the system should first check this cache before querying the database.

  2. Cache Retrieval: Upon encountering a WorkflowDefinitionActivity, the system should attempt to retrieve the corresponding workflow definition from the cache.

  3. Database Fallback: If the workflow definition is not found in the cache, it should then be loaded from the database and subsequently added to the cache for future use.

  4. Cache Invalidation: Implement a strategy for invalidating or updating the cache when workflow definitions are updated or deleted to ensure that the cache remains consistent with the database.

By implementing this caching mechanism, you can expect the following impacts:

It's important to note that while caching can improve performance, it also introduces additional complexity, particularly around cache management and invalidation. Therefore, careful consideration must be given to the cache lifetime, eviction policies, and synchronization with the database to ensure data consistency.

This comment was generated by Glime.