Akka
Message Delivery
System Failure
Network Programming
Message Failure Rate

Akka - why is it that messages are not guaranteed to arrive (after being send)? What is the failure rate for messages?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Akka is a toolkit and runtime for building highly concurrent, distributed, and fault-tolerant event-driven applications on the JVM. Akka uses the Actor Model to abstract away the complexity involved in writing such applications. Despite its robust design, Akka does not guarantee that messages sent between actors will always arrive. Understanding why this is the case, and estimating the failure rate of message delivery, requires a deeper look into Akka's architecture and the principles underpinning distributed systems.

Why Messages May Not Arrive

  1. At-most-once Delivery Default: Akka's default delivery guarantee is "at-most-once," which means that messages won't be delivered more than once, but might not be delivered at all. This model is primarily chosen for performance reasons, as guaranteeing delivery would often require additional overhead - both in terms of time (latency) and resources (bandwidth, memory).
  2. Network Issues: In distributed systems, messages are sent over networks that can be unreliable. Network failures, such as broken connections or lost packets, can lead to undelivered messages.
  3. Actor Failures: Actors in Akka can fail due to reasons like exceptions thrown during message processing. If an actor dies after receiving a message but before successfully processing it, the message might be lost if it's not persisted or handled correctly by a supervisor.
  4. No Buffering by Default: Akka does not buffer messages by default. If an actor gets overloaded and can't process messages quickly enough, sent messages might get dropped, especially during high-load scenarios.
  5. System Crashes: In cases where the entire system (or part of it) crashes, in-flight messages can be lost if they are not persisted. This scenario can be mitigated by distributed data and persistent actors, though it adds to system complexity.

Failure Rate for Messages

The failure rate for messages in Akka inherently depends on various factors including network reliability, system load, the robustness of the hardware, and the design of the application. There's no fixed failure rate that can be universally applied; instead, the failure rate can be minimized through design choices like:

  • Using additional modules such as Akka Persistence, which helps in dealing with actor state recovery and therefore indirectly stabilizes message delivery by persisting actor state.
  • Implementing robust error handling and supervision strategies for actors.
  • Employing strategies such as "at-least-once delivery" by using tools like Akka Persistence or custom solutions to store and retry message delivery until it is confirmed.

Technical Example

Suppose we have two actors, Sender and Receiver, residing in different parts of a distributed system. The sender is sending messages to the receiver over a network that occasionally drops packets.

scala
1import akka.actor.Actor
2import akka.actor.ActorSystem
3import akka.actor.Props
4
5class Receiver extends Actor {
6  def receive = {
7    case msg: String => println(s"Received message: $msg")
8    case _ => println("Unknown message")
9  }
10}
11
12class Sender(receiver: ActorRef) extends Actor {
13  def receive = {
14    case "Send" =>
15      println("Sending message to receiver")
16      receiver ! "Hello, Receiver!"  // message sending
17  }
18}
19
20val system = ActorSystem("MessageSystem")
21val receiver = system.actorOf(Props[Receiver], "receiver")
22val sender = system.actorOf(Props(new Sender(receiver)), "sender")
23
24sender ! "Send"

In this example, the Sender actor sends a "Hello, Receiver!" message to the Receiver. This simplistic scenario doesn't take into account message loss due to network issues or system failures.

Enhancing Reliability

For enhancing message delivery reliability in Akka:

  • Akka Persistence: Can ensure that messages are persisted and can be resent in case of actor failures.
  • Acknowledge Pattern: Receiver can send back an acknowledgment to the sender upon receiving a message. The sender retries sending the message until it receives an acknowledgment.

Summary Table

FeatureDescriptionImpact on Message Delivery
At-most-once deliveryDefault delivery guarantee where messages are not redelivered on failure.Increases potential for message loss.
Network ReliabilityDepends on the underlying hardware and network infrastructure.Critical factor in delivery success.
Actor SupervisionStrategies to manage and recover from failures.Minimizes message loss due to actor failures.
Message PersistenceEnsuring messages are stored until they are confirmed received.Reduces message loss during failures.

In conclusion, while Akka provides powerful abstractions and models for building distributed systems, it operates under the constraints inherent to distributed communication and computing. Understanding these limitations is crucial for effectively designing and deploying resilient Akka applications.


Course illustration
Course illustration

All Rights Reserved.