How can I Synchronization from acebase-server to acebase-server ? #9
Replies: 3 comments 1 reply
-
I tried this code but it doesn't work `const { AceBaseClient } = require('acebase-client'); const db = new AceBaseClient({ cache: { db: server },host: 'localhost', port: 5555, dbname: 'mydb', https: false }); |
Beta Was this translation helpful? Give feedback.
-
Horizontal scaling (multiple servers replicating their data) is not implemented yet. I am currently working on transaction logging that will allow for easy replication to other servers but I can't give you a timeline for this. If you really want to do this now, I would suggest you to add // Start local server
const server = new AceBaseServer(dbname, settings);
server.ready(async () => {
// Connect to remote server
const db = new AceBaseClient(remoteServerSettings);
await db.ready();
// Sign in to remote server
const signInResult = await db.auth.signIn('admin', '12345');
// Get remote server data. Stream to prevent locking - don't use db.root.get()!
let json = ''; const stream = { write(str) { json += str; } };
await db.root.export(stream);
// Set local server data (using private API method to suppress sending events)
const data = JSON.stringify(json);
await server.db.api.set('', data, { suppress_events: true });
// Listen for mutated events on remote server database
// Note that changes to remote data might have occurred while streaming data above
// and attaching this event listener! You could swap the order and batch received mutations
// until streaming is finished - not implemented here for simplicity
db.root.on("mutated", snap => {
const path = snap.ref.path, value = snap.val(), context = snap.context();
// Check context to prevent handling "pingback" mutations from other server
if (context.was_replicated) { return; }
// Alter context: include our was_replicated state variable
context.was_replicated = true;
// Apply mutation to our own server database
server.db.ref(path).context(context).set(value);
});
}); Note that the Last but not least I would want to say - please do not use this on mission critical data. The solution above is very basic and prone to errors. It is better to wait until proper server replication becomes available if your data is important... Let me know if you have any questions. |
Beta Was this translation helpful? Give feedback.
-
My apologies for this late reply, I never received a notification about your last comment. 😢 This might be too little too late, but NEVER have 2 separate AceBase instances access the same database files at the same time, UNLESS you've setup an IPC server so they can communicate with each other to agree on data locking and allocation. Not doing so will result in data corruption. In your code above you setup both a server AND a local db instance on the same database files (mydb), which is very dangerous without IPC. See the new AceBase IPC Server project I created for info about how to safely have multiple AceBase instances accessing the same database files. However, in your particular use-case, using IPC is not the solution because data in a cache db is not stored at the same location as the source data. Data being cached by a client is stored at paths prefixed with "dbname/cache/". This allows multiple db clients to share a single cache database, and to keep track of local mutations that have to be synchronized upon reconnect (at "dbname/pending/mutationx" etc). In your code above, that means the client will cache data from path "orders/someorderid" on your remote server into "mydb/cache/orders/someorderid" in your local cache database. Since your local server uses that same database to serve local clients, them requesting data at path "orders/someorderid" will yield null values because that's not where the data is stored... On top of that, any data changes submitted by clients to your local server will not be sync'd back to your remote server because your local server simply does not know about their existence... But there is also good news! 🥳 I have recently also implemented transaction logging so replicating data between multiple servers is now one step closer to becoming (reliably) possible, but it still needs work and a lot of testing. I will be setting up a dev & test environment for this soon. For anyone else reading this; note that replication means having multiple copies of the same data that is constantly being synchronized between servers, this is something different than when using an IPC server as described above: that's when multiple servers/instances access the same data files. It is also different from a client using a local cache database: that cache only has a copy of data that was previously retrieved from the server, and is only updated with live data if the client explicitly requests it, or has event subscription(s) set on the target path. I'll post in this thread again with sample replication code as soon as I've performed my own tests. |
Beta Was this translation helpful? Give feedback.
-
Your project is great, I really like it
Only one thing is missing
How can I Synchronization from acebase-server to acebase-server ?
I hope to add that, or is it possible?
Beta Was this translation helpful? Give feedback.
All reactions