# Testing persistent context

**URL:** <https://discourse.nodered.org/t/testing-persistent-context/29679>\
**Category:** General\
**Created:** [7 July 2020 19:10 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679 "2020-07-07T19:10:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![davej](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davej/32/25150_2.png) [@davej](https://discourse.nodered.org/u/davej)\
**Post date:** [7 July 2020 19:10 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/1 "2020-07-07T19:10:59Z")

</div>

I am trying out Context to save variables across a reboot.  
It is working fine and I have the timer set at the default 30 seconds.

However, as a test, I replaced a value with the identical value and then did a flow.set(xxxx,"storeInFile") and I am finding that it has resaved the file (as shown by a date -r json.flow) despite there not actually being a change in the contents.  
I was under the belief that the file was only rewritten back to the SD card if there is actually a change to write?

Or have I perhaps misunderstood?

---

<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 July 2020 19:26 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/2 "2020-07-07T19:26:33Z")

</div>

> [@davej](#):
>
> and I am finding that it has resaved the file (as shown by a date -r json.flow) despite there not actually being a change in the contents.

But you _did_ call flow.set. I don't think in the current implementation the store checks the value to see if it has changed. Just calling flow.set is enough to mark it as needing to flush a write out.

---

<div class="post-metadata">

**Author:** ![davej](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davej/32/25150_2.png) [@davej](https://discourse.nodered.org/u/davej)\
**Post date:** [7 July 2020 19:58 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/3 "2020-07-07T19:58:32Z")

</div>

Yes I did call flow.set as I wasn't thinking that I had to check for data changes.  
If I don't do the flow.set then I am not persisting data across a reboot?

I must have got the wrong end of the stick.  
It looks like I will have to do a flow.get, then compare the retrieved value with my new value and only do a flow.set if different..

---

<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:** [7 July 2020 20:04 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/4 "2020-07-07T20:04:18Z")

</div>

is it so bad to rewrite it ?

---

<div class="post-metadata">

**Author:** ![davej](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davej/32/25150_2.png) [@davej](https://discourse.nodered.org/u/davej)\
**Post date:** [7 July 2020 20:22 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/5 "2020-07-07T20:22:41Z")

</div>

That might be a bit drastic.  
I know the default rewrite time is 30 seconds and as Nick said, it was with the intent of minimising rewrites back to a SD card.  
Maybe I was then expecting too much and putting 2 and 2 together in thinking that it would only rewrite actual changes for the same reason of limiting writes to the SD card.

---

<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:** [7 July 2020 20:31 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/6 "2020-07-07T20:31:57Z")

</div>

well I guess it could be optimised further, but then you have to balance the performance impact of checking every item to write vs the write wear. (I have no idea how to compare those apples vs oranges)

---

<div class="post-metadata">

**Author:** ![davej](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davej/32/25150_2.png) [@davej](https://discourse.nodered.org/u/davej)\
**Post date:** [7 July 2020 20:36 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/7 "2020-07-07T20:36:02Z")

</div>

Yes agreed. It's my OCD kicking in! 🙂 I can manage it myself now I know!

---

<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 July 2020 20:36 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/8 "2020-07-07T20:36:47Z")

</div>

Just to add - it would be trivial to do the check if we're checking discrete values - such as a String, boolean or number. And I suspect there's some low-hanging optimisation-fruit to be had with those cases.

It gets harder when the thing being stored is an Object - which can arbitrary depth and complexity. That's the case that would be relatively expensive to check for changes.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 September 2020 20:36 UTC](https://discourse.nodered.org/t/testing-persistent-context/29679/9 "2020-09-05T20:36:50Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
