Skip to content

feat: add backend caching for AMT API endpoints#768

Open
nmgaston wants to merge 28 commits into
device-management-toolkit:mainfrom
nmgaston:backendCaching
Open

feat: add backend caching for AMT API endpoints#768
nmgaston wants to merge 28 commits into
device-management-toolkit:mainfrom
nmgaston:backendCaching

Conversation

@nmgaston

@nmgaston nmgaston commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds backend caching for AMT API endpoints using robfig/go-cache to dramatically reduce latency and backend load for repeated requests within short time windows.

Problem

When users interact with the KVM interface or monitor device status, the frontend makes repeated API calls to fetch power state, features, and KVM display settings. Each call requires a round-trip to the AMT firmware, adding 150-500ms latency per request.

Solution

Implements thread-safe in-memory caching using robfig/go-cache with TTL-based expiration:

  • Power state: 5-second TTL (state changes frequently during power actions)
  • Features & KVM displays: 30-second TTL (settings rarely change)
  • Automatic invalidation: Cache cleared when power actions are performed

Performance Impact

Testing with real AMT device (GUID: d0c96538-9e19-4bc0-9d73-8864e641f77f):

Response time improvement: 25-164x faster for cached requests
Without cache: ~1.15s per request (1150ms)
Cold cache (first call): ~170ms
Warm cache (subsequent calls): 7-45µs (microseconds!)
Backend load reduction: ~67% fewer AMT API calls
Cache efficiency: ~100% hit rate within TTL window

Implementation Details

  • Uses robfig/go-cache for optimal performance without reflection overhead
  • Thread-safe with internal locking for concurrent access
  • Configurable TTLs: Set ttl: 0 in config.yml to disable caching entirely
  • Cache bypass: Use ?refresh=true query parameter to skip cache and fetch live data
  • KVM optimization: New combined endpoint reduces 4 API calls to 1 during KVM initialization
  • Factory pattern (factory.go) for future Redis support
  • Minimal code changes to existing endpoints
  • No breaking changes to API contracts

Configuration

cache:
  # Cache time-to-live for AMT features and KVM data
  # Valid range: 0 (disabled) to 5 minutes (300s)
  # Set to 0 to disable caching entirely
  ttl: 30s
  
  # Separate TTL for power state (changes frequently)
  # Valid range: 0 (disabled) to 1 minute (60s)
  # Set to 0 to disable power state caching only
  powerstate_ttl: 5s

Security

  • Validated configuration bounds: TTL values are validated at startup
    • Maximum ttl: 5 minutes (prevents excessive stale data retention)
    • Maximum powerstate_ttl: 1 minute (limits power state staleness)
    • Minimum: 0 (allows disabling without negative values)
  • Startup rejection: Invalid configurations prevent server start with clear error messages
  • Prevents misconfigurations: Protects against accidental or malicious cache timing attacks

Testing

  • Verified cache hit/miss behavior with server logs
  • Performance tested with quick-cache-test.sh script using real AMT device
  • Confirmed cache invalidation on power actions
  • Validated 3,800-7,500x speedup in server-side response times (170ms → 22-45µs)

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backend caching for AMT API endpoints to reduce latency and backend load

3 participants