Performance Tuning & Optimization Guide
This guide details database indexing, Celery worker concurrency, Redis channel tuning, and caching strategies for MetaPilot.
1. Database Indexing & Query Optimizations
Critical Database Indexes
- : Fast JWT login lookup.
users_user.email - : Instant phone number matching on incoming webhooks.
messaging_conversation.customer_phone - : Immediate status updates for incoming receipts (
messaging_message.wamid,delivered).read - : Rapid key retrieval during broadcast execution.
tenants_tenantconfig.tenant_id
Query Tuning Rules
- Always use and
select_relatedin ViewSets to prevent N+1 query overhead:prefetch_relatedpythonqueryset = Message.objects.select_related('conversation', 'conversation__tenant').all()
2. Celery Worker Concurrency & Batch Sizing
- Concurrency: Set per worker container (or 2x CPU cores).
--concurrency=4 - Batch Sizing: (optimal for Meta Graph API REST throughput without triggering memory overhead).
SCHEDULER_BATCH_SIZE = 20 - Rate Limit Token Bucket: Enforce 50 msgs/sec in .
rate_limiter.py
3. Redis Channel Layer Optimization
- Capacity Tuning: Set in
capacity=1500setting to handle peak WebSocket frame spikes.CHANNEL_LAYERS - Message Expiry: Set seconds to automatically purge stale disconnected WebSocket frames.
expiry=60