3 ms·
I tried many things for making sync work across devices. I tried GUIDs, and partitioning ranges of ints, and several versions of it. But what worked amazing?
by mamcx 6y ago
I tried many things for making sync work across devices. I tried GUIDs, and partitioning ranges of ints, and several versions of it.
But what worked amazing?
Use NATURAL keys (or their hash) + version field. That is all you need most of the cases. It make sync far easier, easier to trace stuff (thanks to version), immune to problems of timestamps (some computers have their cloks wrong). In short:
struct Order {
code: String, //natural key
version:usize
}
struct Location {
code: Hash //hash of city + country
city:String,
country:String,
version:usize
}
Natural keys are global if well defined. In some places where it is not obvious, hashing the whole row and put a nice encode is the same.
This also will reveal when something TRULY need a guid or similar. For example, for invoices in my country the law demand partition of ranges with certain characteristics (ie: INV-1-XXX in machine 1, INV-2-XXX in machine 2).
Add another id:i64 become redundant most of the time. If your Order.code is duplicated or whatever it will be the same problem with or without an extra id:i64, so is better to deal with the problems of the ACTUAL data when is need and not mask it with other stuff.
The downside is that the key become repeated in JOINS (like in InvoiceLine) but honestly all rdbms handle triggers, and it actually become very nice to see the Order.code in the child relations (far easier to correlate).
- BillinghamJ 6y agoIsn't the obvious difficulty here that such natural keys tend to change? Eg names and boundaries of cities & countries change - relatively - all the time. You don't want your IDs to be changing alongside
- mamcx 6y agoWhy not? I work for ERP/eCommerce, so this issue happens. But for real, I need to store historical facts anyway ie: In a invoice/order I must store the ship info at the time of the transaction. A artificial ID is useless in a lot of cases where "and if the key change, what?" because if that is an issue, is mostly because the data must retain history, and then, you need to store that anyway. For that, I log the data in a history/log table.