Debian Codegraph @

docpages/advanced_reference/clusters_shards_and_guilds.md

raw symbols SCIP
1 \page clusters-shards-guilds Clusters, Shards, and Guilds
2
3 D++ takes a three-tiered highly scalable approach to bots, with three levels known as Clusters, Shards, and Guilds as documented below.
4
5 \dot
6 digraph "Clusters, Shards and Guilds" {
7 node [colorscheme="blues9",fontname="helvetica"];
8 subgraph Bot {
9 node [style=filled, color=1];
10 label = "Bot"
11 "Bot" [shape=folder, label="Bot", bordercolor=black];
12 };
13 subgraph Processes {
14 node [style=filled, color=2];
15 label = "Processes"
16 "Bot" -> "Process 1"
17 "Bot" -> "Process 2"
18 "Process 1" [shape=record, label="Process"];
19 "Process 2" [shape=record, label="Process"];
20 };
21 subgraph Clusters {
22 node [style=filled, color=3];
23 label = "Clusters"
24 "Process 1" -> "Cluster 1"
25 "Process 2" -> "Cluster 2"
26 "Cluster 1" [shape=record, label="Cluster"];
27 "Cluster 2" [shape=record, label="Cluster"];
28 };
29 subgraph Shards {
30 node [style=filled, color=4];
31 label = "Shards"
32 "Shard 1" [shape=record, label="Shard"];
33 "Shard 2" [shape=record, label="Shard"];
34 "Shard 3" [shape=record, label="Shard"];
35 "Shard 4" [shape=record, label="Shard"];
36 "Shard 5" [shape=record, label="Shard"];
37 "Shard 6" [shape=record, label="Shard"];
38 "Shard 7" [shape=record, label="Shard"];
39 "Shard 8" [shape=record, label="Shard"];
40 "Cluster 1" -> "Shard 1"
41 "Cluster 1" -> "Shard 3"
42 "Cluster 2" -> "Shard 2"
43 "Cluster 2" -> "Shard 4"
44 "Cluster 1" -> "Shard 5"
45 "Cluster 1" -> "Shard 7"
46 "Cluster 2" -> "Shard 6"
47 "Cluster 2" -> "Shard 8"
48 };
49 subgraph Guilds {
50 node [style=filled, color=5];
51 label = "Guilds";
52 "Guild 1" [shape=record, label="Guild"];
53 "Guild 2" [shape=record, label="Guild"];
54 "Guild 3" [shape=record, label="Guild"];
55 "Guild 4" [shape=record, label="Guild"];
56 "Guild 5" [shape=record, label="Guild"];
57 "Guild 6" [shape=record, label="Guild"];
58 "Guild 7" [shape=record, label="Guild"];
59 "Guild 8" [shape=record, label="Guild"];
60 "Guild 9" [shape=record, label="Guild"];
61 "Guild 10" [shape=record, label="Guild"];
62 "Guild 11" [shape=record, label="Guild"];
63 "Guild 12" [shape=record, label="Guild"];
64 "Guild 13" [shape=record, label="Guild"];
65 "Guild 14" [shape=record, label="Guild"];
66 "Guild 15" [shape=record, label="Guild"];
67 "Guild 16" [shape=record, label="Guild"];
68 "Shard 1" -> "Guild 1"
69 "Shard 1" -> "Guild 5"
70 "Shard 2" -> "Guild 2"
71 "Shard 2" -> "Guild 6"
72 "Shard 3" -> "Guild 3"
73 "Shard 3" -> "Guild 7"
74 "Shard 4" -> "Guild 4"
75 "Shard 4" -> "Guild 8"
76 "Shard 5" -> "Guild 9"
77 "Shard 5" -> "Guild 11"
78 "Shard 6" -> "Guild 10"
79 "Shard 6" -> "Guild 12"
80 "Shard 7" -> "Guild 13"
81 "Shard 7" -> "Guild 15"
82 "Shard 8" -> "Guild 14"
83 "Shard 8" -> "Guild 16"
84 };
85 }
86 \enddot
87
88 ## Clusters
89
90 A bot may be made of one or more clusters. Each cluster maintains a queue of commands waiting to be sent to Discord, a queue of replies from Discord for all commands executed, and zero or more **shards**. Usually, each process has one cluster, but the D++ library does not enforce this as a restriction. Small bots will require just one cluster. Clusters will split the required number of shards equally across themselves. There is no communication between clusters unless you add some yourself, they all remain independent without any central "controller" process. This ensures that there is no single point of failure in the design. Whenever you instantiate the library, you generally instantiate a cluster:
91
92 ```cpp
93 #include <dpp/dpp.h>
94
95 int main()
96 {
97 /* This is a cluster */
98 dpp::cluster bot("Token goes here");
99 }
100 ```
101
102 ## Shards
103
104 A cluster contains zero or more shards. Each shard maintains a persistent connection to Discord via a websocket, which receives all events the bot is made aware of, e.g. messages, channel edits, etc. Requests to the API on the other hand go out to Discord as separate HTTP requests.
105
106 Small bots will require only one shard and this is the default when you instantiate a cluster. The library will automatically determine and create the correct number of shards needed, if you do not configure it by hand. If you do want to specify a number of shards, you can specify this when creating a cluster:
107
108 ```cpp
109 #include <dpp/dpp.h>
110
111 int main()
112 {
113 /* This is a cluster */
114 int total_shards = 10;
115 dpp::cluster bot("Token goes here", dpp::i_default_intents, total_shards);
116 }
117 ```
118
119 Remember that if there are multiple clusters, the number of shards you request will be split equally across these clusters!
120
121 @note To spawn multiple clusters, you can specify this as the 4th and 5th parameter of the dpp::cluster constructor. You must do this, if you want this functionality. The library will not create additional clusters for you, as what you require is dependent upon your system specifications. It is your responsibility to somehow get the cluster id and total clusters into the process, e.g. via a command line argument. An example of this is shown below based on the cluster setup code of **TriviaBot**:
122 ```cpp
123 #include <dpp/dpp.h>
124 #include <iostream>
125 #include <stdlib.h>
126 #include <getopt.h>
127 #include <string>
128
129 int main(int argc, char** argv)
130 {
131 int total_shards = 64;
132 int index;
133 char arg;
134 bool clusters_defined = false;
135 uint32_t clusterid = 0;
136 uint32_t maxclusters = 1;
137
138 /* Parse command line parameters using getopt() */
139 struct option longopts[] =
140 {
141 { "clusterid", required_argument, NULL, 'c' },
142 { "maxclusters", required_argument, NULL, 'm' },
143 { 0, 0, 0, 0 }
144 };
145 opterr = 0;
146 while ((arg = getopt_long_only(argc, argv, "", longopts, &index)) != -1) {
147 switch (arg) {
148 case 'c':
149 clusterid = std::stoul(optarg);
150 clusters_defined = true;
151 break;
152 case 'm':
153 maxclusters = std::stoul(optarg);
154 break;
155 default:
156 std::cerr << "Unknown parameter '" << argv[optind - 1] << "'\n";
157 exit(1);
158 break;
159 }
160 }
161
162 if (clusters_defined && maxclusters == 0) {
163 std::cerr << "ERROR: You have defined a cluster id with -clusterid but no cluster count with -maxclusters.\n";
164 exit(2);
165 }
166
167 dpp::cluster bot("Token goes here", dpp::default_intents, total_shards, clusterid, maxclusters);
168 }
169 ```
170
171 ### Large Bot Sharding
172
173 Discord restricts how many shards you can connect to at any one time to one per five seconds, unless your bot is in at least 150,000 guilds. Once you reach 150,000 guilds, Discord allow your bot to connect to more guilds concurrently, and your number of shards must divide cleanly into this value. By default, at 150,000 guilds this concurrency value is 16 meaning D++ will attempt to connect 16 shards in parallel, then wait for all these to connect and then connect another 16, until all shards are connected. In practice, this means a large bot with many shards (read: hundreds!) will connect significantly faster after a full restart. **You do not need to manually configure large bot sharding and connection concurrency, the D++ library will handle this for you if you are able to use it**.
174
175
176 ## Guilds
177
178 Guilds are what servers are known as to the Discord API. There can be up to **2500** of these per shard. Once you reach 2500 guilds on your bot, Discord force your bot to shard, the D++ library will automatically create additional shards to accommodate if not explicitly configured with a larger number. Discord *does not restrict sharding* to bots on 2500 guilds or above. You can shard at any size of bot, although it would be a waste of resources to do so unless it is required.