M364M4N cro
M364M4N cro

Reputation: 710

Syncronizing 2 databases using hibernate - use save(), update() or saveOrUpdate()?

I am trying to sync multiple databases whose items have GUID for IDs, meaning that one item has the same ID on all databases.

My question is: If i modify or create on item on 1 database, and want to synchronize this change to the other database should i:

1.) Check if the item is new or just modified, if its new then use the save() function, if its modified then use the update() function

or

2.)Do not check if its new or modified and just use the saveOrUpdate() function?

Upvotes: 0

Views: 431

Answers (1)

Colin M
Colin M

Reputation: 13348

After seeing your use case in the comments, I think the best approach is to track (on both the client and server) when the last updated/last synced time was. In the event that the last sync time is null, or comes before the last updated time, you know that the data needs to be synced.

Now, on to the heart of your question: how to sync it. The client need not know the state of a server when it sends an object to you. In fact, it shouldn't. Consider the case where the client posts an object, your server receives it and process it, but the connection dies before your client receives the response. This is a very valid scenario and will result in a mis-match of data. As a result, any way that you try to determine whether or not the server has received an object (from the client) is likely to end up in a bad state.

The best solution is really to create an idempotent endpoint on the server (an upsert method, or saveOrUpdate as you referred to it in your question) which is able to determine what to do with the object. The server can query it's database by primary key to determine if it has the object or not. If it does, it can update, if not, it can insert.

Understandably, performance is important as well as the data. But, stick with primary keys in the database and that one additional select query you add should be extremely minimal (sub-10ms). If you really want to squeeze some more performance out, you could always use memcache or redis as a caching layer to determine if you have a certain GUID in your database. This way, you only have to hit memory (not your database) to determine if an object exists or not. The overhead of that would be measured only in the latency between your web server and cache server (since a memory read is incredibly cheap).

tl;dr

Upsert (or saveOrUpdate) is the way to go. Try not to track the state of one machine on another.

Upvotes: 1

Related Questions