Search code examples
amazon-dynamodb

DynamoDB for Polymorphic Data


I'm looking to design a dynamodb schema for a polymorphic dataset with a special edge case.

My dataset is events of different types for a user

{
    userID: uuid,
    eventType: enum/string,
    createdAt: timestamp,
    data: { ... }
}

My access patterns will be

  • getting all events for a user
  • getting all events of a particular type for a user
  • getting all events between 2 createdAt dates for a user
  • always sorting by createdAt

The special case that I'm struggling to handle is that we have a specific event type "ORDER" and for orders I also want to be able to sort on 10-20 keys in the data. These keys are specific to the "ORDER" event and not relevant to any other events.

Order access patterns:

  • Get all orders for a user sorted by field1
  • Get all orders for a user sorted by field2
  • Get all orders for a user sorted by field3 ...

Is dynamo a good solution here? Can I do this in 1 table or should I consider breaking it up in 2 tables? Looking for help on a schema design / GSI, LSI etc


Solution

  • As you'd imagine with an open question you get a vague answer, it depends.

    DynamoDB is a good fit if you require it's functionality and what it's good for, which is a scalable serverless database.

    If the majority of your use-case can be fulfilled from the table or indexes, then filtering for nested attributes may be fine, or you could integrate OpenSearch via DynamoDB ZeroETL feature to allow you to make efficient queries on nested values.

    In short, it's dependent on your specific application requirements.