# Motoko 🧬 Module+ Easily upgradable stable memory modules standard

**URL:** <https://forum.dfinity.org/t/motoko-module-easily-upgradable-stable-memory-modules-standard/36478>\
**Category:** Developers\
**Created:** [October 23, 2024, 6:41pm UTC](https://forum.dfinity.org/t/motoko-module-easily-upgradable-stable-memory-modules-standard/36478 "2024-10-23T18:41:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![infu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/infu/32/11321_2.png) [@infu](https://forum.dfinity.org/u/infu)\
**Post date:** [October 23, 2024, 6:41pm UTC](https://forum.dfinity.org/t/motoko-module-easily-upgradable-stable-memory-modules-standard/36478/1 "2024-10-23T18:41:52Z")

</div>

While the Motoko language provides a way for us to upgrade our stable memory, for a long time it seemed like something was missing. Developers put third-party modules like BTrees, HashMaps, HTTP servers, ICRC3 Logging, Databases, etc, but then each module provides its own upgrade system or doesn’t provide any at all while leaving it to the developers using these to figure things out.

We came up with the following standard which if adopted by library developers will improve developer experience a lot.

There seem to be two types of upgrades.

**1. Module types upgrade** - A new version of the module needs to upgrade its stable memory. For example, If this is an ICRC3 logging library and it needs to change its data structure. Developers can also write their canisters as multiple modules and changes in their memory will also be such.

**2. Custom types upgrade** - The module version stays the same, but users need to change their types. For example when we make changes in these types HashMap\<Principal, Profile\>

Module+ is expanding on this pattern which we called Class+ [Writing Motoko stable libraries](https://forum.dfinity.org/t/writing-motoko-stable-libraries/21201)

Let’s start with how things will look from the perspective of a developer using Module+ libraries.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/a/6/a626a15caff01c796788be43ca6e36dece866340.png)

Notice that we are not doing

```auto
let mem_avatars_1 = HashMap.new<Text, Nat>();

```

If we do that, whenever the HashMap library changes version and its stable memory type is different, our mem\_avatars\_1 will have a different type. We need to be able to fix the type no matter how the library changed and that’s why we’ve added (.Mem.HashMap.V1) which is part of the standard.

Let’s see what happens if the HashMap library needs a **‘Module types upgrade’**

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/8/f/8fd8e5112d40db3df4f3957d9ad270c24c111240.png)

It’s a one-step upgrade process in which mem\_avatars\_1 will be upgraded to mem\_avatars\_2 and then mem\_avatars\_1 will be emptied.  
The class avatars has to now point to the new memory.  
If we forget to remove this code in consequent upgrades, nothing bad will happen, it will keep working and wont do any additional upgrading.  
We can clean it up by removing mem\_avatars\_1 and changing upgrade to new  
 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/1/f/1f10fdd326dd27194317dee0edb807c2a12b195b.png)

Now let’s perform a **“Custom types upgrade”**  
First we add mem\_avatars\_3 with the same module types version (V2)  
Then we execute ‘upgrade’ given us by the library developers.  
Its task is to upgrade mem\_avatars\_2, place it in mem\_avatars\_3 and delete the old memory.  
To do that it expects us to give it a custom function where we change the types of each item.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/6/9/6938db8791ddbfed3cf31fa2247d79b7b4ed185d.png)

If we forget about the code and upgrade the canister, again nothing bad will happen, no upgrades will run. We can clean it up by deleting the upgrade code  
 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/1/2/126807ad77a38d183b10574d6e406326b807ab09.png)

**If Module+ accepts custom types and stores them in memory, it has to provide such upgrade functions.**

Chaining upgrades is also possible. At the end of this mem\_avatars\_1 and 2 will be empty. Each version only provides upgrades from the previous one.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/0/c/0cf50c639d67aa7b1ba371c25ab6bd9e03071d51.png)

Nested stable memory module upgrades are also possible and a lot easier to do.

**Writing Module+**

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/e/1/e15140f6cd28afd2d7bf15d12559c676e0e409b0.png)

The module is required to provide “Mem” and then the stable memory types like “One” followed by their versions V1 and V2. It picks the last version and its class only works with it.

Let’s see V1

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/1/8/180d869c8727e47332397c167d165cab3f073c14.png)

The import line in production will be `import MU "mo:mosup"`  
(MO)took (S)table (UP)grades  
It is pretty simple and has helper functions and types.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/9/d/9d545c69663bb7c2ede8a38814da7464c7539b31.png)

The module’s memory is stored inside `{var inner : ?M}` and that’s how it can get deleted in a one-step process and provide protections when leaving/forgetting the upgrade code while doing additional canister upgrades.

This is memory V2  
We have added two more record fields ‘name’ and ‘age’.  
Now we also need to write an upgrade function from V1 to V2

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/0/4/04990b452ddcabb64b9d909cd69417dee4bd03d8.png)

One last thing to look at is how **“Custom types upgrade”** works.  
Inside the Hashmap module, we add upgrade functions like this one

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/8/1/816a11dee66e92c264203606ca5324f5ee8e29fd.png)

In a single canister, we typically include several mops libraries along with locally defined modules to organize the codebase. We’ll be using Module+ to manage all of these.

If library developers adopt such a standard, upgrading will be easier, safer and the developer experience much better.  
@skilesare @timo @anon74414410 @icme @nomeata @tomijaga @claudio @luc-blaeser

**Examples here:**  
[[mosup/mo/entry.mo at master · Neutrinomic/mosup · GitHub](https://github.com/Neutrinomic/mosup/blob/master/mo/entry.mo)]

It looks like the Motoko language team is also working on a language feature that will simplify it a bit and this standard will adapt to it. The language feature however won’t free module developers from having to provide upgrade functions, so I believe it will mostly stay the same. The goal here is to not ask canister developers to write their own ‘module type upgrades’ and to simplify ‘custom type upgrades’

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [October 23, 2024, 9:27pm UTC](https://forum.dfinity.org/t/motoko-module-easily-upgradable-stable-memory-modules-standard/36478/2 "2024-10-23T21:27:13Z")

</div>

I like the end result here, although the syntax it requires is unfortunate
