Skip to content

Commit b365c06

Browse files
eabaAaronontheweb
andauthored
Add Member Roles doc (#5742)
* Add `Node Roles` doc Co-authored-by: Aaron Stannard <[email protected]>
1 parent 9e05894 commit b365c06

File tree

4 files changed

+320
-0
lines changed

4 files changed

+320
-0
lines changed

docs/articles/clustering/cluster-overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ A node might also exit the cluster gracefully, preventing it from being marked a
236236
<iframe width="560" height="315" src="https://www.youtube.com/embed/mUTKvGyxbOA" frameborder="0" allowfullscreen></iframe>
237237
<!-- markdownlint-enable MD033 -->
238238

239+
* [Cluster Member Roles](xref:member-roles)
239240
* [How to Create Scalable Clustered Akka.NET Apps Using Akka.Cluster](https://petabridge.com/blog/intro-to-akka-cluster/)
240241
* [Video: Introduction to Akka.Cluster](https://www.youtube.com/watch?v=mUTKvGyxbOA)
241242
* [Gossip Protocol](https://en.wikipedia.org/wiki/Gossip_protocol)
Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
---
2+
uid: member-roles
3+
title: Member Roles
4+
---
5+
6+
# Member Roles
7+
8+
![cluster roles](/images/cluster/cluster-roles.png)
9+
10+
A cluster can have multiple nodes(machines/servers/vms) with different capabilities.
11+
When you require an application to run on a node(machine/server/vm) with certain capabilities, roles helps you to distinguish the nodes so that application can be deployed on that node.
12+
Specifying cluster role(s) is a best practice; you don't want an application that requires less computational power running and consuming resources meant for a mission-critical and resource-intensive application.
13+
Even if you only have a single type of node in your cluster, you should still use roles for it so you can leverage this infrastructure as your cluster expands in the future; and they add zero overhead in any conceivable way.
14+
15+
# How To Configure Cluster Roles
16+
17+
Below I will show you how the cluster above can be reproduced. I will create a five-nodes(ActorSystems - all having same name, though, but living on different machine/server/vm) cluster with different roles applied:
18+
19+
**Node1**: All of my code that receives requests from users and push same to the cluster will be deployed here!
20+
21+
```hocon
22+
akka
23+
{
24+
cluster
25+
{
26+
roles = ["web"]
27+
}
28+
}
29+
```
30+
31+
**Node2**: All of my code handling fraud detections will be deployed on this node
32+
33+
```hocon
34+
akka
35+
{
36+
cluster
37+
{
38+
roles = ["fraud"]
39+
}
40+
}
41+
```
42+
43+
**Node3**: All me code that retrieves, stores data will be deployed on this node
44+
45+
```hocon
46+
akka
47+
{
48+
cluster
49+
{
50+
roles = ["storage"]
51+
}
52+
}
53+
```
54+
55+
**Node4**: All my code that handles customer orders will be deployed on this node
56+
57+
```hocon
58+
akka
59+
{
60+
cluster
61+
{
62+
roles = ["order"]
63+
}
64+
}
65+
```
66+
67+
**Node5**: All my code that handles customer billing will be deployed on this node
68+
69+
```hocon
70+
akka
71+
{
72+
cluster
73+
{
74+
roles = ["billing"]
75+
}
76+
}
77+
```
78+
79+
Now that we have laid the foundation for what is to follow, Akka.Cluster is made of various ready-made extensions(or modules) you can deploy.
80+
I will show you how you can deploy them on any of the nodes. Apart from the Akka.Cluster modules, if you just want to use the Akka.Cluster core, I will show you how you can deploy your own actor to the cluster node with the required role:
81+
82+
**Cluster Sharding**: Sharding be will deployed on the nodes with the `order` role, `node4`
83+
84+
```hocon
85+
akka
86+
{
87+
cluster
88+
{
89+
roles = ["order"]
90+
sharding
91+
{
92+
role = "order"
93+
}
94+
}
95+
}
96+
```
97+
98+
**Distributed Pub-Sub**: DistributedPubSub will be deployed on the nodes with the `web` role, `node1`.
99+
100+
```hocon
101+
akka
102+
{
103+
cluster
104+
{
105+
roles = ["web"]
106+
pub-sub
107+
{
108+
role = "web"
109+
}
110+
}
111+
}
112+
```
113+
114+
**Distributed Data**: DistributedData will be deployed on the node with the `storage` role, `node3`.
115+
116+
```hocon
117+
akka
118+
{
119+
cluster
120+
{
121+
roles = ["storage"]
122+
distributed-data
123+
{
124+
role = "storage"
125+
}
126+
}
127+
}
128+
```
129+
130+
**Cluster Singleton**: To avoid over charging a customer more than once, my code will be deployed with `ClusterSingleton` on the node with the `billing` role, `node5`
131+
132+
```hocon
133+
akka
134+
{
135+
cluster
136+
{
137+
roles = ["billing"]
138+
singleton
139+
{
140+
role = "billing"
141+
}
142+
}
143+
}
144+
```
145+
146+
I have one more node, `node2`, with nothing running in it. I will deploy my custom fraud detection code there, and the way to do that is:
147+
148+
```csharp
149+
var selfMember = Cluster.Get(_actorSystem).SelfMember;
150+
if (selfMember.HasRole("fraud"))
151+
{
152+
context.ActorOf(Billing.Prop(), "bill-gate");
153+
}
154+
else
155+
{
156+
//sleep, probably!
157+
}
158+
```
159+
160+
Using the Cluster `SelfMember`, I am checking if the current node has the `billing` role and if yes, create the `Billing` actor.
161+
162+
## Cluster-Aware Router
163+
164+
Cluster-Aware routers automate how actors are deployed on the cluster and also how messages are routed based on the role specified! Routers in Akka.NET can be either grouped or pooled and you can read up on them [Routers](https://getakka.net/articles/actors/routers.html)
165+
166+
**Router Group**: I will create Cluster-Aware Router Group for all my applications above!
167+
168+
```hocon
169+
akka
170+
{
171+
actor
172+
{
173+
provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
174+
deployment
175+
{
176+
/webdispatcher
177+
{
178+
router = consistent-hashing-group # routing strategy
179+
routees.paths = ["/user/web"] # path of routee on each node
180+
nr-of-instances = 3 # max number of total routees
181+
cluster
182+
{
183+
enabled = on
184+
use-role = "web"
185+
}
186+
}
187+
/frauddispatcher
188+
{
189+
router = consistent-hashing-group # routing strategy
190+
routees.paths = ["/user/fraud"] # path of routee on each node
191+
nr-of-instances = 3 # max number of total routees
192+
cluster
193+
{
194+
enabled = on
195+
use-role = "fraud"
196+
}
197+
}
198+
/billingdispatcher
199+
{
200+
router = consistent-hashing-group # routing strategy
201+
routees.paths = ["/user/billing"] # path of routee on each node
202+
nr-of-instances = 3 # max number of total routees
203+
cluster
204+
{
205+
enabled = on
206+
use-role = "billing"
207+
}
208+
}
209+
/orderdispatcher
210+
{
211+
router = consistent-hashing-group # routing strategy
212+
routees.paths = ["/user/order"] # path of routee on each node
213+
nr-of-instances = 3 # max number of total routees
214+
cluster
215+
{
216+
enabled = on
217+
use-role = "order"
218+
}
219+
}
220+
/storagedispatcher
221+
{
222+
router = consistent-hashing-group # routing strategy
223+
routees.paths = ["/user/storage"] # path of routee on each node
224+
nr-of-instances = 3 # max number of total routees
225+
cluster
226+
{
227+
enabled = on
228+
use-role = "storage"
229+
}
230+
}
231+
}
232+
}
233+
}
234+
```
235+
236+
```csharp
237+
var web = system.ActorOf<Web>("web");
238+
var fraud = system.ActorOf<Fraud>("fraud");
239+
var order = system.ActorOf<Order>("order");
240+
var billing = system.ActorOf<Billing>("billing");
241+
var storage = system.ActorOf<Storage>("storage");
242+
243+
var webRouter = system.ActorOf(Props.Empty.WithRouter(FromConfig.Instance),"webdispatcher");
244+
var fraudRouter = system.ActorOf(Props.Empty.WithRouter(FromConfig.Instance),"frauddispatcher");
245+
var orderRouter = system.ActorOf(Props.Empty.WithRouter(FromConfig.Instance),"orderdispatcher");
246+
var billingRouter = system.ActorOf(Props.Empty.WithRouter(FromConfig.Instance),"billingispatcher");
247+
var storageRouter = system.ActorOf(Props.Empty.WithRouter(FromConfig.Instance),"storagedispatcher");
248+
```
249+
250+
**Router Pool**: I will create Cluster-Aware Router Pool for all my applications above!
251+
252+
```hocon
253+
akka
254+
{
255+
actor
256+
{
257+
provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
258+
deployment
259+
{
260+
/webdispatcher
261+
{
262+
router = round-robin-pool # routing strategy
263+
max-nr-of-instances-per-node = 5
264+
cluster
265+
{
266+
enabled = on
267+
use-role = "web"
268+
}
269+
}
270+
/frauddispatcher
271+
{
272+
router = round-robin-pool # routing strategy
273+
max-nr-of-instances-per-node = 5
274+
cluster
275+
{
276+
enabled = on
277+
use-role = "fraud"
278+
}
279+
}
280+
/billingdispatcher
281+
{
282+
router = round-robin-pool # routing strategy
283+
max-nr-of-instances-per-node = 5
284+
cluster
285+
{
286+
enabled = on
287+
use-role = "billing"
288+
}
289+
}
290+
/orderdispatcher
291+
{
292+
router = round-robin-pool # routing strategy
293+
max-nr-of-instances-per-node = 5
294+
cluster
295+
{
296+
enabled = on
297+
use-role = "order"
298+
}
299+
}
300+
/storagedispatcher
301+
{
302+
router = round-robin-pool # routing strategy
303+
max-nr-of-instances-per-node = 5
304+
cluster
305+
{
306+
enabled = on
307+
use-role = "storage"
308+
}
309+
}
310+
}
311+
}
312+
}
313+
```
314+
315+
```csharp
316+
var routerProps = Props.Empty.WithRouter(FromConfig.Instance);
317+
```

docs/articles/clustering/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
- name: Overview
22
href: cluster-overview.md
3+
- name: Member Roles
4+
href: member-roles.md
35
- name: Cluster Routing
46
href: cluster-routing.md
57
- name: Cluster Configuration
60.4 KB
Loading

0 commit comments

Comments
 (0)