Sign in

Photo by Gabriel Gusmao on Unsplash

A couple of months ago, I was developing services to calculate balance transaction sent from one’s user account to another’s user account and vice versa. Each transaction request is handled concurrently, thus reading and writing to databases also happens concurrently. Concurrent operations could cause data race if handled incorrectly and causing indeterminate value in user’s balance calculation. To avoid this, we need to make balance calculation on each user’s account run synchronously.

In this article, we will discuss how we can solve this problem using our Multiple Lock implementation and compare our Multiple Lock implementation with regular golang Mutex. …


Hi, thanks for publishing your solution.

I think there is a bug in your code as @michaelseu2011 pointed out.

I made some improvement for multi lock,

here is my code.

I use internal lock to make sure acquiring and releasing the lock run synchronously.

I also made some benchmarking to compare the performance between lock from mutex and multilock implementation.

The result is that multi lock is not effective for small operation since it cause overhead. But for heavy operation, you can use multilock implementation.

Hope it helps.

Henry J

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store