LoginSignup
5
5

More than 5 years have passed since last update.

AkkaのActorを増やす話

Last updated at Posted at 2017-04-04

※rpscalaの発表用に作った資料です。

はじめに

AkkaのActor使ってますか。Futureだと不足するような高度な並行並列処理をするのに便利でちょいちょい使います。

今回は同じ処理をするActorを簡単に増やすRoutingを紹介します。

サンプル

Actorを使うときはこんな感じですね。

import akka.actor._
val system = ActorSystem()
val actor = system.actorOf(Props(new HogeActor(fuga)))
actor ! value

Routing

Actorを増やして並列に処理させたいときはRoutingを使います。

import akka.actor._
val system = ActorSystem()
val actor = system.actorOf(Props(new HogeActor(fuga)).withRouter(SmallestMailboxPool(4)))
actor ! value

滅茶苦茶簡単ですね。

Routingアルゴリズム

複数のActorに処理を分散するということは、割り当てのアルゴリズムを指定する必要があります。

RoundRobin

順番に割り当て

Random

ランダムに割り当て

SmallestMailbox

メールボックスが一番小さいものに割り当て。ネットワーク越しのActorに対してはレイテンシが厳しいので特にそういうことはしなくて別の優先順位で決定するようだ。

Broadcast

全体に配信

ScatterGatherFirstCompleted

全体に配信して一番早く処理が終わったものを返す

TailChopping

ランダムに順序を決めて、最初のActorから応答が無かったら次のActorへ処理を移す。

ConsistentHashing

Hash関数を使って、同じValueは同じActorで処理するように分配する

公式

詳しくは公式を見ましょう。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5