Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync cache between 2 processes/threads #296

Open
angrymouse opened this issue Jun 23, 2024 · 6 comments
Open

Sync cache between 2 processes/threads #296

angrymouse opened this issue Jun 23, 2024 · 6 comments

Comments

@angrymouse
Copy link

Is it possible to sync caches between 2 processes? So that if one of them does put, other will fetch new version (not the one cached in other process's memory).

@kriszyp
Copy link
Owner

kriszyp commented Jun 23, 2024

Yes, one of the powerful aspects of using memory maps is that there is only one cache, operating system's disk cache, and so when it is updated, all processes see the update.
The one hazard to be careful of, is that lmdb-js does reuse the current read transaction for the current event turn. When a read is performed, a read transaction will be created or reset, and subsequent reads will use the same read transaction and snapshot until an event queued with setImmediate resets it. Normally this is adequate, providing a view of the next data snapshot at the next event turn and keeping a consistent view of data within an event turn. However, if you want to force lmdb-js to reset the read transaction to guarantee it is reading the latest state, you can call db.resetReadTxn().

@angrymouse
Copy link
Author

So cache property on db will reuse the same cache even if I did open same db from 2 different processes? Not local per process?
Would be good to mention it in doc if so.

@kriszyp
Copy link
Owner

kriszyp commented Jun 24, 2024

Sorry, I should have realized that you meant that object cache (which the cache property provides). This object cache (a cache of the actual JS objects) is indeed specific to the process (or V8 context/worker). There actually is a way to do process synchronization with this case, you just set it up with:

open({
  cache: {
    validated: true
  },..

This validated cache will always check to see if the cached version matches the version in the database, and if it does, it will use the in-memory cache, otherwise it will reload from the db.
I will get this in docs too...

kriszyp added a commit that referenced this issue Jun 24, 2024
@angrymouse
Copy link
Author

angrymouse commented Jun 24, 2024

What happens when you do db.put() with cache set to {validated:true}?
Does db.get() in this case bring old value from db or new one from cache (but just not flushed to disk)?

@DraviaVemal
Copy link

Sorry, I should have realized that you meant that object cache (which the cache property provides). This object cache (a cache of the actual JS objects) is indeed specific to the process (or V8 context/worker). There actually is a way to do process synchronization with this case, you just set it up with:

open({
  cache: {
    validated: true
  },..

This validated cache will always check to see if the cached version matches the version in the database, and if it does, it will use the in-memory cache, otherwise it will reload from the db. I will get this in docs too...

I tried this approch
but still the second cluster worker is not picking up the updates from primary thread.

Node Version: v18
package details
"lmdb": "^3.0.12",
"cluster": "^0.7.7",

@kriszyp
Copy link
Owner

kriszyp commented Jul 31, 2024

What happens when you do db.put() with cache set to {validated:true}?
Does db.get() in this case bring old value from db or new one from cache (but just not flushed to disk)?

with cache set to {validated:true}, db.get() will read from the latest that has been committed to the database, and reload the data from the database if the transaction id doesn't match object in the cache.

I tried this approch but still the second cluster worker is not picking up the updates from primary thread.

Do you have any steps to reproduce this? I just tested this, and it works properly, picking up the updates from the primary process/therad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants