Skip to content

Conversation

Copy link

Copilot AI commented Dec 23, 2025

Identified and fixed multiple performance bottlenecks causing excessive database round-trips in message publishing, queue operations, and subscription lookups.

Changes

Batch queue fetching in topic message publishing

  • Added QueueRepository.GetMany(ids []string) to fetch multiple queues in one query
  • Modified Topic.CreateMessage() to collect queue IDs and fetch in batch
  • Impact: Reduces N+1 queries to 2 queries when publishing to N subscriptions
// Before: N queries in loop
for _, subscription := range subscriptions {
    queue, err := t.queueRepository.Get(ctx, subscription.QueueID)
    // ...
}

// After: 1 batch query
queues, err := t.queueRepository.GetMany(ctx, queueIDs)

Batch message updates on list operations

  • Replaced per-message UPDATEs with single batch UPDATE using ANY() operator
  • Maintains in-memory state updates for returned message objects
  • Impact: Reduces N UPDATE queries to 1 when listing N messages
-- Single query replaces N individual updates
UPDATE messages 
SET delivery_attempts = delivery_attempts + 1, 
    scheduled_at = $1, 
    updated_at = $2 
WHERE id = ANY($3)

Database index for subscription lookups

  • Added B-tree index on subscriptions.topic_id
  • Migration: 000003_add_subscriptions_topic_idx

Removed redundant Get calls in service layer

  • Eliminated validation queries in Delete, Stats, Purge, Cleanup operations
  • Database operations already fail appropriately when entity doesn't exist
  • Impact: Reduces 2 queries to 1 per operation

Performance Characteristics

Operation Before After Reduction
Publish to 100 subscriptions 101 queries 2 queries 98%
List 50 messages 51 queries 2 queries 96%
Queue delete/stats/purge/cleanup 2 queries 1 query 50%
Original prompt

Identify and suggest improvements to slow or inefficient code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 23, 2025 23:23
- Add GetMany method to QueueRepository to batch fetch queues
- Optimize topic.CreateMessage to fetch all queues in single query instead of N queries
- Optimize message.List to use single batch UPDATE instead of N individual updates
- Add database index for subscriptions.topic_id for faster lookups
- Remove redundant Get calls in service layer Delete/Stats/Purge/Cleanup operations

Co-authored-by: allisson <5202+allisson@users.noreply.github.com>
- Update queue, subscription, and topic service tests to remove expectations for redundant Get calls
- Add GetMany test for batch queue fetching
- Update CreateMessage test to expect GetMany instead of individual Get calls
- All service and domain tests now passing

Co-authored-by: allisson <5202+allisson@users.noreply.github.com>
- Explain that DeliverySetup updates messages in memory for correct return values
- Clarify that SQL query performs the actual database update
- Addresses code review feedback about potential confusion

Co-authored-by: allisson <5202+allisson@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements to slow or inefficient code Optimize database queries: eliminate N+1 problems and batch operations Dec 23, 2025
Copilot AI requested a review from allisson December 23, 2025 23:30
@allisson allisson marked this pull request as ready for review December 25, 2025 20:07
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