Search code examples
javaakkadelaydistributed-systemraft

In Akka, how can network delays be simulated?


I have a question that is more of a design one rather than a coding one.

I'm currently using Akka (we're transitioning from Classic to Typed) to implement a Raft cluster, using the Java version. Our assignment requires the cluster - like the paper implementation - to operate on harsh network conditions, and as such I would like to implement Akka-side a way to systematically delay messages. Timings and selectivity do not matter - e.g., assume we want to delay ALL messages going through the system by 200ms.u

My idea was to use Routers - https://doc.akka.io/docs/akka/current/typed/routers.html - but I would like to know what's the best approach to write something that is both scalable and does not add unpredictable bugs (like using Thread.sleep() does, which delays message queue handling)

EDIT: The raft cluster is hosted on a single machine, so transmission is basically instantaneous now, and any interaction is handled by Akka itself. No network stack is ever involed.


Solution

  • There are a number of ways you can accomplish this, but note that in Akka you can explicitly model the network by sending messages to an actor A through another actor B, e.g. by wrapping the messages in an envelope class which has the destination and the message. Actor B then forwards the message actor A and can use Akka's scheduling utilities (they differ somewhat between Classic and Typed) to delay the send; actor B can also simulate arbitrary message loss.