You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: motoko/parallel_calls/README.md
+17-44Lines changed: 17 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -2,70 +2,42 @@
2
2
3
3
This example demonstrates how to implement inter-canister calls that run in parallel in Motoko, and highlights some differences between parallel and sequential calls. Running independent calls in parallel can lower the latency, especially when messages are sent across subnets. For example, a canister that swaps two tokens might want to launch both token transfer operations in parallel.
4
4
5
-
## Architecture
6
-
7
5
The sample code revolves around two simple canisters, `caller` and `callee`. `Caller` has three endpoints:
8
6
1.`setup_callee`, to set the ID of the callee canister.
9
7
2.`sequential_calls` and `parallel_calls`, which both take a number `n` and issue `n` calls to the callee, returning the number of successful calls. The former performs calls sequentially, the latter in parallel.
10
8
11
9
The callee exposes a simple `ping` endpoint that takes no parameters and returns nothing.
12
10
13
-
## Prerequisites
14
-
This example requires an installation of:
15
-
16
-
-[x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx).
17
-
-[x] Clone the example dapp project: `git clone https://github.com/dfinity/examples`
Navigate into the folder containing the project's files and start a local instance of the Internet Computer with the commands:
24
-
25
-
```bash
26
-
cd examples/motoko/parallel_calls
27
-
dfx start --background
28
-
```
15
+
## Build and deploy from the command-line
29
16
30
-
##Step 2: Deploy the canisters
17
+
### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)
31
18
32
-
```bash
33
-
dfx deploy --no-wallet
34
-
```
19
+
### 2. Download your project from ICP Ninja using the 'Download files' button on the upper left corner, or [clone the GitHub examples repository.](https://github.com/dfinity/examples/)
35
20
36
-
##Step 3: Set up the caller canister
21
+
### 3. Navigate into the project's directory.
37
22
38
-
We now provide the ID of the callee to the caller, such that the caller can initiate calls.
23
+
### 4. Deploy the project to your local environment:
39
24
40
25
```
41
-
dfx canister call caller setup_callee "(principal \"`dfx canister id callee`\")"
26
+
dfx start --background --clean && dfx deploy
42
27
```
43
28
44
-
## Step 4: Invoke sequential and parallel calls
45
-
46
-
Let's first call the different endpoints of the `caller` canister using `dfx`
29
+
Invoke sequential and parallel calls. First call the different endpoints of the `caller` canister using `dfx`:
47
30
48
31
```bash
49
32
dfx canister call caller sequential_calls 100
50
33
```
51
34
52
-
This should output:
53
-
```bash
54
-
(100 : nat64)
55
-
```
56
-
57
35
And the other endpoint:
58
36
59
37
```bash
60
38
dfx canister call caller parallel_calls 100
61
39
```
62
40
63
-
This should output:
64
-
65
-
```bash
66
-
(100 : nat64)
67
-
```
68
-
69
41
The results are identical: all calls succeed. There also isn't a large difference in performance between these calls:
70
42
71
43
```bash
@@ -79,7 +51,7 @@ dfx canister call caller parallel_calls 100 0.11s user 0.03s system 8% cpu 1.72
79
51
80
52
The reason why the performance is similar is because the local replica has only a single subnet. Inter-canister calls normally have almost no latency on a single subnet, so it doesn't matter much if we run them sequentially or in parallel.
81
53
82
-
However, once we increase the number of calls, we observe a difference in both the results and performance.
54
+
However, once you increase the number of calls, you can observe a difference in both the results and performance.
83
55
84
56
```bash
85
57
time dfx canister call caller sequential_calls 2000
@@ -92,11 +64,7 @@ dfx canister call caller parallel_calls 2000 0.11s user 0.03s system 4% cpu 3.5
92
64
93
65
All the sequential calls succeed, but most parallel calls fail. The reason is that the replica imposes a limit on the number of in-flight calls a canister can make (in particular, to a different canister). Doing the calls sequentially yields only one in-flight call at a time. However, too many parallel calls exceed the limit, after which the calls start failing. Note that it's also possible to hit this limit with sequential calls under high load (if `sequential_call` was itself called many times in parallel). If such limits are hit, immediate retries will also fail; retries should be done in a timer or a heartbeat instead.
94
66
95
-
Lastly, the parallel calls here complete sooner -- because most of them fail!
96
-
97
-
## Step 5: Multi-subnet setting
98
-
99
-
Parallel calls are a lot more useful in multi-subnet settings. We can create such a setting locally using Pocket IC.
67
+
Parallel calls are a lot more useful in multi-subnet settings. Create such a setting locally using Pocket IC:
100
68
101
69
First, follow the [installation instructions](https://github.com/dfinity/pocketic) to install `pocket-ic` in the `parallel_calls` directory.
Parallel calls: 90/90 successful calls in 296.402ms
111
79
```
112
80
113
-
As you can see, parallel calls run a lot faster than sequential calls here. The difference on the IC mainnet would be significantly larger still, as Pocket IC executes rounds much faster than the IC mainnet.
81
+
As you can see, parallel calls run a lot faster than sequential calls here. The difference on the ICP mainnet would be significantly larger still, as Pocket IC executes rounds much faster than the ICP mainnet.
82
+
83
+
## Security considerations and best practices
84
+
85
+
If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/building-apps/security/overview) for developing on ICP. This example may not implement all the best practices.
0 commit comments