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 |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
