I have a scenario where two or more instances of the same verticle will be instantiated. I want to make sure that only one of the instances is consuming the key 'keyx' and in order to do that I check on a Service Discovery instance if a certain type of record is there and, if not, I can safely say that no one is consuming 'keyx'.
Therefore, I publish a record on the Service Discovery instance and I subscribe to 'keyx'. All the other instances will now check with the Service Discovery that some instance is already registered for 'keyx'.
If the machine with the verticle instance has any serious problem, it will get the verticle killed and the record will still be on the Service Discovery (in this case, removing the record in the stop() method would not work because this method wouldn't be called) and all the other instances created will believe that an instance is still consuming 'keyx' when it is possibly not the case.
Does someone know any viable solution for this problem?
Thanks ;)
One way is to use a datastore that has automatic expiry for keys. Then clients must periodically re-add the key to the store to continue using it, and if they fail then the key is automatically removed. Redis offers this kind of feature (https://redis.io/commands/expire).
Alternatively if you dont have this feature, you can simply store a timestamp when you set the key. If another client reads the key but the time has expired it can safely use the key.