Skip to main content

Composite queries

Advanced
Tutorial

Overview

The Internet Computer Protocol supports two types of messages: updates and queries. An update message is executed on all nodes and persists canister state changes. A query message discards state changes and typically executes on a single node. It is possible to execute a query message as an update. In such a case, the query still discards the state changes, but the execution happens on all nodes and the result of execution goes through consensus. This “query-as-update” execution mode is also known as replicated query.

An update can call other updates and queries. However a query cannot make any calls, which can hinder development of scalable decentralized applications, especially those that shard data across multiple canisters.

Composite queries solve this problem. You can add composite queries to your canister using the following annotations:

  • Candid: composite_query
  • Motoko: composite query
  • Rust: #[query(composite = true)]

Users and the client-side JavaScript code can invoke a composite query endpoint of a canister using the same query URL for existing regular queries. In contrast to regular queries, a composite query can call other composite and regular queries. Due to limitations of the current implementation, composite queries have two restrictions:

QueryUpdateComposite query
Cannot call other queries or composite queriesCan call other updates and queries ; Cannot call composite queriesCan call other queries and composite queries
Can be called as an updateCannot be called as a queryCannot be called as an update
Can call canisters on another subnetCan call canisters on another subnetCannot call canisters on another subnet

Composite queries were enabled in the following releases:

Platform / LanguageVersion
Internet computer mainnetRelease 7742d96ddd30aa6b607c9d2d4093a7b714f5b25b
Candid2023-06-30 (Rust 0.9.0)
Motoko0.9.4, revision: 2d9902f
Rust0.6.8

Sample code

As an example, consider a partitioned key-value store, where a single frontend does the following for a put and get call:

  • First, determines the ID of the data partition canister that holds the value with the given key.
  • Then, makes a call into the get or put function of that canister and parses the result.
import Debug "mo:base/Debug";
import Array "mo:base/Array";
import Cycles "mo:base/ExperimentalCycles";
import Buckets "Buckets";

actor Map {

let n = 4; // number of buckets

// divide initial balance amongst self and buckets
let cycleShare = Cycles.balance() / (n + 1);

type Key = Nat;
type Value = Text;

type Bucket = Buckets.Bucket;

let buckets : [var ?Bucket] = Array.init(n, null);

public func getUpdate(k : Key) : async ?Value {
switch (buckets[k % n]) {
case null null;
case (?bucket) await bucket.get(k);
};
};

public composite query func get(k : Key) : async ?Value {
switch (buckets[k % n]) {
case null null;
case (?bucket) await bucket.get(k);
};
};

public func put(k : Key, v : Value) : async () {
let i = k % n;
let bucket = switch (buckets[i]) {
case null {
// provision next send, i.e. Bucket(n, i), with cycles
Cycles.add(cycleShare);
let b = await Buckets.Bucket(n, i); // dynamically install a new Bucket
buckets[i] := ?b;
b;
};
case (?bucket) bucket;
};
await bucket.put(k, v);
};

public func test() : async () {
var i = 0;
while (i < 16) {
let t = debug_show(i);
assert (null == (await getUpdate(i)));
Debug.print("putting: " # debug_show(i, t));
await Map.put(i, t);
assert (?t == (await getUpdate(i)));
i += 1;
};
};

};

Resources

The following example canisters demonstrate how to use composite queries:

Feedback and suggestions can be contributed on the forum here: https://forum.dfinity.org/t/proposal-composite-queries/15979