Thinking about my next ruby-on-icp build...What is a canister?

Greetings folks.

As per yesterday’s post, I now have ruby running in a canister.

It has me thinking starting to think about how to best conceptualize a canister.

I’m still getting used to wasm runtimes, alongside the specifics of ICP.

As I currently understand it, a canister will:
a. boot an app
b. allow the app to write memory to its heap/stable structures
c. kill the app’s process if it didn’t exit itself within the instruction count limit

Setting aside IC for a moment, If I pack a ruby.wasm, and then run wasmtime run packed-app.wasm -e 'puts 1', it will take 8 to 10 seconds to return on my machine on the first pass. Then maybe 800ms on second pass:

time wasmtime run ../ruby_wasm_builder/packed-app.wasm -e 'puts 1'
1

real	0m0.368s
user	0m0.167s
sys	    0m0.202s

which is about 4x larger than running native:

 time ruby -e 'puts 1'
1

real	0m0.110s
user	0m0.068s
sys	    0m0.030s

I don’t know where that difference comes from yet but wasmtime seems to have a ‘cold-boot’ and a ‘warm-boot’ time.

Now, when I look to my demo. I’m seeing maybe 2-4s delay before the frontend gets its response from the backend. I’m going to naively call this analogous to the ‘warm boot’ of a non-IC wasm run locally.

In my opening post, I was interested to learn about how the consensus mechanism limits the instruction to 40b per-request (in a request-response scenario).

I’ve now read the articles about the rust-cdk and stable structures. I haven’t tried yet but I suspect that I’ll able to pull rust-cdk into my buildsystem and drive it alongside wasi-sdk to produce future builds.

I also understand that the stable structures have lots of space available. The 10Mb limit almost reads like a bootstrapping step (to my interpreted language point of reference) if the memory and storage can persist between consensus cycles. What if I just load my app code into that stable structure?

In that case, I can perhaps limit the scope of my future builds to wiring the interpreter into all IC system specific calls, then on getting the interpreter’s warm-boot time down.

Even canister specific calls could perhaps just be a gem (package) of wrapped functions?

How does that mesh with what people have been doing for web-servers to date? I saw that the quickstarts show front-end frameworks. So are people even running webservers on the back end? or doing something more akin to direct db calls?

Cheers

You can run a web server, but keep in mind that all update calls(if the web call causes something to change on the canister…even just incrementing a view count) you are going to go through consensus and have a 2-4 second serving time - and maybe longer as each http_request actually has to be upgraded via the http_request_upgrade pathway. So most people only serve static HTML because that can be done with a query, which can come back much more quickly, and then use the loaded agent to call the IC and have it process the request with the page already being shown to the user, so they feel like the app is responsive.

So if you are really going to try to run ruby on a canister you’re likely going to want to just serve static html and have an api that can have a bit more grace for updates. That is going to eliminate running many existing Ruby web apps because 1. they aren’t going to be architected like that, and 2. they are eventually going to do something that runs for a long time and fail due to the cycle limit.

Interesting. So the 2-4 seconds delay I’m seeing is actually a consensus round triggered by the update..

So what happens when multiple users visit and trigger a counter increment in the same 2 to 4 seconds?

There would need to be some kind of lock-and-queue system in place to ensure that requests aren’t dropped. I’m going to guess that the canisters handle this, and then pass the stack of queued requests to the backend to complete and respond to every 2 to 4 seconds?

which agent is this?

so far, this doesn’t sound like it presents any new blockers for vanilla rails (beyond the current wasm constraints around websockets, etc). But you may be right about certain long running processes that I’m not aware of

This is the much more significant constraint from a UX perspective and has nothing to do with language choice. I’m sure I’ll be able to concoct a demo use case where 2-4s lag is tolerable. But for a huge section of modern app development, this would be immediately disqualified.

I’m sure you are well aware of this. Are you building fully on-chain apps or just offloading specific data and workflows to ic canisters?

The dfinity agent - https://github.com/dfinity/icp-js-core - This is what your web app uses to call IC canisters and the functions they host.

Most web apps already do a form of “optimistic updates”. If you submit a comment, show that it was successful…if you get an error, only then change the UI to show an error. You make it “feel fast”. Basically your UI code needs to know how to make something look like it happened even if it hasn’t happened yet.

If you are just reading data…just read it from queries and not from update calls…they should still be fast.