title: Can I use the same state.dir for multiple instances in Kafka streams?
body: <p>My use case is to get the latest value for <em>any</em> key in a Kafka topic. For this, I am currently using GlobalStateStore.</p>
  
  <p>The problem is that, if I have different <code>application.id</code> for each instance, a different state dir is being created.</p>
  
  <p>What I would want is to reuse the existing state store dir across all instances to avoid duplicating the data.
  This is because, all of my application instances run on the same machine, so there is no point of having different state directories.</p>
  
  <p><strong>I have done the following:</strong></p>
  
  <p>Use the same <strong>application.id</strong> and created 2 stream instances,
  The 2 instances pointing to the same state.dir but the state store instances are different (java objects are different) <em>statestore1 and statestore2</em></p>
  
  <p>and then as a test I have done the following:</p>
  
  <pre><code>while(true)
  {
   new Thread(()-&gt; stateStore1.get(key)).start();
   new Thread(()-&gt; stateStore2.get(key)).start();
  }
  </code></pre>
  
  <p>and then I started producing values in the topic...</p>
  
  <p>I have observed that all the state store instances (statestore1 and statestore2) are updating the same state store (i.e. same state.dir) (since that is the code)</p>
  
  <p>I did not encounter any problems in the tests doing that. But.. I suppose that a write lock should be acquired before writing the data to the state.dir.
  Now, if for some reason, statestore1 did not release the lock, then statestore2 will be going on waiting?</p>
  
  <p>Is my above approach <strong>safe</strong> or are there any other ways?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>The use-case is that there is some common data which I would want to share across different processes (different JVM instances). Since the data is common there is no need to have a global store for each process (since it would be redundant).</p>
  
  <p>All processes should have the ability to read and write to that store because each process is independent of each other.
  So if one process goes down, the other can do its work (store the latest value in the global store and get it when needed).</p>