API / Microservices Design Patterns Interview Questions
What is the Application Metrics pattern and what is the difference between push and pull metric collection?
The Application Metrics pattern instruments each service to emit numeric measurements — counters, gauges, histograms, and summaries — that describe its runtime behaviour. These metrics feed dashboards, alerting rules, and capacity-planning models that plain logs cannot efficiently support (logs are for discrete events; metrics are for continuous numerical trends).
Common metric types:
- Counter — monotonically increasing (e.g., total HTTP requests served, total errors). Never decremented except on process restart.
- Gauge — a value that goes up and down (e.g., current active connections, JVM heap used, queue depth).
- Histogram — distributes observations into configurable buckets (e.g., request latency distribution, enabling P50/P95/P99 calculations).
Pull model (Prometheus): The Prometheus server periodically scrapes a /metrics HTTP endpoint on each service instance. The service maintains in-memory metric state; Prometheus pulls it on its own schedule.
// Micrometer / Prometheus metric registration (Java) Counter httpRequests = Counter.builder("http_requests_total") .tag("method", "GET").tag("status", "200") .register(Metrics.globalRegistry); httpRequests.increment(); // Prometheus scrapes GET /actuator/prometheus every 15s
Push model (StatsD, Prometheus Pushgateway): The service actively sends metric updates to a collection agent or gateway. Used when services are short-lived (batch jobs, serverless functions) that do not run long enough to be scraped.
| Aspect | Pull (Prometheus) | Push (StatsD / Pushgateway) |
|---|---|---|
| Discovery | Prometheus discovers targets via service discovery | Service knows the collector address |
| Short-lived jobs | Poor fit — job may finish before being scraped | Good fit — pushes before exit |
| Load on service | Scrape adds a momentary HTTP request | Service bears cost of every metric push |
More Related questions...