KatInterview

Design a High-Throughput Event Aggregation System

System DesignModel answer
HardModel answerSystem Designdistributed-systemsstream-processingscalability

Last updated

Build a distributed system for counting events by user, device, and region with real-time aggregation, deduplication, and time-windowed queries.

You need to architect a distributed service that tracks and aggregates application events across multiple dimensions. The system must handle events from users, devices, and geographic regions, providing both real-time and historical analytics.

Core Requirements

Your system should support:

  1. Writing operations: Accept event records with metadata (user_id, device_id, region, activity_type, timestamp)
  2. Reading operations:
    • Retrieve total counts for any dimension combination
    • Query counts over specific time ranges (fixed intervals or rolling windows)
    • Estimate distinct entities (e.g., unique users per region)
  3. Offline behavior: Handle clients that buffer events locally for extended periods before syncing

Technical Constraints

  • Write volume: ~10 billion events daily with 5× peak-to-average ratio
  • Read freshness: Results should reflect recent writes within seconds
  • Read performance: Sub-50ms p99 latency
  • Duplicate handling: Retried events must not inflate counts
  • Retention: Keep raw data for 30 days, aggregates longer
  • Approximation: Unique counts may use probabilistic algorithms with <2% error

What to Cover

Design the complete system architecture including:

  • API contracts for ingestion and retrieval
  • Storage layer selection and data modeling
  • Deduplication strategy across retries and network failures
  • Time-based aggregation approaches (both fixed and sliding windows)
  • Handling high-cardinality dimensions and traffic hotspots
  • Client-side buffering and synchronization logic
  • Capacity planning with concrete numbers
  • Failure scenarios and recovery mechanisms
  • Monitoring and operational considerations

Consider trade-offs between consistency guarantees, latency, cost, and complexity.

Hints

Hint 1

Think about how clients can reliably identify events across retries without server-assigned IDs

Hint 2

Consider separating the write path (optimized for throughput) from aggregation (optimized for query patterns)

Hint 3

Hot dimensions like popular regions will need special handling to avoid bottlenecks