Distributed data

47 views
Skip to first unread message

Steve Winfield

unread,
Nov 28, 2016, 12:24:23 PM11/28/16
to Akka User List
Hey,

I'm currently programming a room service application which gets a room actor reference from a map (given an id) or creates a new room actor if there is no reference existent. Right now, it works fine because there is only one service. Nevertheless, I want to create a cluster of room services and this is where it begins to get tricky for me.

I want to ensure that there is only one instance of a room among the services.

My first attempt:

(Starting up)
1. When started, it waits for the CurrentClusterState and sends a "register" message to all given nodes.
2. It creates a counter for expected "register" responses to make sure that it doesn't look for existent rooms across the services until it knows about all.
3. After getting all responses, it is ready for "get this room" input.

(Fetching rooms)
4. When it receives a "get room with id 1" message, it looks for an entry in the local map.
5. If it founds one, it's done.
6. If it doesn't find it, it sends a "get room with id 1 or avoid creation of it" message across the cluster (the "avoid creation of it" message is sent in order to avoid that another service creates the same room after it was made sure that the room doesn't exist globally).
7. Now, it waits for all responses. If there is a room, it sends a "make the room creatable again" message to all services and it's done.
8. If it doesn't find it again, it creates the room, adds it to the local map and sends a "make the room creatable again" message to all services.
9. (Sends result to requestor)

In my opinion, this solution is way too complicated and it depends too much on the availability of all nodes.

Is Redis (especially the command https://redis.io/commands/setnx) a good alternative (my second approach)? How would you guys solve this "problem"?

Thanks!

Cheers,
Steve Winfield

Rafał Krzewski

unread,
Nov 28, 2016, 12:43:26 PM11/28/16
to Akka User List
Hi Steve,
have you looked at http://doc.akka.io/docs/akka/2.4/scala/cluster-sharding.html? It seems well suited to your use case if I understood it correctly.

Cheers,
Rafał

Steve Winfield

unread,
Nov 28, 2016, 1:23:28 PM11/28/16
to Akka User List
Hey Rafał,
thanks for your response.

Yes, but I wasn't sure whether the data is consistent across the cluster and how to create the room only if it's not existent (I might not understand it properly).

Cheers,
Steve

Justin du coeur

unread,
Nov 28, 2016, 4:08:00 PM11/28/16
to akka...@googlegroups.com
That's all quite common for Cluster Sharding.  The notion of Sharding is that, if you set it up correctly, a given identity will always map to a particular Actor on a particular node -- so long as the Cluster itself is consistent, the data for a given room should always exist on at most one live Actor.  (And if there are no live Actors for the Room yet, it will boot one up.)

As for creating it if it doesn't exist, that depends on whether you want long-term persistence or not.  If so, there are various ways to accomplish that, but the easiest is Akka Persistence -- basically, when the Room Actor for a given Room boots up, it will try to recover its previous events.  You then code it so that, if it doesn't find any, it sets its state to some reasonable initial value, and you're off and running...

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages