Skip to Content
Course content

213: Alerting Strategies for Scala Production Services

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're running a high-end professional kitchen. You've got a dozen things happening at once: sauces reducing, steaks searing, and orders flying in. If you had a buzzer that went off every time a single drop of water splashed on the floor, you'd lose your mind within ten minutes. You'd eventually just turn the buzzer off and ignore it—even when the actual grease fire starts in the back. But if the oven temperature drops below 300 degrees or the smoke detector screams, that's when you stop everything and react.

Production alerting in Scala is exactly the same. Most engineers make the mistake of building "splash" alerts—notifying them every time a single java.lang.NullPointerException hits the logs. In a high-throughput system, that's just noise. To build a service that doesn't burn the house down (or keep you awake at 3 AM for no reason), you need to distinguish between telemetry and alerting.

The Noise Problem: Why Your Pager Shouldn't Scream

I've seen too many Scala services where every logger.error triggers a Slack notification. This is the fastest way to create "alert fatigue." When everything is an emergency, nothing is. In a functional Scala stack—whether you're using ZIO, Cats Effect, or just plain Akka—you have to move away from "event-based" alerting (something happened!) to "symptom-based" alerting (the system is unhealthy!).

Instead of alerting on a single failed database query, you alert on the error rate. If 0.1% of requests fail, it's a blip. If 5% fail over a rolling 5-minute window, you have a problem. That's a symptom.

Defining What Actually Matters with SLIs

To do this right, you need Service Level Indicators (SLIs). Think of these as the "vital signs" of your Scala app. For most production services, I focus on the "Golden Signals": Latency, Traffic, Errors, and Saturation.

  • Latency: Not the average, but the p99. If the 99th percentile of your PaymentRequest handler jumps from 200ms to 2s, your users are feeling it, even if the average looks fine.
  • Traffic: A sudden drop to zero requests per second usually means your load balancer is dead, not that your users suddenly stopped liking your product.
  • Errors: The rate of 5xx responses relative to total requests.
  • Saturation: JVM Heap usage or thread pool exhaustion. If your ExecutionContext is saturated, your Scala app will stop responding long before the CPU hits 100%.

Integrating Metrics into Your Scala Effects

You can't alert on what you don't measure. In the Scala ecosystem, I usually lean toward Micrometer or Kamon because they play well with Prometheus. The trick is to wrap your business logic in a way that doesn't clutter your domain code.

Here is how I typically handle a critical path, like a payment gateway call, using a simple wrapper pattern to track success and failure rates:

import io.micrometer.core.instrument.Metrics
import io.micrometer.core.instrument.Timer

object PaymentMetrics {
  private val paymentTimer = Timer.builder("payment.processing.time")
    .description("Time taken to process payment")
    .register(Metrics.globalRegistry)

  private val failureCounter = Metrics.counter("payment.failures.total", "type", "gateway_timeout")

  def recordPayment[A](block: => A): A = {
    val sample = Timer.start(Metrics.globalRegistry)
    try {
      val result = block
      sample.stop(paymentTimer)
      result
    } catch {
      case e: Exception => 
        failureCounter.increment()
        throw e
    }
  }
}

// Usage in your service
def processPayment(req: PaymentRequest): Task[PaymentResponse] = {
  Task.delay {
    PaymentMetrics.recordPayment {
      gateway.submit(req)
    }
  }
}

By doing this, you aren't just logging "Payment failed"; you're incrementing a counter that Prometheus can scrape. Your alert then becomes a PromQL query: rate(payment_failures_total[5m]) / rate(payment_processing_time_count[5m]) > 0.05. Translation: "Wake me up if the failure rate exceeds 5% over the last five minutes."

The Art of the Actionable Alert

Finally, an alert without a playbook is just a nuisance. When your pager goes off at 2 AM, you are not in the mental state to perform a forensic investigation of the codebase. Every alert should link to a Runbook.

If the alert is "JVM Heap Saturation High," the alert notification should include a link to a document that says:

  • Check the GC logs in Kibana.
  • Verify if the cache-size config was recently changed.
  • If all else fails, perform a rolling restart of the cluster.

I've found that spending an extra hour writing that documentation saves me ten hours of stress during an incident. Be kind to your future, sleep-deprived self.




📋 Practical Task

Building a Rate-Based Alerting Wrapper for a User Service

You are managing a UserService that fetches user profiles from a remote legacy API. This API is flaky; it often throws TimeoutException. You've been told that a few timeouts are normal, but if the failure rate exceeds 10% over a window, the system is considered "Degraded."

Your Task: Implement a MetricWrapper object in Scala that tracks both the total number of requests and the number of failures for the fetchUser method.

Requirements:

  • Use a mock Registry (you can simulate this with two AtomicLong counters: totalRequests and failedRequests).
  • Create a method trackFetch[T](name: String)(block: => T): T that increments the total count on every call and the failure count if an exception is thrown.
  • Implement a method calculateErrorRate: Double that returns the current error percentage (failures / total).
  • Write a small test script that simulates 100 calls to the fetchUser method, where 15 of them throw an exception, and print whether the "Alert" should be triggered (Error Rate > 0.10).
Rating
0 0

There are no comments for now.

to be the first to leave a comment.