0

I'm trying to learn proper DynamoDB modeling coming from a standard RDBMS background.

The relational database I am trying to denormalise is as follows:

TwoFactorDetail

  • Id (pk)
  • Status
  • PhoneNumberId (fk)

PhoneNumber

  • Id (pk)
  • PhoneNumber
  • Type
  • AuthAttempts

Log

  • Id (pk)
  • TwoFactorDetailId (fk)
  • EventType
  • CreatedAt

My single table schema is as follows:

https://i.stack.imgur.com/8p3WY.png

As you can see, the same PhoneNumber can be used by many TwoFactorDetail entities.

Considering these two read / write patterns:

  • Read TwoFactorDetail metadata including phone number, phone number type and attempts by TwoFactorDetail.Id
  • Update PhoneNumber attempts by phone number

As you can see in the model, I am satisfying the read pattern with a known approach where the PhoneNumber entity linked to the TwoFactorDetail becomes an item within that partition. This means I can read all details for a particular TwoFactorDetail.Id with a single round trip to the db by using this condition: PK = "DET#12345" AND (SK = "DET" OR SK Begins with "PHO")

Now my question is how to keep PhoneNumber metadata consistent across partitions.

Ie. Because phone numbers can be shared across many two factor detail entities, and I would like to duplicate this data in each partition to ease reads, I am unsure how to update all items matching a phone number in a smart way so that I use the less amount of requests.

1 Answers1

0

My solution was to change my table model as follows:

  • Move PhoneNumber metadata item to the TwoFactorDetail metadata item as additional attributes as opposed to being a sepparate item.
  • Create a GSI on the PhoneNumber attribute making the PhneNumber the Partition Key so that I can get all TwoFactorDetail entities using it across partitions.

This picture is self explanatory: https://i.stack.imgur.com/nAGc6.png

I am happy with this change because I can get all TwoFactorDetail metadata with just a single round trip to the DB.

On the other hand, this is what I have decided to do to keep the same PhoneNumber metadata consistent across TwoFactorDetail entities:

  1. Query main table against the PhoneNumber GSI
  2. Modify properties of TwoFactorDetail retrieved items as necessary
  3. Send a batch write request to DynamoDb