System Design FAQ: Top Questions
30. How would you design an API Rate Analytics System?
An API Rate Analytics System tracks the number of API calls made by clients, categorized by IP, API key, endpoint, and time. It's used for monitoring usage, billing, throttling, and security.
๐ Functional Requirements
- Track total and per-endpoint API usage
- Aggregate requests by time window (second, minute, hour)
- Generate usage dashboards and alerts
- Expose internal APIs to query usage
๐ฆ Non-Functional Requirements
- High write throughput with real-time visibility
- Low-latency reads for dashboards
- Durability and fault-tolerance
๐๏ธ Core Components
- Ingestion Layer: API gateway emits logs to Kafka
- Processor: Aggregates metrics using Flink or Spark
- Storage: Time-series DB like InfluxDB or ClickHouse
- Query API: Backend to serve analytics endpoints
๐ค NGINX Log Format (for ingestion)
log_format apilog '$remote_addr - $http_api_key [$time_iso8601] '
'"$request" $status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
access_log /var/log/nginx/api.log apilog;
๐ Sample Aggregated Metric (InfluxDB)
measurement = "api_usage"
tags = {
api_key = "abc123",
endpoint = "/v1/data",
status = "200"
}
fields = {
count = 1
}
timestamp = 2025-06-11T12:00:00Z
๐งช Query Example: Total Requests by Endpoint
SELECT endpoint, SUM(count) AS total_hits
FROM api_usage
WHERE time > now() - interval 1h
GROUP BY endpoint
ORDER BY total_hits DESC;
๐ Dashboard Panels
- Requests per endpoint (bar/line chart)
- Top 10 API keys by usage
- Error rate % by status code (pie chart)
- Usage heatmap by time of day
๐ Alert Rules (Grafana/Prometheus)
ALERT HighErrorRate
IF rate(api_usage_errors_total[5m]) / rate(api_usage_total[5m]) > 0.05
FOR 1m
LABELS { severity = "critical" }
ANNOTATIONS {
summary = "High API error rate detected",
description = "More than 5% error rate over the last 5 minutes"
}
๐งฐ Tools/Infra Used
- Data Pipeline: Kafka โ Flink โ InfluxDB
- Analytics: Grafana, Redash
- API Monitoring: Prometheus + Alertmanager
๐ Final Insight
API analytics enables real-time monitoring, billing, and defense against abuse. Efficient event aggregation and time-series storage are key. Dashboards and alerts make usage patterns actionable.
