Skip to content

Performance: Optimize fetch-before-delete pattern in event emission #223

Closed

Description

@malibio

Context

Current implementation in HttpAdapter and TauriAdapter performs a GET request before every DELETE operation to capture node metadata for event emission. This was identified during code review of PR #222.

Current Pattern:

async deleteNode(id: string): Promise<void> {
  // Fetch node before deletion to get metadata for events
  const nodeBeforeDeletion = await this.getNode(id);
  
  const response = await fetch(..., { method: 'DELETE' });
  
  // Emit events with metadata from fetched node
  if (nodeBeforeDeletion) {
    eventBus.emit<NodeDeletedEvent>({ ... });
  }
}

Problem

Performance Impact:

  • 2x HTTP overhead per delete operation (GET + DELETE instead of just DELETE)
  • For batch deletions (e.g., cleaning up 100 nodes), this doubles HTTP requests from 100 to 200
  • Extra latency for every delete operation

Root Cause:

  • Backend returns minimal response on DELETE (no node metadata)
  • Events require parentId and other metadata
  • Frontend must fetch before delete to capture this data

Proposed Solutions

Option A: Backend Returns Deleted Node Metadata (Recommended)

Modify backend DELETE endpoint to return the deleted node data in response:

// Current: Returns 204 No Content
DELETE /api/nodes/{id} -> 204

// Proposed: Returns deleted node data
DELETE /api/nodes/{id} -> 200 { id, parentId, nodeType, ... }

Pros:

  • ✅ Eliminates extra GET request
  • ✅ Proper REST pattern (DELETE can return entity representation)
  • ✅ Single source of truth (backend knows what was deleted)

Cons:

Option B: Cache Node Metadata in Frontend State

Maintain node metadata cache in frontend (ReactiveNodeService):

private nodeMetadataCache = new Map<string, { parentId: string | null, nodeType: string }>();

async deleteNode(id: string): Promise<void> {
  const metadata = this.nodeMetadataCache.get(id);
  // ... delete operation
  // Use cached metadata for events
}

Pros:

  • ✅ No backend changes needed
  • ✅ Eliminates extra fetch

Cons:

  • ❌ Cache can become stale
  • ❌ Memory overhead for large node counts
  • ❌ Complex cache invalidation logic

Option C: Conditional Fetch Based on Subscriber Count

Only fetch metadata if event subscribers exist:

async deleteNode(id: string): Promise<void> {
  const needsMetadata = eventBus.hasActiveSubscribers('node:deleted') || 
                         eventBus.hasActiveSubscribers('hierarchy:changed');
  
  const nodeBeforeDeletion = needsMetadata ? await this.getNode(id) : null;
  // ... rest of delete logic
}

Pros:

  • ✅ Optimizes for no-subscriber case
  • ✅ Simple to implement

Cons:

  • ❌ Still 2x overhead when subscribers exist (common case)
  • ❌ Requires EventBus API change

Recommendation

Phase 1 (Current): Accept 2x overhead as documented trade-off
Phase 2: Implement Option A when addressing Issue #219 (backend DELETE improvements)
Phase 3: Add performance metrics to track actual impact

Acceptance Criteria

  • Backend DELETE returns deleted node metadata
  • Frontend adapters use response metadata for events
  • Eliminate extra GET request before DELETE
  • All event emission tests still pass
  • Performance benchmarks show improvement
  • No regression in event emission behavior

Related Issues

Metrics to Track

  • Before: 2 HTTP requests per delete (GET + DELETE)
  • After: 1 HTTP request per delete (DELETE with metadata response)
  • Improvement: 50% reduction in delete operation overhead

Priority: Medium
Complexity: Medium (requires backend + frontend changes)
Estimated Impact: High (affects every delete operation)

Activity

  1. added
    enhancement
    on Oct 11, 2025
  2. malibio commented on Dec 5, 2025

    @malibio
    CollaboratorAuthor

    Architecture has changed significantly since this was written:

    If performance issues are found, create new issues against current architecture.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions