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: rust/parallel_calls/README.md
+19-39Lines changed: 19 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -2,69 +2,45 @@
2
2
3
3
This example demonstrates how to implement inter-canister calls that run in parallel in Rust, 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
11
+
## Deploying from ICP Ninja
14
12
15
-
-[x] Install the [IC
16
-
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
17
-
-[x] Clone the example dapp project: `git clone https://github.com/dfinity/examples`
### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)
22
18
23
-
Navigate into the folder containing the project's files and start a local instance of the replica with the commands:
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/)
24
20
25
-
```bash
26
-
cd examples/rust/parallel_calls
27
-
dfx start --background
28
-
```
21
+
### 3. Navigate into the project's directory.
29
22
30
-
##Step 2: Deploy the canisters
23
+
### 4. Deploy the project to your local environment:
31
24
32
-
```bash
33
-
dfx deploy --no-wallet
34
25
```
35
-
36
-
## Step 3: Set up the caller canister
37
-
38
-
We now provide the ID of the callee to the caller, such that the caller can initiate calls.
39
-
```
40
-
dfx canister call caller setup_callee "(principal \"`dfx canister id callee`\")"
26
+
dfx start --background --clean && dfx deploy
41
27
```
42
28
43
-
##Step 4: Invoke sequential and parallel calls
29
+
### 5. Setup the caller canister.
44
30
45
-
Let's first call the different endpoints of the `caller` canister using `dfx`
31
+
Provide the ID of the callee to the caller, such that the caller can initiate calls:
46
32
47
-
```bash
48
-
dfx canister call caller sequential_calls 100
49
33
```
50
-
51
-
This should output:
52
-
```bash
53
-
(100 : nat64)
34
+
dfx canister call caller setup_callee "(principal \"`dfx canister id callee`\")"
54
35
```
55
36
56
-
And the other endpoint:
37
+
#### 6. Invoke sequential and parallel calls
57
38
58
39
```bash
40
+
dfx canister call caller sequential_calls 100
59
41
dfx canister call caller parallel_calls 100
60
42
```
61
43
62
-
which outputs:
63
-
64
-
```bash
65
-
(100 : nat64)
66
-
```
67
-
68
44
The results are identical: all calls succeed. There also isn't a large difference in performance between these calls:
69
45
70
46
```bash
@@ -78,7 +54,7 @@ dfx canister call caller parallel_calls 100 0.11s user 0.03s system 8% cpu 1.72
78
54
79
55
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.
80
56
81
-
However, once we increase the number of calls, we observe a difference in both the results and performance.
57
+
However, once you increase the number of calls, you will observe a difference in both the results and performance.
82
58
83
59
```bash
84
60
time dfx canister call caller sequential_calls 2000
@@ -93,7 +69,7 @@ All the sequential calls succeed, but most parallel calls fail. The reason is th
93
69
94
70
Lastly, the parallel calls here complete sooner -- because most of them fail!
95
71
96
-
## Step 5: Multi-subnet setting
72
+
## Multi-subnet setting
97
73
98
74
Parallel calls are a lot more useful in multi-subnet settings. We can create such a setting locally using Pocket IC.
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.
89
+
90
+
## Security considerations and best practices
91
+
92
+
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