# Cycle burn rate heartbeat

**URL:** <https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090>\
**Category:** Developers\
**Created:** [April 12, 2022, 2:19pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090 "2022-04-12T14:19:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [April 12, 2022, 2:19pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/1 "2022-04-12T14:19:19Z")

</div>

👋

Can somebody explain why the heartbeat consumes so much cycles, even when the function body is empty it burns through `3_000_000` on every beat.

Just a explainer what i am trying to do / how i set it up;

I am trying to set up self-sustaining canisters-system where each canister can request cycles from a management canister if their current cycles are below a certain threshold.

I first implemented a heartbeat in every canister, but after seeing what they burned i moved all the logic over to the management canister.

In the management canister i loop over all the canisters and do a inter-canister call to see if they need to be topped-up, these inter-canister calls consume about `1_000_000_000` cycles each and are checked every 24 hours.

```auto
#[heartbeat]
async fn check_canister_cycles()
  let day = 1 * 24 * 60 * 60 * 1_000_000_000;
  if time() > last_triggered_time + day { 
    // do calls
  }
}

```

So i’m kind of trying to find the sweet-spot, when managing the heartbeat in the management canister the inter-canister calls consume a lot of cycles, which would increase by every canister that is added dynamically. But when doing it in the canisters themselves it consumes `3_000_000` every beat but doesnt require a inter-canister call until it reaches the threshold.

Is there maybe a other(better) way to handle this? Maybe something that gets triggered once a call fails because it doesnt have enough cycles to handle the request?

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [April 12, 2022, 8:24pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/2 "2022-04-12T20:24:18Z")

</div>

I wonder if the heartbeat _needs_ to consume so many cycles, or whether it is a side effect of the particular implementation. I’ve heard this [complaint](https://forum.dfinity.org/t/why-does-a-canister-keep-consuming-cycles/10821/7) of high cycle consumption quite a number of times.

@PaulLiu, do you happen to know? (Sorry for the ping, but I believe you may have implemented the heartbeat in Motoko. Can you tag the person who implemented the heartbeat in the replica?)

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [April 12, 2022, 9:05pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/3 "2022-04-12T21:05:12Z")

</div>

I wonder if just having some kind of “manual trigger” mechanism in place instead of heartbeat for the apps that don’t need checks so often would be a viable way of going around of using heartbeat for “cron like” things that need to be occasionally done inside of the canister?  
ie: have a method that does not trigger on a heartbeat, but instead it’s a regular method that only allows certain caller to trigger it (an admin for example) and it then does the same thing a heartbeat will. It can then be perhaps done manually by whoever needs to do it and however often it needs to be called, or some kind of a web2 app could be created just for the sakes of calling the canister method that acts as a hearbeat every so often?

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [April 12, 2022, 9:13pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/4 "2022-04-12T21:13:09Z")

</div>

Yeah I’m thinking of going that route if necessary. I would prefer to have everything done on the Internet Computer though…

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [April 12, 2022, 9:42pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/5 "2022-04-12T21:42:46Z")

</div>

Yeah creating a webworker on digital ocean for example also crossed my mind, but as @jzxchiang mentioned, i would rather do it al in a decentralized matter on the IC.

It would be nice if we could only trigger the heartbeat call every x amount of beats for example. That would drastically reduce the cycles spent. And for a functionality like checking and updating the cycles it isnt really bound to precise timing.

so something like  
`#[heartbeat(trigger_at = 60)]`

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [April 12, 2022, 10:16pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/6 "2022-04-12T22:16:36Z")

</div>

Tbh I see no issue with “not going the IC way” for this specific thing as it does not, in any way, defeat the purpose of the blockchain in the first place. The things that should be decentralized should be added into the blockchain, but things like these, imo are not that important as they are not the source of truth or data itself that needs to live and be 100% legit and validated by nodes.

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [April 12, 2022, 10:18pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/7 "2022-04-12T22:18:38Z")

</div>

There is a way to trigger something only on a specific amount of beats in a way… You can find the example that does that exact thing in the documentation. I will paste the code here for reference.

```auto
import Debug "mo:base/Debug";

actor Alarm {

  let n = 5;
  var count = 0;

  public shared func ring() : async () {
    Debug.print("Ring!");
  };

  system func heartbeat() : async () {
    if (count % n == 0) {
      await ring();
    };
    count += 1;
  }
}

```

This code would run on every 5 heartbeats. So on 5th, 10th, 15th etc

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [April 12, 2022, 11:03pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/8 "2022-04-12T23:03:10Z")

</div>

yeah your example is exactly the same as in the message that started this thread, only i used `time` instead of a counter to prevent updating the state. But this still triggers the heartbeat and burns cycles.

So to take your example

```auto
system func heartbeat() : async () {}

```

This empty function still burns cycles every time the heartbeat is triggered

So the counter should be on higher level then the heartbeat itself.

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [April 12, 2022, 11:06pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/9 "2022-04-12T23:06:30Z")

</div>

Oh I see 🙂 Makes no sense to burn it every time if it’s not used at all 🙂 Yeah… I guess the only way around this rn would be to do what I mentioned earlier perhaps and go around heartbeat completely.

---

<div class="post-metadata">

**Author:** ![PaulLiu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulliu/32/4701_2.png) [@PaulLiu](https://forum.dfinity.org/u/PaulLiu)\
**Post date:** [April 13, 2022, 3:30am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/10 "2022-04-13T03:30:54Z")

</div>

Just waking up your canister every second is work, actually a lot of work. It is not heartbeat that is charging more, but try sending your canister 1 message every block, not cheap either!

For more flexible scheduling, you could use something like [IC Cron](https://forum.dfinity.org/t/ic-cron-lets-schedule-some-tasks-bois/6506). I’m not sure if @senior.joinu (author of IC Cron) is offering a deployed IC Cron canister as a public service for everyone. But as a community, if enough people find it useful, you can collectively fund such a public service (e.g. using [tip jar](https://forum.dfinity.org/t/worry-about-your-favorate-canister-running-low-on-cycles-you-got-it-covered/10307)) in an open and transparent way that benefits everyone.

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [April 13, 2022, 5:20am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/11 "2022-04-13T05:20:51Z")

</div>

If you have occasional update calls to your canister, you could also check your balance during these calls, and call your management canister to top up if your balance is below a threshold.

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [April 13, 2022, 5:43am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/12 "2022-04-13T05:43:11Z")

</div>

This could be a useful mixed, i.e. legacy and IC, infrastructure service that canisters on the IC could use.

The service consists of a canister that can be paid to watch the balance and top up other canisters and a simple cloud function that checks the cycles of the registered canisters.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [April 13, 2022, 6:00am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/13 "2022-04-13T06:00:16Z")

</div>

It definitely takes some work, but the proportions just seem off.

0.5 TC a day works out to almost $0.70 per day, which is roughly $21 per month or $252 per year.

That means `heartbeat` (a simple version) is 50x more expensive than storing 1 GB of data for a year on the IC.

I definitely agree that a public heartbeat canister that anyone can subscribe to and whose cycle costs are split among its subscribers would be very useful, but I don’t think the community will be able to organize around such a service in the near future…

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [April 13, 2022, 6:56am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/14 "2022-04-13T06:56:51Z")

</div>

Yeah also thought of this, but issue is that my implementation is for a self-scaling storage solution, so for example;  
there is a storage canister, once it reaches a certain memory threshold it locks up for any further update calls and spins up a “brother” canister. So eventually the update calls will stop once the canister is full.

I think the only way your solution would work is to make every query call an update call, or can a query call internally call an update call? 🤔

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [April 13, 2022, 7:03am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/15 "2022-04-13T07:03:56Z")

</div>

> so eventually the update calls will stop once the canister is full.

Ah, I see.

> or can a query call internally call an update call?

No, it can’t do any inter-canister call.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [April 13, 2022, 7:11am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/16 "2022-04-13T07:11:20Z")

</div>

Since inter-canister update calls take awhile, I wonder if there’s a hacky solution in here where you can set up a canister to recursively call other canisters, and only repeat the inter-canister call once the calling canister has received a response (2+ sec).

This “heartbeat” wouldn’t be as predictable in terms of timing, but would most likely be less expensive.

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [April 13, 2022, 7:32am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/17 "2022-04-13T07:32:06Z")

</div>

haha this sound like worth trying, but my assumption is that it would be pretty expensive.

If i understand you correctly it would be something like

```auto
--storage canister--
fn get_cycles() {
  storage_management_canister_request_cycles().await
} 

--storage management canister--
fn request_cycles() {
   storage_canister_get_cycles().await // based on caller
}

```

And then maybe work with some kind of delay solution

---

<div class="post-metadata">

**Author:** ![Maxfinity](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/maxfinity/32/1484_2.png) [@Maxfinity](https://forum.dfinity.org/u/Maxfinity)\
**Post date:** [April 13, 2022, 9:05pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/18 "2022-04-13T21:05:01Z")

</div>

> [@icme](#):
>
> Since inter-canister update calls take awhile, I wonder if there’s a hacky solution in here where you can set up a canister to recursively call other canisters, and only repeat the inter-canister call once the calling canister has received a response (2+ sec).

Yes, or even self-calls although that is less safe - [IC Cron - let's schedule some tasks bois - #3 by nomeata](https://forum.dfinity.org/t/ic-cron-lets-schedule-some-tasks-bois/6506/3)

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [April 16, 2022, 10:13am UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/19 "2022-04-16T10:13:59Z")

</div>

> [@PaulLiu](#):
>
> It is not heartbeat that is charging more, but try sending your canister 1 message every block, not cheap either!

It’s way cheaper to do this instead of using heartbeat. I moved to running a script on a server that calls an upgrade method on my canister every 2 seconds because of costs. It would be nice if we could reduce heartbeat costs to make it as cheap as calling the canister from the “outside”.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [April 16, 2022, 2:09pm UTC](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090/20 "2022-04-16T14:09:12Z")

</div>

I have this hacky system I’m my ICDevs bounty roadmap. A dao that owns this “scheduler” public utility would be a fun project.

Maybe I’ll code it up for supernova.

Interesting problem and needed data:

How many fire and forget intercanister calls can fit into one round of consensus?

Do we need guaranteed delivery? Rouge canisters probably break in line processing of returns….would need to use @nomeata ’s upgrade pattern.

How do the economic play out? How much for 10,000 notifications, etc.

[Next page](https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090.md?page=2)
