Sample Header Ad - 728x90

How do distributed databases follow unique constraints?

3 votes
1 answer
181 views
Lets say i have an application where user can register, and the username has to be unqiue value. Now lets say i have N partitions and for each partition i have M replicas with multiple leaders. Now i have questions regarding these scenarios: First: 1) User 1 attempts to register with username user1 - the write request gets routed to partition1 and to leader1 2) User 2 attempts to register with username user1 - the write request gets routed to the same partition1 and also to the leader1, In this scenario the behavior is same as we had just one database. First transaction occures and the second one fails since the user1 value is already here and we are operating on the same replika Second: 1) User 1 attempts to register with username user1 - the write request gets routed to partition1 and to leader1 2) User 2 attempts to register with username user1 - the write request gets routed to the same partition1 and to leader2, In this case we have concurrent write. How does this determine what registration fails and what not? We can look at this as no partition and multiple leader and as far as i researched in this case the typical solution is to either 1) prevent this by doing first scenario or 2) merge the values which is not acceptable in this case. Or solve conflicts on application level that is also not acceptable. How do DB's deal with this ? Third: 1) User 1 attempts to register with username user1 - the write request gets routed to partition1 and to leader1 2) User 2 attempts to register with username user1 - the write request gets routed to the same partition2 and to leader3, In this case all writes go to different partitions ( what makes sense to me that this will probably not happen in real life since they have same value and thus should be routed to one partition ). How would the DB resolve what registration would succeed and which one would fail? How would it lock stuff or check if the value exists and so on? The more i read about distributed DB's and how it works (even on high level ) im more and more confused. Thanks for answers!
Asked by Johnyb (131 rep)
May 11, 2023, 01:39 PM
Last activity: Jun 30, 2025, 03:02 PM