Skip to Content
Course content

147: The Reactive Manifesto Principles

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

I want to show you something that usually happens when developers first try to scale a system. We've spent a lot of time talking about types and concurrency in Scala, but today I want to step back and look at the philosophy of how we build systems that don't fall over when things get weird. We're going to look at the Reactive Manifesto, but instead of reading a PDF, let's actually break a small order-processing system and see why these principles exist.

The Blocking Nightmare

Let's start with a naive implementation of an Order Service. I've got a service that takes an order and calls a third-party payment gateway. It looks clean, right? Just a simple function call.

case class Order(id: String, amount: Double)

class OrderService(paymentGateway: PaymentGateway) {
  def processOrder(order: Order): String = {
    println(s"Processing order ${order.id}...")
    // This call is synchronous and blocks the thread
    val status = paymentGateway.charge(order.amount) 
    s"Order ${order.id} result: $status"
  }
}

I tried running this under a load test. Everything was fine until the payment gateway started experiencing latency—maybe it took 5 seconds instead of 200ms. Suddenly, my entire application stopped responding. Why? Because every single request thread was stuck waiting for the gateway. My system wasn't Responsive. In the Reactive world, responsiveness is the primary goal. If the system can't respond in a timely manner, it's effectively down, regardless of whether the process is still "running."

Moving to a Message-Driven Model

To fix this, I realized I couldn't let the request thread be held hostage. I shifted to a message-driven approach using Akka (or Pekko). Instead of calling a function and waiting, I'll send a message to a PaymentActor and let it handle the work asynchronously.

case class ProcessPayment(order: Order, replyTo: ActorRef)

class PaymentActor extends Actor {
  def receive = {
    case ProcessPayment(order, replyTo) =>
      // We simulate the gateway call here
      val result = paymentGateway.charge(order.amount)
      replyTo ! s"Payment successful for ${order.id}"
  }
}

Now, the OrderService just fires a message and moves on. The system is responsive again. But then I noticed something scary. I manually killed the PaymentActor (simulating a crash) while a few orders were in flight. Those orders just... vanished. The system didn't know they failed; they just stopped existing. This is where we hit the wall of Resilience.

Containing the Blast Radius

A resilient system doesn't mean a system that never fails—that's impossible. It means a system that handles failure gracefully. I adjusted my architecture to use a "Supervisor" pattern. Instead of the PaymentActor being a lone wolf, it's now a child of a Manager actor.

class OrderManager extends Actor {
  // Define a supervision strategy: if the child crashes, restart it
  override val supervisorStrategy = OneForOneStrategy() {
    case _: Exception => Restart
  }

  val paymentWorker = context.actorOf(Props[PaymentActor], "payment-worker")

  def receive = {
    case order => paymentWorker ! ProcessPayment(order, sender())
  }
}

By isolating the failure to the PaymentActor and having a supervisor that knows how to restart it, the "blast radius" of the crash is contained. The rest of the system keeps humming along. We've moved from "fragile" to "resilient."

The Black Friday Problem

So, we're responsive and resilient. I felt confident until I simulated a "Black Friday" spike—100x the normal traffic. My single PaymentActor became a bottleneck. The mailbox grew to millions of messages, and the memory usage spiked until the JVM hit an OutOfMemoryError.

This is where Elasticity comes in. A reactive system needs to scale up or down based on load without requiring a rewrite of the logic. I stopped using a single actor and switched to a Router.

// Instead of one actor, we create a pool of them
val paymentRouter = context.actorOf(
  RoundRobinPool(5).props(Props[PaymentActor]), 
  "payment-router"
)

Now, I can dynamically adjust the number of workers in that pool based on the CPU load or mailbox size. The system "stretches" to meet the demand and shrinks when the sale is over to save costs. That's elasticity in action.

Connecting the Dots

Looking back at this exploration, you can see how the principles feed into each other. You can't have true elasticity without being Message-Driven (because you need to decouple the sender from the receiver to move them across different threads or servers). You can't have responsiveness under load without Resilience (because one hanging dependency shouldn't bring down the whole fleet).

  • Responsive: It answers quickly, even when things are slow.
  • Resilient: It stays up even when components fail.
  • Elastic: It scales its resources to match the load.
  • Message-Driven: It uses asynchronous communication to achieve the other three.



📋 Practical Task

Implementing a Supervised Order Pipeline

You are tasked with fixing a fragile payment pipeline. You have an OrderProcessor actor that delegates work to a PaymentWorker. Currently, if the PaymentWorker throws a PaymentGatewayException, the actor dies and the OrderProcessor stops receiving updates.

Your Goal: Modify the OrderProcessor to implement a OneForOneStrategy. The strategy should:

  1. Restart the worker if a PaymentGatewayException occurs.
  2. Stop the worker permanently if a FatalConfigurationException occurs (since a restart won't fix a bad config).
  3. Escalate any other type of exception to the parent supervisor.

Write the Scala code for the OrderProcessor's supervisor strategy and the actor initialization to ensure the system remains resilient according to the Reactive Manifesto.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.