# Questions about persistent context

**URL:** https://discourse.nodered.org/t/questions-about-persistent-context/5547
**Category:** Developing Nodes
**Created:** [7 December 2018 00:13 UTC](https://discourse.nodered.org/t/questions-about-persistent-context/5547 "2018-12-07T00:13:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [7 December 2018 00:13 UTC](https://discourse.nodered.org/t/questions-about-persistent-context/5547/1 "2018-12-07T00:13:35Z")

</div>

I want to use persistent context in one of my contributed nodes, but I have a few questions about how it works.

First, when using the `localfilesystem` module, the documentation says the default `flushInterval` is 30 seconds. (Good for the health of SD cards.) Does this mean that the entire cache is flushed every 30 seconds, regardless of whether any of its contents have changed? It would be nice (at least for testing with small amounts of data) to be able to set the interval much shorter without having to worry.

Second, am I right to assume that "flush" here just means refreshing the filesystem copy, so that the copy in memory continues to be used (unless `cache` is `false`)?

Third, is there a way to force a flush from memory to "disk", so that the persistent copy is guaranteed to be up to date?

Finally, I want to assume that NR flushes the cache when it shuts down. Is that correct?

Sorry to be a bother, but trial-and-error has taken me only so far.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [7 December 2018 10:44 UTC](https://discourse.nodered.org/t/questions-about-persistent-context/5547/2 "2018-12-07T10:44:14Z")

</div>

> [@drmibell](#):
>
> I want to use persistent context in one of my contributed nodes, but I have a few questions about how it works.

To begin with, as a node author, you just use Context. You don't get to pick if its persistent or not as that is up to the end user.

> [@drmibell](#):
>
> Does this mean that the entire cache is flushed every 30 seconds, regardless of whether any of its contents have changed?

No. It only flushes when there are changes to flush, but will ensures it doesn't flush any more frequently than the configured interval.

> [@drmibell](#):
>
> Second, am I right to assume that "flush" here just means refreshing the filesystem copy, so that the copy in memory continues to be used (unless `cache` is `false` )?

Right.

> [@drmibell](#):
>
> Third, is there a way to force a flush from memory to "disk", so that the persistent copy is guaranteed to be up to date?

No. The context api only lets you get/set values - that is all. You don't know what instance of context plugin the user has configured.

> [@drmibell](#):
>
> Finally, I want to assume that NR flushes the cache when it shuts down. Is that correct?

Yes. Assuming NR is shutdown cleanly, then the filesystem context plugin flushes its contents as part of its closing down. If NR is abruptly halted then any unflushed data will be lost.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [7 December 2018 11:13 UTC](https://discourse.nodered.org/t/questions-about-persistent-context/5547/3 "2018-12-07T11:13:09Z")

</div>

> [@knolleary](#):
>
> To begin with, as a node author, you just use Context. You don't get to pick if its persistent or not as that is up to the end user.

The node in question is [node-red-simple-gate](https://flows.nodered.org/node/node-red-contrib-simple-gate), and the aim is to persist the node's state in the event of a reboot/ node-RED restart, etc. So saving the state to memory would not be effective, it would need to be saved to disk, so that it could be re-loaded, to persist the gate's state.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [7 December 2018 11:15 UTC](https://discourse.nodered.org/t/questions-about-persistent-context/5547/4 "2018-12-07T11:15:37Z")

</div>

That doesn't change the fact that a node just uses context and it is up to the end user to choose how that is configured and whether it will be persistent or not.

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [7 December 2018 15:08 UTC](https://discourse.nodered.org/t/questions-about-persistent-context/5547/5 "2018-12-07T15:08:39Z")

</div>

@knolleary Thanks for the information. I think that tells me what I need to know.

> [@knolleary](#):
>
> a node just uses context and it is up to the end user to choose how that is configured and whether it will be persistent or not

Understood. As @Paul-Reed says, the immediate question involves his [issue](https://github.com/drmibell/node-red-contrib-simple-gate/issues/3) with `node-red-contrib-simple-gate`. The goal is not to guarantee the user any particular kind of persistence but to give him the option of using it if available. The node itself will have to determine whether it can get what it needs from the store or should do something else.
