Technology Stack
PostHog’s core architecture is built on three main technologies:Django Backend
The backend is built on Django, providing:
- RESTful APIs using Django REST Framework (DRF)
- PostgreSQL for application data and metadata
- Celery for background task processing
- Business logic and data orchestration
React Frontend
The frontend uses a modern React stack:
- TypeScript for type safety
- Kea for state management
- Tailwind CSS for styling
- Vite for fast development builds
High-Level Architecture
Key Components
API Layer
PostHog’s API layer is built with Django REST Framework:- Views: Handle HTTP requests and responses
- Serializers: Define request/response schemas and validation
- Authentication: Team-based access control
- Type Generation: Automatic TypeScript types via OpenAPI
Django serializers are the source of truth for API types. Run
hogli build:openapi to generate TypeScript types for the frontend.Query Engine
The query engine translates user requests into optimized database queries:- HogQL: SQL-like query language for ClickHouse
- Query Runners: Execute and optimize queries (
posthog/queries/) - Query Nodes: AST-based query composition
- Caching: Redis-based result caching
Background Processing
Celery handles asynchronous tasks:- Event ingestion: Process incoming analytics events
- Scheduled jobs: Periodic data aggregation and cleanup
- Exports: Generate and send data exports
- Webhooks: Trigger external integrations
Data Storage
PostgreSQL
PostgreSQL
Stores application metadata:
- User accounts and teams
- Feature flag definitions
- Dashboard configurations
- Cohort definitions
ClickHouse
ClickHouse
Stores high-volume event data:
- Product analytics events
- Session recordings metadata
- Person and group properties
- Time-series metrics
Object Storage (S3)
Object Storage (S3)
Stores large binary data:
- Session recording blobs
- Export files
- Upload artifacts
Deployment Architecture
PostHog Cloud
PostHog Cloud runs on Kubernetes with:- Multi-region deployment: US and EU data residency
- Horizontal scaling: Auto-scaling based on load
- Managed ClickHouse: Dedicated clusters per customer tier
- CDN: Global edge network for SDK delivery
Self-Hosted
For self-hosted deployments:- Docker Compose: Single-server hobby deployments
- Resource requirements: Minimum 4GB RAM, 2 CPU cores
- Scale limits: ~100k events/month recommended
PostHog has sunset support for self-hosted Kubernetes deployments. We recommend PostHog Cloud for production use.
Data Flow
- Event Capture: Client SDKs send events to capture endpoints
- Ingestion: Events are validated, enriched, and queued
- Processing: Background workers process and store events
- Storage: Events land in ClickHouse, metadata in PostgreSQL
- Querying: Frontend requests data via Django APIs
- Response: Data is queried, aggregated, and returned to UI
Security Architecture
- Team isolation: All queries are scoped to
team_id - SQL injection protection: Parameterized queries and HogQL parsing
- Authentication: Session-based auth with CSRF protection
- Data encryption: TLS in transit, encryption at rest for Cloud
- Access control: Role-based permissions per team
Next Steps
Monorepo Structure
Learn how the codebase is organized
Product Architecture
Understand the product pattern
Data Model
Explore data structures and schemas
Development Setup
Set up your local environment