Architecture Overview
Understanding how Bklit Analytics works
Architecture Overview
This document provides an overview of Bklit's architecture and how components work together.
System Architecture
Bklit is built as a modern, scalable analytics platform with the following architecture:
Frontend Applications
- Dashboard - Next.js app for viewing analytics
- Website - Marketing site
- Playground - SDK testing app
- Docs - Documentation site
Backend Services
- API Layer - tRPC for type-safe APIs
- Analytics Service - ClickHouse for analytics data
- Database - PostgreSQL for application data
- Auth Service - Better Auth for authentication
Data Flow
- SDK connects to WebSocket server (wss://bklit.ws)
- WebSocket Server validates tokens and enriches with geolocation
- Events queued in Redis for reliable processing
- Worker processes queue in batches and stores in ClickHouse
- WebSocket broadcasts events instantly to connected dashboards
- Dashboard receives real-time updates (under 1 second latency)
Technology Stack
Frontend
- Next.js 15 (App Router)
- React 19
- TypeScript
- Tailwind CSS v4
- shadcn/ui components
Backend
- tRPC for APIs
- Prisma for database
- Better Auth for authentication
- ClickHouse for analytics
Infrastructure
- PostgreSQL database
- ClickHouse for analytics
- Resend for email
- Polar.sh for billing
Monorepo Structure
See Monorepo Structure for detailed structure.
Data Storage
PostgreSQL
Stores:
- Users and authentication
- Organizations and projects
- API tokens
- Event definitions
- Funnels
ClickHouse
Stores:
- Page view events
- Custom events
- Session data
- Analytics aggregates
Authentication Flow
- User signs in with GitHub/Google
- Better Auth handles OAuth
- Session created and stored
- Protected routes check session
- tRPC procedures verify authentication
Analytics Flow (WebSocket Architecture)
Bklit uses WebSocket architecture for instant real-time analytics:
Event Tracking
- SDK establishes WebSocket connection to wss://bklit.ws
- Events sent over persistent connection (pageviews, custom events)
- Token validation with Redis caching
- IP geolocation via ip-api.com
- Events queued in Redis for processing
- Instant broadcast to connected dashboards
Background Processing
- Worker polls Redis queue every 1 second
- Pops up to 100 events in batch
- Looks up EventDefinition UUIDs from Postgres (cached)
- Creates/updates sessions in ClickHouse
- Batch inserts events to ClickHouse
- Publishes to Redis pub/sub for real-time updates
Real-time Updates
- Worker publishes processed events to Redis
- WebSocket server subscribes to Redis channel
- Broadcasts to connected dashboard clients
- Dashboard UI updates instantly
Benefits
- 100x faster ClickHouse writes (batch vs single inserts)
- Zero data loss - Redis queue with AOF persistence
- Scalable - Add more workers for higher throughput
- Observable - /terminal UI shows complete pipeline
Billing Flow
- User upgrades via Polar checkout
- Polar webhook received
- Organization plan updated
- Features enabled/disabled
- Usage limits adjusted
Related Documentation
- Monorepo Structure - Project structure
- Getting Started - Setup guide