Skip to content

Commit 3aff670

Browse files
add: Deploy to ICP Ninja + standardize README (#1182)
1 parent af04aa5 commit 3aff670

File tree

1 file changed

+17
-44
lines changed

1 file changed

+17
-44
lines changed

motoko/parallel_calls/README.md

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,42 @@
22

33
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.
44

5-
## Architecture
6-
75
The sample code revolves around two simple canisters, `caller` and `callee`. `Caller` has three endpoints:
86
1. `setup_callee`, to set the ID of the callee canister.
97
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.
108

119
The callee exposes a simple `ping` endpoint that takes no parameters and returns nothing.
1210

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`
18-
19-
Begin by opening a terminal window.
11+
## Deploying from ICP Ninja
2012

21-
## Step 1: Setup the project environment
13+
[![](https://icp.ninja/assets/open.svg)](https://icp.ninja/editor?g=https://github.com/dfinity/examples/tree/master/motoko/parallel_calls)
2214

23-
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
2916

30-
## Step 2: Deploy the canisters
17+
### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)
3118

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/)
3520

36-
## Step 3: Set up the caller canister
21+
### 3. Navigate into the project's directory.
3722

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:
3924

4025
```
41-
dfx canister call caller setup_callee "(principal \"`dfx canister id callee`\")"
26+
dfx start --background --clean && dfx deploy
4227
```
4328

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`:
4730

4831
```bash
4932
dfx canister call caller sequential_calls 100
5033
```
5134

52-
This should output:
53-
```bash
54-
(100 : nat64)
55-
```
56-
5735
And the other endpoint:
5836

5937
```bash
6038
dfx canister call caller parallel_calls 100
6139
```
6240

63-
This should output:
64-
65-
```bash
66-
(100 : nat64)
67-
```
68-
6941
The results are identical: all calls succeed. There also isn't a large difference in performance between these calls:
7042

7143
```bash
@@ -79,7 +51,7 @@ dfx canister call caller parallel_calls 100 0.11s user 0.03s system 8% cpu 1.72
7951

8052
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.
8153

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.
8355

8456
```bash
8557
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
9264

9365
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.
9466

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:
10068

10169
First, follow the [installation instructions](https://github.com/dfinity/pocketic) to install `pocket-ic` in the `parallel_calls` directory.
10270

@@ -110,4 +78,9 @@ Sequential calls: 90/90 successful calls in 599.863583ms
11078
Parallel calls: 90/90 successful calls in 296.402ms
11179
```
11280

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.
86+

0 commit comments

Comments
 (0)