Distributed locks and transactions
Distributed locks
Used when different processes must operate with shared resources in a mutually exclusive way
Optimistic locking
Instead of blocking something potentially dangerous happen, we continue anyway, in the hope that everything will be ok
- We use a version field on the database record we have to handle, and when we update it, we check if the data we read have the same version of the data we are writing
Pessimistic
- Block access to the resource before opearting on, and we release the lock at the end
- We rely on an external system that will hold the lock for our microservices
Problems?
What happens if the lock owner doesn't release it? The lock will be held forever and we could be in a deadlock. We will set expiration time
on the lock, so the lock wil be auto-released
References
https://dzone.com/articles/everything-i-know-about-distributed-locks
https://medium.com/swlh/handling-transactions-in-the-microservice-world-c77b275813e0