amazon-dynamodbamazon-dynamodb-data-modeling

DynamoDB Schema - Modeling Locations inside a building


New to DynamoDB over here. I need to figure out a DynamoDB schema for different locations inside a building. Additionally, I need to be able to identify computers assigned to each of the locations. The locations are nested inside other locations. For instance,

... and so on.

ACCESS PATTERNS:

What I was thinking:

I initially wanted to create something like this:

PartitionKey              SortKey                                 Attributes

Building#1                Building#1 (For metadata)
Building#1                Section#1                                 [...]
Building#1                Section#1|Section#2                       [...]
Building#1                Section#1|Section#2|Section#3             [...]

I know this is the wrong way to think about it, but I can't figure out any other way.

What is the best way to model the location of sections, offices, etc of a building?


Solution

  • If those are really the only access patterns you can probably do something with a simple GSI. I wouldn't using Building as the PartitionKey because this will give you a lot of hot spots in the data. Something like this would probably work:

    PartitionKey        SortKey     GSI_PartitionKey GSI_SortKey            Attributes
    Building#1          'Location'                                          [...]
    Wing#1              'Location'  'Location'       Building#1 .           [...]
    Floor#1             'Location'  'Location'       Building#1|Wing#A      [...]
    .
    .
    .
    Computer#1          'Computer'  'Computer'       B#1|W#A|F#1|S#A|O#1    [...]
    Computer#2          'Computer'  'Computer'       B#1|W#A|F#1|S#B|O#1    [...]
    .
    .
    .
    

    The SortKey values here are more optional, but they tend to allow for changes later without as much work now.

    To get all the locations in a building you query the GSI where the GSI_PartitionKey is 'Location' and the GSI_SortKey begins with your building ID. You can add sub locations to the string so you can get all the locations in Wing A with a begins with of Building#1|Wing#A|

    Get a specific location using the PartitionKey (and optionally the SortKey = 'Location').

    To get all the computers in a locations GSI where the GSI_PartitionKey is 'Computer' and the GSI_SortKey begins with your location ID.

    Get a specific computer using the PartitionKey (and optionally the SortKey = 'Computer') the attributes should include it's location.