Implementing distributed locking with HashiCorp Consul
At the beginning of the chapter, we mentioned an example of a video data processing pipeline, where multiple service instances were attempting to process shared video files. In our example, the basic requirement was to avoid duplicate processing, so we used distributed locks to show how to limit the processing of the same files to one service instance at a time.
Let’s illustrate how to implement this scenario using our movie service as an example. The steps are the following:
- Inside our movie service code, create a directory called
internal/lock/consul
. This directory will contain the distributed lock implementation that will use the HashiCorp Consul API. Add the file calledconsul.go
with the following contents:package consul import ( "context" "errors" "github.com/hashicorp/consul/api" "go.uber.org/zap" ) // LockProvider defines a Consul...