Skip to content

feat!(perf): coalesce identical queries and limit concurrent query execution#805

Open
shahzeelahmed wants to merge 30 commits intodevelopfrom
redlock
Open

feat!(perf): coalesce identical queries and limit concurrent query execution#805
shahzeelahmed wants to merge 30 commits intodevelopfrom
redlock

Conversation

@shahzeelahmed
Copy link
Member

@shahzeelahmed shahzeelahmed commented Feb 16, 2026

Problem: Each site has a limited number of worker threads to handle requests. when a dashboard has 20+ queries which takes more than a minute to execute, each thread gets blocked for the duration of I/O bound work. This causes the site to become slow and even downtime in some cases. now, the max number of queries which can run at a time will be capped at 6 by default and the client will poll for the retries.

  • Prevents duplicate execution of identical queries using redlock mechanism so when multiple users run the same query, only one actually runs and others poll for the result
  • Adds a semaphore to restrict number of concurrent queries which can run at any given time (min 6 and max 18)
  • Queries which exceed semaphore limit are retried from client side to free up workers
  • Track query changes using query_hash field in Insights Query v3
  • Monitoring for lock contention
  • Configurable semaphore limit from general settings
  • Test Coverage for this feature

TODO

  • use shared keys for locks and query_hash
  • use make_key method to create hash
  • remove monitoring
  • minimize calls from client

@shahzeelahmed shahzeelahmed marked this pull request as draft February 16, 2026 08:35
@shahzeelahmed shahzeelahmed marked this pull request as ready for review February 17, 2026 05:51
@shahzeelahmed shahzeelahmed changed the title feat(perf): coalesce redundant queries and limit concurrent query execution feat!(perf): coalesce redundant queries and limit concurrent query execution Feb 17, 2026
@shahzeelahmed shahzeelahmed marked this pull request as draft February 19, 2026 08:44
@shahzeelahmed shahzeelahmed changed the title feat!(perf): coalesce redundant queries and limit concurrent query execution feat!(perf): coalesce identical queries and limit concurrent query execution Feb 19, 2026
release_lock(lock_key)

def _publish_query_complete(self, lock_key, dashboard=None):
if not dashboard:
Copy link
Member

@nextchamp-saqib nextchamp-saqib Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The events should be sent regardless of dashboard param? Or is it intentionally meant only for dashboards?

IMO, results should be made available on the client via realtime events for query & chart builder as well, to avoid duplicate execution. In this case "dashboard" param is unnecessary (?)

@shahzeelahmed shahzeelahmed marked this pull request as ready for review March 6, 2026 10:46
dashboard.refresh()

// avoid listening if user navigates to another dashboard room
onBeforeUnmount(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be done in DashboardBuilder too?

break
def _cache_execution_result(self, lock_key, result, cache_expiry=600):
cache_key = f"insights:exec_result:{lock_key}"
frappe.cache().set_value(cache_key, frappe.as_json(result), expires_in_sec=cache_expiry)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we eliminate this duplicate cache? results are already cached in execute_ibis_query function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants