Overview
Permission Mongo is optimized for 50K+ QPS workloads through careful tuning of connection pools, caching strategies, and lock-free data structures. This guide covers performance optimization techniques and benchmarking.Connection Pooling
MongoDB Connection Pool
The MongoDB driver maintains a connection pool for efficient resource utilization:| Parameter | Default | Description |
|---|---|---|
MaxPoolSize | 100 | Maximum concurrent connections |
MinPoolSize | 10 | Minimum idle connections |
ServerSelectionTimeout | 5s | Timeout for server selection |
ConnectTimeout | 10s | Initial connection timeout |
- High read workload: Increase
MaxPoolSizeto 200-300 - Low latency requirement: Keep
MinPoolSizehigh (50-100) to avoid connection warm-up - Distributed deployment: Set
ServerSelectionTimeoutto 2-3s for faster failover
Redis Connection Pool
Redis caching layer is configured for high throughput:- 50K+ QPS: Use
PoolSize: 500-1000 - Low latency: Set
ReadTimeoutandWriteTimeoutto 500ms - High write volume: Increase
WriteTimeoutto 2-3s
Lock-Free Optimizations
AST Caching with sync.Map
RBAC expression compilation uses a thread-safe cache to avoid re-parsing:- Zero lock contention for reads
- Amortized parsing cost across requests
- Memory efficient - caches only unique expressions
Atomic Operations for Metrics
Prometheus metrics use atomic counters for zero-overhead instrumentation:Async Audit Logging
Audit logs are batched and written asynchronously to avoid blocking request paths: Architecture:- High throughput: Increase
batch_sizeto 500-1000 - Low latency requirement: Reduce
flush_intervalto 1-2s - Memory constrained: Reduce
buffer_sizeto 5000
HTTP Server Tuning
Permission Mongo uses optimized HTTP timeouts:Benchmarks
Throughput Targets
| Metric | Target | Notes |
|---|---|---|
| QPS | 50,000+ | With caching enabled |
| P50 Latency | Less than 5ms | Cache hit scenario |
| P99 Latency | Less than 50ms | Including RBAC evaluation |
| Cache Hit Rate | Greater than 90% | Policy and hierarchy |
Load Testing
Setup:Monitoring Performance
Key Metrics
Track these Prometheus metrics:Grafana Dashboard
Included dashboard panels:- HTTP Request Rate - Requests/sec by endpoint
- Cache Hit Ratio - Policy, hierarchy, schema cache effectiveness
- MongoDB Pool Usage - Active connections vs max pool size
- RBAC Evaluation Time - Expression compilation and evaluation latency
- Audit Log Queue Depth - Async batch writer backlog
Performance Checklist
Enable Redis caching for policies and hierarchy
Tune MongoDB pool size based on workload (100-300 connections)
Configure Redis pool for high concurrency (500-1000 connections)
Enable async audit logging with batching
Set appropriate HTTP timeouts (read/write: 30s)
Monitor cache hit ratios (target >90%)
Create MongoDB indexes on frequently queried fields
Use connection keep-alive (MinPoolSize) for consistent latency
Troubleshooting
High Latency
Symptoms: P99 latency >100ms Solutions:- Check cache hit rate - should be >90%
- Verify MongoDB indexes exist on query fields
- Increase connection pool sizes
- Reduce RBAC expression complexity
Connection Pool Exhaustion
Symptoms: Timeout errors, connection refused Solutions:Memory Pressure
Symptoms: High memory usage, OOM errors Solutions:- Reduce audit log buffer size
- Clear AST cache on policy reload
- Limit batch sizes for bulk operations
Next Steps
Caching Strategy
Learn about Redis caching patterns
Expression Language
Optimize RBAC expressions for performance