title: Best practice for DynamoDB composite primary key travelling inside the system (partition key and sort key)
body: <p>I am working on a system where I am storing data in DynamoDB and it has to be sorted chronologically. For <code>partition_key</code> I have an <code>id</code> (uuid) and for <code>sort_key</code> I have a <code>date_created</code> value. Now originally it was enough to save unique entries using only the ID, but then a problem arose that this data was not being sorted as I wanted, so a sort_key was added.</p>
  <p>Using python boto3 library, it would be enough for me to get, update or delete items using only the id primary key since I know that it is always unique:</p>
  <pre><code>import boto3
  
  resource = boto3.resource('dynamodb')
  table = resource.Table('my_table_name')
  
  table.get_item(
      Key={'item_id': 'unique_item_id'}
  )
  table.update_item(
      Key={'item_id': 'unique_item_id'}
  )
  table.delete_item(
      Key={'item_id': 'unique_item_id'}
  )
  </code></pre>
  <p>However, DynamoDB requires a sort key to be provided as well, since primary keys are composed partition key and sort key.</p>
  <pre><code>table.get_item(
      Key={
          'item_id': 'unique_item_id',
          'date_created': 12345          # timestamp
      }
  )
  </code></pre>
  <p>First of all, is it the right approach to use sort key to sort data chronologically or are there better approaches?</p>
  <p>Secondly, what would be the best approach for transmitting partition key and sort key across the system? For example I have an API endpoint which accepts the ID, by this ID the backend performs a <code>get_item</code> query and returns the corresponding data. Now since I also need the sort key, I was thinking about using a hashing algorithm internally, where I would hash a JSON like this:</p>
  <pre><code>{
      &quot;item_id&quot;: &quot;unique_item_id&quot;,
      &quot;date_created&quot;: 12345
  }
  </code></pre>
  <p>and a single value then becomes my identifier for this database entry. I would then dehash this value before performing any database queries. Is this the approach common?</p>