Skip to content

Cache Key Collision Risk: Symbol order causes cache misses #21

Description

@bearmug

Problem

The current cache key generation in lookupSymbols method creates different cache keys for the same symbols in different orders, reducing cache efficiency.

Current implementation:

const cacheKey = JSON.stringify({ type: "lookup", symbols, columns: inputColumns });

This means:

  • ["TVC:SPX", "TVC:DJI"] → Cache key A
  • ["TVC:DJI", "TVC:SPX"] → Cache key B (different!)

Both requests return identical data but generate separate cache entries, wasting memory and causing unnecessary API calls.

Impact

  • Cache miss rate: Unnecessarily high when users request same symbols in different orders
  • Memory inefficiency: Duplicate data stored in cache for symbol permutations
  • API rate limit: More API calls than necessary
  • Performance: Slower response times due to cache misses

Example Scenario

// First request - cache miss, API call made
lookup_symbols({ symbols: ["TVC:SPX", "TVC:DJI", "TVC:IXIC"] })

// Second request - cache miss again! (different order)
lookup_symbols({ symbols: ["TVC:IXIC", "TVC:SPX", "TVC:DJI"] })

Both requests fetch the same 3 indexes but hit the API twice instead of using cached data.

Proposed Solution

Sort symbols before generating cache key:

const cacheKey = JSON.stringify({ 
  type: "lookup", 
  symbols: [...symbols].sort(),  // Sort for consistent cache keys
  columns: inputColumns 
});

Benefits:

  • ✅ Consistent cache keys regardless of symbol order
  • ✅ Higher cache hit rates
  • ✅ Reduced API calls (respects rate limits better)
  • ✅ Lower memory usage (no duplicate cache entries)
  • ✅ Faster response times for cached data

Implementation Notes:

  • Use [...symbols].sort() to avoid mutating input array
  • JavaScript's default sort is sufficient (lexicographic ordering)
  • Consider sorting columns array as well for complete consistency
  • No breaking changes - purely internal optimization

Location

File: src/tools/screen.ts
Method: lookupSymbols (around line 374)

async lookupSymbols(input: { symbols: string[]; columns?: string[] }): Promise<any> {
  const { symbols, columns: inputColumns } = input;

  // ... validation code ...

  // Build cache key (CURRENT - needs fix)
  const cacheKey = JSON.stringify({ type: "lookup", symbols, columns: inputColumns });
  
  // Should be:
  const cacheKey = JSON.stringify({ 
    type: "lookup", 
    symbols: [...symbols].sort(), 
    columns: inputColumns 
  });

Additional Considerations

  • Column order: Should columns array also be sorted for consistency?
  • Test coverage: Add tests verifying cache hits for different symbol orders
  • Documentation: Note in code comments that sorting ensures cache consistency
  • Metrics: Consider logging cache hit/miss rates to measure improvement

References

Priority

Medium - Not critical for functionality but improves efficiency and respects rate limits. Good optimization to include before v1.0 release.

Activity

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions