Search code examples
amazon-web-servicesnosqlamazon-dynamodbaws-appsync

How do I minimize data redundancy in DynamoDB many-to-many relationship?


I am currently building a simple newsletter app, where users can get invited via their emails to read a newsletter.

I have the current example table setup:

enter image description here

  • where i use autogenerated Ids and emails as partition keys and generic keywords as sort keys,
  • for defining newsletter-email relationship I use newsletter ID as PK and emails as SK's

I use 1 GSI, which uses base table SK as PK and base table PK as SK - this gives me the following aggregated view:

enter image description here

  • my desired access pattern is to allow invited users to list their newsletters with simple nextToken AWS mapping template resolver pagination.

My question is - for my use case, the "newsletter email relationship data" and "newsletter data" has to be identical, so that I can show a preview of my newsletters in the newsletter list and at the same time, display newsletter itself after the user clicks on one of the listed newsletters and displays the newsletter detail.

  • Is there a way to achieve this without redundancy?
  • If not, how do i keep the hundreds of newsletter email relationship data in sync with the newsletter data? Do i really need to perform hundreds of PutItem operations via TransactionPutItem every time i update the name of my newsletter?
  • Is there some methodology I am missing?

Solution

  • For anyone trying to understand dynamoDB and appsync the same way I did. You dont need to save most of your data redundantly the way I did in this example. You can actually define a relationship between your newsletter and your newsletter application and just fetch the newsletter data straight from the invitation as such as in the example below:

    type Newsletter {
        id: Int!
        name: String!
        description: String!
    }
    
    type NewsletterInvitation {
        id: Int!
        email: String!
        newsletter: Newsletter!
    }
    

    This should give an answer to most of the people coming across this post.

    For any use cases regarding SK modifications, it gets more difficult. You will need to use dynamodb data streams, SQS fronts and possibly custom lambda triggers to listen to your specific Item change, creating an SQS event or triggering a lambda and doing the modifications of the newsletter invitation sort keys manually.