title: How to synchronize microservices instances running in a jetty cluster using Java Spring
body: <p>I am developing a Java 8 Spring 5 microservice that runs on a Jetty embedded server. I intend to spawn several instances of this microservice, so there will be several Jetty servers running concurrently on different ports, one for each spawned instance.</p>
  
  <p>All instances share the same database which is a MongoDB database.</p>
  
  <p>From within my microservice I invoke a third party REST Web Service. I am saving the data returned by this REST WS in my database, so all instances of the cluster can take advantage of the request made by one of the instances.</p>
  
  <p>However, this data expires after some time, so one instance will have to invoke the external Web Service after some time.</p>
  
  <p>I do not want two or more instances of my cluster to invoke this external web service at the same time, if one instance is invoking this web service, and another one requires the data, it should wait for the first instance to retrieve the data and then use it.</p>
  
  <p>This is when i find the problem, because i do not know how to lock one instance of the jetty server when another one is invoking the web service.</p>
  
  <p>I have been looking into MongoDBs 4.0 Transaction feature, to try and lock a document while the service is being invoked, but without success (still :) ).</p>