0
votes

I'm sure this is a trivial and common question with regards to DynamoDB but Google or AWS docs didn't help much.

My question is simply:

What is the pattern to generate partition keys?

In an RDBMS, this is pretty straightforward e.g. with Identity columns (MSSQL) or sequences (Postgres). However, when it comes to DynamoDB (I guess this is applicable to any NoSQL database) it's not clear how to generate the partition keys - should it be a GUID or a sequence maintained in the table itself?

Here is an example schema from AWS docs - it uses incrementing numbers as the partition key. But I can't think of a way to generate non-overalapping sequential numbers at application level (as opposed to at a database level that provides atomic transactions which DynamoDB doesn't)

1

1 Answers

2
votes

Almost certainly, you want the partition key to be something which you know to be unique without reference to the existing data, either because its uniqueness is assumed in your business logic (like the serial number of a product or the email address of a user) or because it's universally unique (most simply by being a GUID, a timestamp, or some other one-time-only property).

Generating sequential unique identifiers is actually pretty hard to do at scale, and most RDBMS implementations are actually not as rigid as you might expect at scale (they generally make no guarantees that numbers won't be skipped, for instance, only that they'll be unique and monotonically increasing). If you think you need to use an incrementing id in your table, think very carefully about why and whether you could avoid doing so.

If you actually do need an incrementing numeric partition key, you'll need to have a service in your application which generates it for you. You could implement this as a separate dynamodb table containing only one row, with one "counter value" field which you increment using an ADD update expression. Scaling this implementation would be problematic.