| Scale | Users / Devices | Metrics Volume | Data Storage | Query Load | System Changes |
|---|---|---|---|---|---|
| Small (100 users) | 100 devices | ~10K metrics/min | Single DB instance | Low QPS | Simple ingestion, single server |
| Medium (10K users) | 10,000 devices | ~1M metrics/min | DB with read replicas | Moderate QPS | Introduce caching, load balancer |
| Large (1M users) | 1,000,000 devices | ~100M metrics/min | Sharded DB, distributed storage | High QPS | Partitioning, message queues, batch processing |
| Very Large (100M users) | 100,000,000 devices | ~10B metrics/min | Multi-region distributed storage | Very high QPS | Advanced sharding, edge processing, CDN for dashboards |
Metrics collection in HLD - Scalability & System Analysis
At small to medium scale, the database is the first bottleneck because it must handle many writes and queries per second. The ingestion pipeline can overwhelm the DB with high write QPS. As scale grows, network bandwidth and storage I/O also become bottlenecks.
- Horizontal scaling: Add more ingestion servers behind a load balancer to handle more incoming metrics.
- Caching: Use in-memory caches (e.g., Redis) to reduce read load on the database for frequent queries.
- Sharding: Partition data by device ID or time to distribute load across multiple database instances.
- Message queues: Use Kafka or similar to buffer and batch writes, smoothing spikes in traffic.
- Distributed storage: Use scalable time-series databases or cloud storage optimized for large write volumes.
- Edge processing: Aggregate or filter metrics near the source to reduce data volume sent to central servers.
- CDN for dashboards: Cache dashboard data closer to users to reduce backend query load.
- At 1M users sending 100 metrics/minute: 100 million metrics/minute ≈ 1.7 million writes/second.
- Storage needed: Assuming 200 bytes per metric, 1.7M writes/sec × 200 bytes ≈ 340 MB/s write throughput.
- Network bandwidth: 340 MB/s ≈ 2.7 Gbps, requiring multiple network interfaces or distributed ingestion.
- Database QPS: Single DB handles ~5K QPS, so need ~340 DB shards or use specialized time-series DB.
- Cost scales with number of servers, storage, and network infrastructure.
Start by clarifying the scale and data volume. Identify the main bottleneck (usually DB writes). Discuss incremental scaling steps: caching, sharding, message queues. Mention trade-offs like consistency vs latency. Show awareness of cost and complexity.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Introduce read replicas and caching to reduce DB load, and implement message queues to batch writes. Consider sharding data to distribute load across multiple DB instances.
Practice
COUNT, SUM, and AVG in metrics collection?Solution
Step 1: Understand aggregate functions
Aggregate functions like COUNT, SUM, and AVG are used to calculate summary values from multiple rows of data.Step 2: Identify their purpose in metrics
These functions help get totals, averages, and counts which are useful for metrics and reports.Final Answer:
To calculate totals, averages, and counts from data -> Option CQuick Check:
Aggregate functions = summary numbers [OK]
- Thinking aggregate functions modify data structure
- Confusing aggregate functions with data deletion
- Believing aggregate functions create tables
Orders?Solution
Step 1: Recall correct COUNT syntax
The correct syntax to count rows isCOUNT(*)inside SELECT.Step 2: Check each option
SELECT COUNT(*) FROM Orders; usesCOUNT(*)correctly. Others have syntax errors or wrong functions.Final Answer:
SELECT COUNT(*) FROM Orders; -> Option DQuick Check:
COUNT(*) counts rows correctly [OK]
- Omitting parentheses after COUNT
- Using COUNT without specifying columns or *
- Confusing SUM with COUNT
Sales with columns amount and region, what will this query return?SELECT AVG(amount) FROM Sales WHERE region = 'East';
Solution
Step 1: Understand the WHERE clause effect
The WHERE clause filters rows to only those where region is 'East'.Step 2: Understand AVG function
AVG calculates the average of the filteredamountvalues.Final Answer:
The average sales amount for the East region -> Option AQuick Check:
AVG with WHERE filters = average for filtered data [OK]
- Thinking AVG returns total sum
- Confusing COUNT with AVG
- Believing WHERE cannot be used with aggregates
SELECT SUM(amount) FROM Sales WHERE region == 'West';
Solution
Step 1: Check WHERE clause syntax
SQL uses single equals (=) for comparison, not double equals (==).Step 2: Confirm aggregate and WHERE usage
SUM works with WHERE filtering; GROUP BY is not needed here.Final Answer:
Using double equals (==) instead of single equals (=) in WHERE clause -> Option AQuick Check:
Use = for comparisons in SQL WHERE [OK]
- Using == instead of = in WHERE
- Thinking GROUP BY is always required with SUM
- Assuming SUM cannot be used with WHERE
Solution
Step 1: Understand grouping by customer
To get average per customer, group rows bycustomer_id.Step 2: Filter groups with more than 5 orders
Use HAVING withCOUNT(*) > 5to keep only customers with more than 5 orders.Step 3: Select average order amount
UseAVG(amount)in SELECT to get average per filtered customer group.Final Answer:
Use GROUP BY customer_id with HAVING COUNT(*) > 5 and SELECT AVG(amount) -> Option BQuick Check:
GROUP BY + HAVING filters groups before AVG [OK]
- Using WHERE to filter aggregated counts
- Forgetting GROUP BY when using aggregates per group
- Using HAVING without GROUP BY
Ready to prove it?
Pass the test and this pattern moves unknown
