# Saving context variable to the localfilesystem immediately on change

**URL:** <https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515>\
**Category:** General\
**Created:** [18 June 2020 03:57 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515 "2020-06-18T03:57:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kushan-athukorala](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kushan-athukorala/32/21830_2.png) [@kushan-athukorala](https://discourse.nodered.org/u/kushan-athukorala)\
**Post date:** [18 June 2020 03:57 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/1 "2020-06-18T03:57:43Z")

</div>

Hi,

I have my node-red flow configured to use localfilesystem as contextStorage. I know that for every 30 seconds context variables are saved to the file system.

Is there a way to save these immediately to the file system as and when they are created,updated or deleted?

Regards,  
Kushan

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [18 June 2020 06:07 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/2 "2020-06-18T06:07:40Z")

</div>

You can configure the flush time as per the docs.. But I have to ask why ?

---

<div class="post-metadata">

**Author:** ![kushan-athukorala](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kushan-athukorala/32/21830_2.png) [@kushan-athukorala](https://discourse.nodered.org/u/kushan-athukorala)\
**Post date:** [18 June 2020 06:37 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/3 "2020-06-18T06:37:25Z")

</div>

Yes I understand that the flush time can be configured. But my need is to preserve context data in case of sudden restart of the server.

---

<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:** [18 June 2020 07:54 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/4 "2020-06-18T07:54:47Z")

</div>

> [@kushan-athukorala](#):
>
> But my need is to preserve context data in case of sudden restart of the server

However rapidly you attempt to update it it can never be instantaneous, there will always be a window after it is changed before it is saved to disk during which the power may fail. That is true even if there were a flag you could set that said that immediately after updating a variable it is to be flushed to disc. The power could still fail in the gap between update and flush.

---

<div class="post-metadata">

**Author:** ![kushan-athukorala](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kushan-athukorala/32/21830_2.png) [@kushan-athukorala](https://discourse.nodered.org/u/kushan-athukorala)\
**Post date:** [18 June 2020 08:13 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/5 "2020-06-18T08:13:07Z")

</div>

I am ok to loose data for a second or two and I don't want the flush interval to be too short as well. A flag or some other way for immediate update is what I am looking for.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [18 June 2020 08:21 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/6 "2020-06-18T08:21:47Z")

</div>

Again as per the docs you can also set cache to false which should turn it off.

- [https://nodered.org/docs/api/context/store/localfilesystem](https://nodered.org/docs/api/context/store/localfilesystem)

---

<div class="post-metadata">

**Author:** ![kushan-athukorala](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kushan-athukorala/32/21830_2.png) [@kushan-athukorala](https://discourse.nodered.org/u/kushan-athukorala)\
**Post date:** [18 June 2020 08:25 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/7 "2020-06-18T08:25:26Z")

</div>

Oh really, I didn't noticed that. That is the solution.  
Great thanks.

---

<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:** [18 June 2020 08:27 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/8 "2020-06-18T08:27:53Z")

</div>

Just be aware that if you turn off the cache, the store becomes a _asynchronous_ store, which means you cannot do:

```auto
var a = flow.get("foo");

```

You have to use the version with a callback function:

```auto
flow.get("foo", function(err, a) {
});
```

---

<div class="post-metadata">

**Author:** ![kushan-athukorala](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kushan-athukorala/32/21830_2.png) [@kushan-athukorala](https://discourse.nodered.org/u/kushan-athukorala)\
**Post date:** [18 June 2020 08:43 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/9 "2020-06-18T08:43:17Z")

</div>

Thank you for your valuable advice for saving my time. I was just noticing breaking my flow after cache=false.

Hope it affects to change nodes too.

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [18 June 2020 08:47 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/10 "2020-06-18T08:47:15Z")

</div>

Now I am a bit confused, if you turn of the cache it gets a synchronous store? You mean asynchronous?  
And shouldn't the callback for the `get` have at least two parameters, because the first one is the error?

---

<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:** [18 June 2020 08:56 UTC](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/11 "2020-06-18T08:56:04Z")

</div>

> [@cinhcet](#):
>
> Now I am a bit confused,

Yup - you are quite right, will edit my post. Need more tea.
