Barsum
Barsum

Reputation: 156

Basic akka - locating actors and their state

This is a couple of very basic questions concerning akka. I'm totally green on akka, so any advice is appreciated.

Say that I have an application with thousands of users - not millions.

The domain model is a hierarchy where each user is located in a region within a country i.e. a country has multiply regions, a region has multiple users.

I'm thinking of creating the exact same hierarchy in akka. CountryActor ->* RegionActor ->* UserActor. The CountryActor/RegionActor will aside from being parents, also have functional duties such as collecting newsfeeds, calculating statistics, supervising its children etc.

Q1: Does it make sense to mimic the domain model this way?

Q2: Should I store the attributes of each entity in the actor? This way the data would only need to be stored once and the akka system would effectively be memory database as well.

(pseudo-code)

CountryActor {
  Name,
  CountryCode,
  (children = list of RegionActors handled by akka)
}

RegionActor {
  Name,
  RegionCode,
  (children = list of UserActors handled by akka)
}

UserActor {
  UserId,
  Firstname,
  Lastname,
  Alias,
  ReceiveRegionalNews,
  ReceiveCountryNews,
  ...
}

Q3: How do I efficiently lookup a user by userId? I expect the child-name of the UserActor will be the userId, but given that I only have a userId, I still need to find that correct Country and Region to do a getContent().findChild(userId)? Do I need to keep a complete map of all userIds and reference to their actor?

Q4: How to locate actors by their state? Imagine that each user has the ability to switch on a RegionalNews attribute, which means that he wants to receive news from the RegionalActor. Whenever the RegionalActor want to distribute news to all listeners, how does it locate them? Does it keep an internal map of the users with the attribute or does it make a broadcast to all it's children and then send to all responders?

Thanks in advance

Upvotes: 1

Views: 171

Answers (1)

mwardm
mwardm

Reputation: 2013

I don't have a huge amount of experience in modelling this stuff in Akka(.Net) in particular, but I'm old enough to know that none of these paradigms are magic and just because an actor system is hierarchical it doesn't mean you can fit all your data into such a structure without what's probably called an impedance mismatch.

If you were to think more along the lines of...

  • A UserManagerActor doing some child-per-entity management of your UserActors;
  • Alongside those, a Country / Region hierarchy, where at least Region implements some pub-sub pattern and keeps a list of its subscribers;
  • When it's created, a User sends a subscribe message to its appropriate Region (or more than one if that floats your boat);

...then would that make everything you need to do seem much easier?

There's more on design patterns here - but don't get too caught up in them!

Upvotes: 1

Related Questions