Skip to Content
Course content

227: Chaos Testing Concepts for Scala Services

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

Think about a high-rise office building's fire drill. We don't wait for an actual five-alarm fire to find out if the emergency exits are blocked by cardboard boxes or if the alarm system is too quiet to wake someone up in the basement. We trigger a fake alarm, force everyone out, and watch where the bottlenecks happen. It's annoying for the employees, sure, but it's a hell of a lot better than finding out the fire escape is rusted shut while the building is actually burning.

Chaos testing is exactly that, but for your Scala services. Instead of hoping your CircuitBreaker works, we intentionally trip it. Instead of assuming your ZIO or Cats Effect fibers will handle a timeout gracefully, we inject a 30-second hang into a critical dependency. Here is how that fire drill maps to our code:

  • The Fire Alarm: This is your Fault Injection. It's the intentional act of killing a Kubernetes pod, dropping network packets, or forcing a Scala service to throw a RuntimeException.
  • The Evacuation Plan: These are your Resilience Patterns. This is your retry logic, your fallback methods, and your circuit breakers. If the "alarm" goes off, does the system shift traffic to a backup or return a cached response?
  • The Fire Marshal: This is your Observability. If you inject a failure but your Grafana dashboard stays green and your logs are silent, you haven't passed the test—you've just discovered that you're blind to your own failures.

Injecting Failure into Functional Pipelines

In a Scala service, especially one using a functional stack, we often wrap our side effects in an effect type. This actually makes chaos testing easier because we can inject failure at the "edge" of our business logic. I've seen too many teams try to test chaos by manually unplugging servers; while that's great for infrastructure, it doesn't tell you if your Option or Either handling is actually robust.

case class PaymentRequest(amount: BigDecimal, currency: String)
case class PaymentResponse(transactionId: String)

trait PaymentGateway {
  def process(req: PaymentRequest): IO[PaymentResponse]
}

// This is our "Chaos Wrapper"
class ChaosPaymentGateway(delegate: PaymentGateway) extends PaymentGateway {
  override def process(req: PaymentRequest): IO[PaymentResponse] = {
    val diceRoll = Random.nextInt(100)
    if (diceRoll < 15) { 
      // 15% chance of a simulated "Black Hole" (infinite hang)
      IO.sleep(Duration.ofMinutes(10)).as(PaymentResponse("timeout"))
    } else if (diceRoll < 30) {
      // 15% chance of a hard crash
      IO.raiseError(new RuntimeException("Chaos Monkey: Network Partition!"))
    } else {
      delegate.process(req)
    }
  }
}

By wrapping the real gateway in a chaos decorator, I can simulate a flaky third-party API without actually needing the third party to fail. I can run this in a staging environment and see if my service's timeout configuration is actually shorter than the gateway's hang time. If it's not, my fibers will pile up, memory will leak, and the whole service will crash—which is exactly what I want to find out now rather than at 3 AM on a Tuesday.

Defining the Blast Radius

The biggest fear people have with chaos testing is that they'll accidentally take down the entire production environment. We call this the "Blast Radius." In a Scala microservice architecture, you manage this by targeting specific Cores or Shards.

If you're using Akka or Pekko, you might target a specific actor system or a specific cluster node. The goal isn't to destroy everything; it's to ensure that if one instance of your "Shipping Service" goes haywire, the "Order Service" doesn't enter a death spiral because it's waiting on a response that will never come. You want to see a "graceful degradation"—maybe the user sees a message saying "Shipping estimates are currently unavailable," but they can still actually place the order.

The Correlation between Chaos and SLOs

I'll be honest: chaos testing without a defined Service Level Objective (SLO) is just breaking things for fun. To make this professional, you need a hypothesis. A bad hypothesis is: "I wonder what happens if I kill this pod." A good hypothesis is: "If 20% of the Payment Gateway requests time out, our overall checkout success rate should not drop by more than 5% because the fallback cache should kick in."

When you run your chaos experiment, you monitor that specific metric. If the success rate drops by 20%, your "fire drill" just proved that your evacuation plan is broken. You then fix the code, update the circuit breaker thresholds, and run the drill again.




📋 Practical Task

Building a Resilient Circuit Breaker with Chaos Injection

You have a service that fetches user profiles from a remote legacy API. Currently, if the legacy API becomes slow, your service hangs and eventually crashes due to thread exhaustion. Your task is to implement a resilience layer and verify it using a chaos-injection strategy.

Requirements:

  • Create a ProfileService that calls a RemoteProfileApi.
  • Implement a CircuitBreaker (you can use resilience4j or a custom implementation) that opens after 3 consecutive failures or timeouts.
  • Create a ChaosProfileApi wrapper that randomly introduces a 5-second delay (latency) or throws a ConnectException.
  • Write a test loop that sends 50 requests to the service.
  • The Goal: Demonstrate that once the ChaosProfileApi starts failing, the ProfileService stops calling the API entirely for a period (the circuit is "Open") and immediately returns a cached "Guest Profile" instead of waiting for the timeout.

Success Criteria: Your logs should show a transition from CALLING_API → API_FAILURE → CIRCUIT_OPEN_FALLBACK. The total execution time for the 50 requests should be significantly lower than (50 requests * 5 seconds) because the circuit breaker should be short-circuiting the slow calls.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.