Skip to content

Fix issue with n+1 API calls on device/owners#2

Draft
pelted wants to merge 1 commit into
mainfrom
chris/fix-n+1-api-call
Draft

Fix issue with n+1 API calls on device/owners#2
pelted wants to merge 1 commit into
mainfrom
chris/fix-n+1-api-call

Conversation

@pelted
Copy link
Copy Markdown
Contributor

@pelted pelted commented Feb 27, 2026

What Changed

Before: The loop iterated through every record, issuing up to 2 await calls per unique device — fully sequential. 100 unique devices = up to 200 serial API calls.

After: Two batched concurrent rounds:

  • Round 1 — Collect all unique device IDs from the records, then asyncio.gather to fetch all /devices/{id} concurrently.
  • Round 2 — Extract unique person IDs from those device responses, then asyncio.gather to fetch all /people/{id} concurrently.
  • Apply — Walk the records once more and apply cached owner info.

The asyncio.Semaphore(10) caps concurrency to, at most, 10 API requests in-flight at a time to avoid hammering the Kolide API.

For 100 unique devices owned by 100 unique people, this goes from ~200 sequential round-trips to 2 concurrent batches — a dramatic improvement.

…f device and person data. This improves performance by batching requests and reduces the need for sequential API calls. Update records in-place with owner_name and owner_email fields based on fetched data. For 100 unique devices owned by 100 unique people, this goes from ~200 sequential round-trips to 2 concurrent batches.
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.

1 participant