Skip to content

[Perf] Cache API key validation to eliminate per-request MongoDB hit #135

@ishaanxgupta

Description

@ishaanxgupta

Description
Every authenticated request triggers a MongoDB find_one + update_one round-trip to validate the API key and update last_used. This is the single biggest latency contributor for authenticated endpoints, adding roughly 30–80ms to every request.

Code Reference
src/database/api_key_store.py - validate_api_key()

Proposed Fix
Add an in-memory TTL cache (e.g. cachetools.TTLCache) keyed by key_hash.
Cache validated results for 60–300s.
Batch last_used updates in a background task instead of blocking the main request path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions