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:
User 1 attempts to register with username
user1
- the write request gets routed topartition1
and toleader1
User 2 attempts to register with username
user1
- the write request gets routed to the samepartition1
and also to theleader1
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:
- User 1 attempts to register with username
user1
- the write request gets routed topartition1
and toleader1
- User 2 attempts to register with username
user1
- the write request gets routed to the samepartition1
and toleader2
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:
User 1 attempts to register with username
user1
- the write request gets routed topartition1
and toleader1
User 2 attempts to register with username
user1
- the write request gets routed to the samepartition2
and toleader3
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!