class Router(routeeProps: Props, parallelism: Int) extends Actor with ActorLogging { private def partitionCampaign(cid: String): Int = { val rawMod = cid.hashCode % parallelism rawMod + (if (rawMod < 0) parallelism else 0) } override def receive: Receive = initialized(ListMap.empty[Int, ActorRef]) def initialized(routees: Map[Int, ActorRef]): Receive = { case cacheCmd: CacheCmd => val partition = partitionCampaign(cacheCmd.cid) val (newRoutees, updated) = routees.updateIfMissing(partition)(context.actorOf(routeeProps)) newRoutees(partition).tell(cacheCmd, sender()) if (updated) context.become(initialized(newRoutees)) } }