Implementing a lease on top of cockroach

Instead of using a software like Zoopkeeper or Consul, my team is considering using Cockroach to implement a distributed lease. The lease will be distributed between two processes, each in one datacenter. One argument is that we already have Cockroach deployed and if the algorithm is simple, it could be simpler than deploying and configuring this software. Another reason is that the process having the lock need to access cockroach so if we bind the lease to cockroach, a process could acquire the lease only when cockroach is available for him.

Does it sound like a reasonable idea to you? If yes, what would be the best primitives in cockroach to implement such a lease or such a lock?

This is a very good point; Cockroach can be used as a system to coordinate distributed leases. In fact, cockroach uses itself for leasing for a good number of things; its higher level leasing subsystem uses just SQL. Cockroach does not currently export any fundamental concepts to handle leasing semantics for you, but they aren’t hard to implement.

This post explains a way to do it and the discussion which follows is also nice: Quick and Easy, Exactly-Once, Distributed Work Queues Using Serializable Transactions - DEV Community.

There is a paper on the subject: Mahmoud Ismail, Gautier Berthou, Jim Dowling, Salman Niazi. Leader Election Using NewSQLDatabase Systems. 15th IFIP International Conference on Distributed Applications and InteroperableSystems (DAIS), Jun 2015, Grenoble, France. pp.158-172, 10.1007/978-3-319-19129-4_13. hal-01775025

But the implementation is too complex to my taste.