# Problems understanding and implementing Persistent Storage

**URL:** <https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527>\
**Category:** General\
**Created:** [13 August 2023 01:09 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527 "2023-08-13T01:09:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 01:09 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/1 "2023-08-13T01:09:07Z")

</div>

I'm having trouble understanding how to achieve the following:  
I have a set of 15 global variables that I'd like to have restored after a system restart. I have the following section added to the settings,js file:

```auto
contextStorage: {	
   default: {	
      module: "localfilesystem",	
      config: {	
         cache: true,	
         flushInterval: 1800,	
      },	
   },	
},	

```

I watched the file system and I've seen the `global.json` file updating its timestamp every 30 minutes, so I assume the persistent store is being formed and updated.

When I have the digital Ocean droplet restart, and Node-Red is back up, none of the globals are available to a `var X = global.get("XYZ")`

It seems I am missing/misunderstanding something crucial here.  
Do I have these settings correct? Do I have to add a flow that runs on restart to somehow restore the globals?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2023 09:16 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/2 "2023-08-13T09:16:12Z")

</div>

See if this helps you. It's something I wrote awhile ago but still hold up.

> [@A guide to understanding 'Persistent Context'](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115):
>
> This is something I wrote up after playing with the context options. I've filed a PR with some of this added to the Node-RED documentation but thought I'd release it here also. I hope people find it useful. Paul A guide to understanding 'Persistent Context' Prior to Node-RED v0.19.0, data could be stored in context as a global, flow or node. This was stored in memory and would be reset with a restart of Node-RED. As of v.0.19.0 you can now store node, flow and global context data in memory O…

and the here is a link to the documentation:  
[https://nodered.org/docs/api/context/](https://nodered.org/docs/api/context/)

and also read this:  
[https://nodered.org/docs/api/context/store/localfilesystem](https://nodered.org/docs/api/context/store/localfilesystem)  
and pay attention to the `options!!!` (hint hint)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 August 2023 09:32 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/3 "2023-08-13T09:32:34Z")

</div>

> [@UmbraLimi](#):
>
> When I have the digital Ocean droplet restart, and Node-Red is back up, none of the globals are available

Does the droplet have persistent file storage? Do you lose flows upon restart too?

Check your settings.js file - are your settings still present?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2023 10:07 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/4 "2023-08-13T10:07:48Z")

</div>

One other thing, `flushInterval` is in seconds and you have:

> [@UmbraLimi](#):
>
> `flushInterval: 1800,`

1800 / 60 = 30 minutes.

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 10:11 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/5 "2023-08-13T10:11:07Z")

</div>

Yes, the droplet has persistent storage. On a start, the flows are all restored and I see the `global.json` file in the `...\.node-red\context\global` folder being updated for time every 30 minutes. So the saving is happening. What isn't happening is the restoring of the globals to memory

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 10:14 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/6 "2023-08-13T10:14:59Z")

</div>

Yes, the 30 minutes is a compromise, and perhaps a result of a lack of understanding. In my case, the globals get set once and then don't change unless a certain flow is manually started which updates all the globals. I'm using NR as a cache of sorts and a REST API for a NoCode front end.

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 10:25 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/7 "2023-08-13T10:25:06Z")

</div>

I have read (but perhaps not completely understood) all of your references, and they were what I used to come up with my solution. But it seems I am missing something still.

I saw this in your one post:

```auto
SUGGESTION: If you want have all your context data be persistant, setup your settings.js file with the following:

    contextStorage: {
    	default : { module: "localfilesystem"}
    },

```

Then I added the config options to allow for the cache.

Is there something amiss here? Sorry for being a bit unsure here.  
Or do I need to do a function that runs on startup to restore my globals?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2023 10:39 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/8 "2023-08-13T10:39:24Z")

</div>

Hmmm, you may have found a bug. I was testing with flow context and it works fine, but when I switched to global context I've been able to reproduce your error.

Let me play a bit more and see what I can find.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2023 10:50 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/9 "2023-08-13T10:50:49Z")

</div>

Of course now I can't recreate it. I may have deleted something between stoping and starting NR.

After stopping NR, can you open the global.json file in a editor and see what it contains.

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 11:04 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/10 "2023-08-13T11:04:28Z")

</div>

Interesting!

global.json contains only `{}`

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2023 11:09 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/11 "2023-08-13T11:09:31Z")

</div>

