Skip to content

Batch independent DB queries in broker with asyncio.gather() #7

@artugro

Description

@artugro

Problem

BrokerService.invoke_agent() makes 4-6 sequential database round trips per invocation:

  1. get_effective_config() (broker config)
  2. get_by_id() (conversation validation)
  3. get_agent_by_agent_id() (agent lookup)
  4. get_agent_credential() (credential fetch)
  5. count_invocations_for_integration() (daily quota — now Redis, but DB fallback)
  6. count_invocations_for_integration() (monthly quota — same)

Each DB round trip adds 1-3ms with connection pooling. These are mostly independent and can run concurrently.

Proposed Solution

Group independent queries using asyncio.gather():

  • Parallel group 1: agent lookup + config lookup + conversation lookup (if provided)
  • Parallel group 2: daily quota + monthly quota (if Redis fallback)

This should reduce broker overhead by 30-50% per invocation.

Files

  • src/services/broker.py — main target

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions