title: What is a good way to manage keys in a key-value store?
body: <p>Trying to define some policy for keys in a key-value store (we are using Redis).  The keyspace should be:</p>
  
  <ul>
  <li><p><strong>Shardable</strong> (can introduce more servers and spread out the keyspace between them)</p></li>
  <li><p><strong>Namespaced</strong> (there should be some mechanism to "group" keys together logically, for example by domain or associated concepts)</p></li>
  <li><p><strong>Efficient</strong> (try to use as little as possible space in the DB for keys, to allow for as much data as possible)</p></li>
  <li><p><strong>As collision-less as possible</strong> (avoid keys for two different objects to be equal)</p></li>
  </ul>
  
  <p><br/>
  Two alternatives that I have considered are these:</p>
  
  <ol>
  <li><p>Use prefixes for namespaces, separated by some character (like <code>human_resources:person:&lt;some_id&gt;</code>).The upside of this is that it is pretty scalable and easy to understand.  The downside would be possible conflicts depending on the separator (what if <code>id</code> has the character <code>:</code> in it?), and possibly size efficiency (too many nested namespaces might create very long keys).</p></li>
  <li><p>Use some data structure (like Ordered Set or Hash) to store namespaces.  The main drawback to this would be loss of "shardability", since the structure to store the namespaces would need to be in a single database.</p></li>
  </ol>
  
  <p><strong>Question</strong>: What would be a good way to manage a keyspace in a sharded setup?  Should we use one these alternatives, or is there some other, better pattern that we have not considered?</p>
  
  <p>Thanks very much!</p>