Check the time stamp of the global.flow and then start NR again and do something to create a global variable, In the NR sidebar, select the `contact data` tab and click the refresh button so you can see the global data.

Then check the global file again. If the data is there, stop NR and check it again.

I'm wondering it stopping the droplet is causing the global info to be destroyed.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 August 2023 11:11 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/12 "2023-08-13T11:11:10Z")

</div>

> [@UmbraLimi](#):
>
> global.json contains only `{}`

Was that after stopping node red, but before restarting it?

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 11:24 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/13 "2023-08-13T11:24:28Z")

</div>

It was after I stopped node-red with `sudo systemctl stop node-red` and confirmed it was stopped with `sudo systemctl status node-red`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 August 2023 11:31 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/14 "2023-08-13T11:31:05Z")

</div>

What does it contain before you stop node-red (but after long enough has elapsed to make sure it has been written to file).  
An unrelated point about the flush interval. It will only save it to file if the values have changed, so if they do not change often there is no harm in having a faster update rate.

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 12:25 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/15 "2023-08-13T12:25:20Z")

</div>

@zenofmud and @Colin

I have done a `global.set("Persons", global.get("Persons"))` at the end of global "creation" process and I am now waiting for the 30 mins to elapse to see if global.json was updated

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2023 12:59 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/16 "2023-08-13T12:59:26Z")

</div>

> [@UmbraLimi](#):
>
> global.set("Persons", global.get("Persons"))

Is that the exact code you use? Are you doing that in a function node? If so I'm not sure if that is going to work and if it does, it is just going to set 'Persons' with the value that is already in it.

Just what are you trying to accomplish?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 August 2023 13:05 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/17 "2023-08-13T13:05:27Z")

</div>

> [@zenofmud](#):
>
> it is just going to set 'Persons' with the value that is already in it.

I think it should also trigger the code that says something has (potentially) changed and so cause a context flush at the next timeout. I doubt if that logic checks whether anything has actually changed, though I have not checked the code.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 August 2023 13:51 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/18 "2023-08-13T13:51:41Z")

</div>

Also, since JavaScript passes objects by reference, if something "clears" that object out before the write occurs, it will write the changed value - depending on the life cycle of the variable, that may well be an empty object.

The test to make is - write something you know can never be externally affected e.g...

Function 1

```auto
const testObject= {
   prop1: 123
   prop2: "check me"
}
global.set('test_value', testObject) 

```

then reboot and test it...

Function 2

```auto
const testObject = global.get('test_value') 
node.warn([ 'What do you see', testObject)

```

Additionally, as said earlier, you should set the flush time to something sensible (e.g. 30 secs). Flush only occurs when there is something to write. I.e., never change a value, never write to global.

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 14:04 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/19 "2023-08-13T14:04:47Z")

</div>

> [@Problems understanding and implementing Persistent Storage](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/16):
>
> Is that the exact code you use? Are you doing that in a function node? If so I'm not sure if that is going to work and if it does, it is just going to set 'Persons' with the value that is already in it. Just what are you trying to accomplish?

Yes, this is a function node. In this case, I was trying to trigger the write-to-file function of node-red according to my incomplete knowledge of persistent storage, but not change anything. I've seen others use this, and not sure if it is a good practice, but I wanted to show you what I did

> [@Problems understanding and implementing Persistent Storage](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/17):
>
> I think it should also trigger the code that says something has (potentially) changed and so cause a context flush at the next timeout. I doubt if that logic checks whether anything has actually changed, though I have not checked the code.

Exactly. I only need this write operation to happen once and on demand. Thereafter, I will be reading only from the persistent storage, with memory being the preferred routine use, but from disk on restart

> [@Problems understanding and implementing Persistent Storage](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/18):
>
> Also, since JavaScript passes objects by reference, if something "clears" that object out before the write occurs, it will write the changed value - depending on the life cycle of the variable, that may well be an empty object. The test to make is - write something you know can never be externally affected e.g... Function 1 const testObject= { prop1: 123 prop2: "check me" } global.set('test\_value', testObject) then reboot and test it... Function 2 const testObject = global.get('test…

This is good information as well, and I will try this too.

Thanks to all three of you!

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [13 August 2023 14:09 UTC](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527/20 "2023-08-13T14:09:16Z")

</div>

And thanks @Colin, @Steve-Mcl for the workings of the update process. I have returned the `flushInterval` to 30 seconds

[Next page](https://discourse.nodered.org/t/problems-understanding-and-implementing-persistent-storage/80527.md?page=2)
