# Story 6.6: Performance Optimization

## Metadata
- **Story ID**: 6.6
- **Title**: Performance Optimization
- **Epic**: 6 - Observability & Production Readiness
- **Status**: Pending
- **Priority**: Medium
- **Estimated Time**: 6-8 hours
- **Dependencies**: 1.2, 5.1

## Goal
Optimize platform performance through database connection pooling, query optimization, response compression, and caching strategies.

## Description
This story implements performance optimizations including database connection pooling, query optimization, response compression, and strategic caching.

## Deliverables

### 1. Database Connection Pooling
- Configure max connections
- Configure idle timeout
- Monitor pool stats
- Connection health checks

### 2. Query Optimization
- Add indexes for common queries
- Use database query logging (development)
- Add slow query detection
- Query performance monitoring

### 3. Response Compression
- Gzip middleware for large responses
- Configurable compression levels
- Content type filtering

### 4. Caching Strategy
- Cache frequently accessed data (user permissions, roles)
- Cache invalidation strategies
- Cache warming

## Acceptance Criteria
- [ ] Database connection pooling is optimized
- [ ] Query performance is improved
- [ ] Response compression works
- [ ] Caching strategy is effective
- [ ] Performance meets SLA (< 100ms p95 for auth endpoints)

## Files to Create/Modify
- `internal/infra/database/client.go` - Connection pooling
- `internal/server/middleware.go` - Compression middleware
- `internal/perm/in_memory_resolver.go` - Add caching

