title: binary vs. string vs. number for storing UUID in DynamoDB partition key?
body: <p>I'm trying to decide whether to use binary, number, or string for my DynamoDB table's partition key.  My application is a React.js/Node.js social event-management application where as much as half of the data volume stored in DynamoDB will be used to store relationships between Items and Attributes to other Items and Attributes. For example: friends of a user, attendees at an event, etc.</p>
  
  <p>Because the schema is so key-heavy, and because the maximum DynamoDB Item size is only 400KB, and for perf &amp; cost reasons, I'm concerned about keys taking up too much space. That said, I want to use UUIDs for partition keys. There are well-known reasons to prefer UUIDs (or something with similar levels of entropy and minimal chance of collisions) for distributed, serverless apps where multiple nodes are giving out new keys.</p>
  
  <p>So, I think my choices are: </p>
  
  <ol>
  <li>Use a hex-encoded UUID (32 bytes stored after dashes are removed)</li>
  <li>Encode the UUID using base64 (22 bytes)</li>
  <li>Encode the UUID using <a href="https://rfc.zeromq.org/spec:32/Z85/" rel="noreferrer">z85</a> (20 bytes)</li>
  <li>Use a binary-typed attribute for the key (16 bytes)</li>
  <li>Use a number-typed attribute for the key (16-18 bytes?) - the Number type can only accommodate 127 bits, so I'd have to perform some tricks like stripping a version bit, but for my app that's probably OK. See
  <a href="https://stackoverflow.com/questions/53056504/how-many-bits-of-integer-data-can-be-stored-in-a-dynamodb-attribute-of-type-numb">How many bits of integer data can be stored in a DynamoDB attribute of type Number?</a> for more info.</li>
  </ol>
  
  <p>Obviously there's a tradeoff in developer experience. Using a hex string is the clearest but also the largest. Encoded strings are smaller but harder to deal with in logs, while debugging, etc. Binary and Number are harder than strings, but are the smallest. </p>
  
  <p>I'm sure I'm not the first person to think about these tradeoffs.  Is there a well-known best practice or heuristic to determine how UUID keys should be stored in DynamoDB?</p>
  
  <p>If not, then I'm leaning towards using the Binary type, because it's the smallest storage and because its native representation (as a base64-encoded string) can be used everywhere humans need to view and reason about keys, including queries, logging, and client code. Other than having to transform it to/from a <code>Buffer</code> if I use <code>DocumentClient</code>, am I missing some problem with the Binary type or advantage of one of the other options in the list above?</p>
  
  <p>If it matters, I'm planning for all access to DynamoDB to happen via a Lambda API, so even if there's conversion or marshalling required, that's OK because I can do it inside my API. </p>
  
  <p>BTW, this question is a sequel to a 4-year-old question (<a href="https://stackoverflow.com/questions/21888923/uuid-data-type-in-dynamodb">UUID data type in DynamoDB</a>) but 4 years is a looooooong time in a fast-evolving space, so I figured it was worth asking again.</p>