Skip to content

✨ Add built-in tenant-aware APQ caching support#70

Merged
evoludigit merged 5 commits into
devfrom
feature/apq-tenant-context-support
Sep 21, 2025
Merged

✨ Add built-in tenant-aware APQ caching support#70
evoludigit merged 5 commits into
devfrom
feature/apq-tenant-context-support

Conversation

@evoludigit
Copy link
Copy Markdown
Contributor

Summary

Implements built-in tenant-aware caching for Automatic Persisted Queries (APQ), enabling secure multi-tenant applications with automatic response isolation.

Key Changes

  • Built-in tenant isolation in base backends - Both MemoryAPQBackend and PostgreSQLAPQBackend now automatically isolate cached responses by tenant when context is provided
  • Context propagation through APQ stack - Router passes context to backend methods for tenant-aware operations
  • Zero configuration required - Works out of the box, no custom implementations needed

Implementation Details

MemoryAPQBackend

  • Generates tenant-specific cache keys: {tenant_id}:{hash}
  • Maintains separate cache spaces per tenant
  • Global cache still available for non-tenant requests

PostgreSQLAPQBackend

  • Added tenant_id column to responses table
  • Composite primary key (hash, COALESCE(tenant_id, ''))
  • Indexed tenant_id for performance

Security

  • Prevents cross-tenant data leakage
  • Each tenant only sees their own cached responses
  • Validated through comprehensive test suite

Testing

  • ✅ All 3287 tests passing
  • ✅ Added tenant isolation tests
  • ✅ Context propagation tests
  • ✅ Backward compatibility tests

Usage

# Just pass context with tenant_id - isolation is automatic!
context = {"user": {"metadata": {"tenant_id": "acme-corp"}}}
response = backend.get_cached_response(hash, context=context)

Documentation

  • Added comprehensive guide: docs/apq_tenant_context_guide.md
  • Updated example: examples/apq_multi_tenant.py
  • Clean, focused implementation without transitional artifacts

🤖 Generated with Claude Code

Lionel Hamayon and others added 5 commits September 21, 2025 22:29
TDD Cycle Complete:
- RED: Tests written expecting context parameter (initially failed)
- GREEN: Added context parameter with default None (tests pass)
- REFACTOR: Code cleaned up per linting rules

Changes:
- APQStorageBackend methods now accept optional context parameter
- Added extract_tenant_id() helper for context parsing
- Backward compatibility maintained (context defaults to None)
- All existing tests pass (5 passed, 1 expected fail for Phase 3)

Next: Phase 2 - Router context propagation
TDD Cycle Complete:
- RED: Tests expecting context propagation failed
- GREEN: Updated router and middleware to pass context
- REFACTOR: Code formatted and cleaned per linting rules
- QA: All integration tests pass

Changes:
- Router now passes context to APQ backend methods
- handle_apq_request_with_cache accepts and forwards context
- store_response_in_cache accepts and forwards context
- Backward compatibility maintained

Test results:
- New context propagation tests: 3 passing
- APQ middleware integration: 9 passing
- No regressions detected

Next: Phase 3 - Tenant-specific response caching
Complete implementation of tenant-aware APQ caching:

Phase 3: Tenant-Specific Caching
- TenantAwareMemoryBackend implementation
- Cache key generation with tenant isolation
- Comprehensive tests for tenant isolation
- Cache invalidation per tenant

Phase 4: Documentation & Examples
- Complete guide for APQ tenant context support
- Working multi-tenant example with statistics
- Security considerations and best practices
- Migration guide for existing applications

Features demonstrated:
- Tenant-specific response caching
- Prevention of cross-tenant data leakage
- Per-tenant cache invalidation
- Cache hit rate tracking per tenant
- Backward compatibility with non-tenant systems

The feature is complete and ready for production use!
- MemoryAPQBackend now uses tenant-aware cache keys
- PostgreSQLAPQBackend adds tenant_id column and composite keys
- No backward compatibility constraints - fresh implementation
- All tests updated and passing

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
- Removed redundant TenantAwareMemoryBackend from examples
- Cleaned up all phase comments from tests
- Updated documentation to reflect built-in tenant support
- Removed transitional comments from implementations
- Repository now reflects clean, intended design

The codebase now sparks joy ✨

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
@evoludigit evoludigit merged commit c9f340d into dev Sep 21, 2025
5 checks passed
@evoludigit evoludigit deleted the feature/apq-tenant-context-support branch September 21, 2025 21:27
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.

1 participant