Implementing a distributed semaphore.

08:49AM May 14, 2016 in category Java by Zoltan Farkas

I recently needed to limit the number of parallel accesses to a resource across a cluster. The classic use case for a Semaphore. 

So I went ahead and looked around for existing distributed implementations. The most solid one I could find is a zookeeper based one part of apache curator. This is probably a good implementation. (I say probably since the quality of popular OS libs is questionable more often than I like it)

The one drawback in my case is that this requires zookeeper, a extra piece of infra I don’t have(yet). What I do have is a MVCC Sql database with ACID… 

So a few hours of coding later the JdbcSemaphore  was born!

What is special (I thinnk) about this implementation is: 
1) You increase/decrease the number of total reservations, see the actual total/available reservation with JMX, which is pretty useful!
2) See the state in the DB with plain SQL!
3) Relatively low overhead, it is part of spf4j!

Currently it is in beta, and is being peer reviewed… concurrency is hard to get right!

Enjoy, for information, comments, suggestions go to www.spf4j.org and don't be afraid to use the issue tracker! :-)

Comments[0]

Comments:

Post a Comment:
Comments are closed for this entry.