Watching for changes in etcd is relatively easy and well documented, but if you want to specifically watch for expired leases it is a bit less straightforward. The etcd watcher only tells you if an event is a put or delete event, the rest you have to figure out for yourself.

Since an expired lease is a special kind of delete event, let’s start by watching for delete events that match our key prefix.

Here we watch for events with our key prefix, filter out all put events, and return the remaining events.

To find out if the deleted event happened because of a lease expiry, we need to check the lease TTL. The problem is that deleted events only contain the key of the entry and not the value or lease . This is because after the deletion the current value is nil and there is no lease. To find the value that was deleted and the lease we need to look at the previous value the entry had. We get this by adding the option clientv3.WithPrevKV() to the Watch command.

With this change and a function that finds out if the event was because of an expired lease the code now looks like this.

To determine if the event is expired, we check if the previous key-value had a lease. Using its lease ID we check if the TTL is equal to -1 and therefore expired.

That’s it!

But… do you have to start an etcd server every time you want to test this to confirm that it or similar code works? Luckily not, you can start your own server easily from code and run this example against that based on some code from the etcd repo.

Now start up the server and run the example with this test code.

See the full code on github and run the test yourself