Analytics Service ๐
The Analytics Service is responsible for collecting, processing, and exposing business metrics in ShopVerse using an event-driven analytics pipeline.
It provides near real-time insights without impacting core transactional services.
๐ฏ Responsibilitiesโ
The Analytics Service handles:
- Consuming domain events from Kafka
- Processing events using stream processing
- Storing analytics data in ClickHouse
- Exposing aggregated metrics via APIs
- Supporting business dashboards and reporting
๐ง Why a Separate Analytics Service?โ
Analytics workloads are:
- Read-heavy
- Aggregation-focused
- Computationally expensive
Separating analytics ensures:
- No performance impact on transactional services
- Independent scaling
- Technology freedom (OLAP databases)
- Clean separation between operations and insights
๐๏ธ High-Level Architectureโ
๐ก Event Ingestion Flowโ
๐ฆ Events Consumedโ
| Event Type | Source Service | Purpose |
|---|---|---|
| USER_REGISTERED | Auth Service | User growth |
| ORDER_PLACED | Order Service | Order metrics |
| PAYMENT_SUCCESS | Payment Service | Revenue tracking |
| PRODUCT_CREATED | Product Service | Catalog insights |
All events follow a standard analytics schema.
๐งฉ Analytics Event Modelโ
{
"eventType": "PAYMENT_SUCCESS",
"service": "PAYMENT_SERVICE",
"userEmail": "user@example.com",
"entityId": "ORD123",
"amount": 2499.00,
"timestamp": "2026-01-21T11:45:00",
"metadata": {
"paymentMode": "UPI"
}
}
๐๏ธ Data Storage (ClickHouse)โ
ClickHouse is used because:
- Columnar storage
- Extremely fast aggregations
- High write throughput
- Ideal for OLAP workloads
Example Metrics Stored:โ
- Total users
- Orders per day
- Revenue per day
- Revenue by payment method
- Service-level event counts
๐ Analytics APIsโ
| Method | Endpoint | Role | Description |
|---|---|---|---|
| GET | /api/analytics/dashboard | ADMIN | Get dashboard metrics |
| GET | /api/analytics/revenue | ADMIN | Revenue trends |
| GET | /api/analytics/orders | ADMIN | Order statistics |
These APIs are read-only.
๐ Security Modelโ
-
All APIs accessed via API Gateway
-
Only
ADMINrole allowed -
Identity verified using:
X-User-EmailX-User-Role
Analytics data is never exposed to customers.
๐ Stream Processing Strategyโ
Apache Flink handles:
- Event ingestion
- Transformation
- Aggregation
- Windowing (daily, hourly metrics)
Benefits:โ
- Near real-time analytics
- Fault tolerance
- Exactly-once semantics
- Horizontal scalability
๐ก๏ธ Reliability & Fault Toleranceโ
- Kafka retains events on failure
- Flink checkpoints state
- ClickHouse ensures data durability
- Queries are idempotent
- No data loss during restarts
โ ๏ธ Failure Scenariosโ
โ Kafka Lagโ
- Analytics delayed
- Core services unaffected
โ Flink Job Failureโ
- Job restarts from checkpoint
โ ClickHouse Downโ
- Events buffered until recovery
โ๏ธ Key Componentsโ
AnalyticsConsumerโ Kafka consumer / Flink jobAnalyticsEventโ Standard event modelAnalyticsQueryServiceโ ClickHouse queriesAnalyticsControllerโ REST APIsJdbcTemplateโ ClickHouse integrationGatewayHeaderAuthenticationFilterโ Security
๐ Scalability Considerationsโ
- Kafka partitions enable parallel ingestion
- Flink scales horizontally
- ClickHouse supports distributed clusters
- Analytics Service APIs are stateless
๐งช Testing Strategyโ
- Unit tests for query logic
- Mock Kafka events for Flink jobs
- Validation of aggregations
- Security tests for admin-only access
๐ Summaryโ
The Analytics Service provides:
- Real-time business insights
- Zero impact on core workflows
- Scalable stream processing
- Production-grade analytics architecture
It turns ShopVerse operational data into actionable intelligence.