简体   繁体   中英

Dynamodb table design pattern

I am not sure if this is correct place to ask this question.

I am new to dynamodb and trying to figure my way out to create a small web application. I've read the best practices here http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/BestPractices.html

My tables will be:

  1. Buildings
  2. Tenants (a building can have as many tenants, identified by floor number)
  3. Recipients (each floor can be as many recipients, basically an alias of a tenant)
  4. Couriers
  5. Deliveries (a courier tied with the particular recipient)

My approach currently to design the schema looks like this:

// Tables
// PK: Building Id, SK: name
- Building ID : {
  name: <Building Name>
  Tenants: [
     { TenantId: { Receipients: [{Receipient Id 1, Receipient Id 2...}] }
  ] 
}

// PK: Courier Id, SK: createdAt
- CourierId :  {
 name: <Courier name>
 ...
}

//PK: Not Sure, SK: Not Sure <-- This is where I messed up, looks relational, defeats the purpose?
- Deliveries: {
  receipient id: Courier id
}

In order to list out all deliveries along with recipient details, I will be needing recipient details by recipient Id. That, according to the LSI docs, will mean making recipient Id as a sort key in the in the Building table.

Since you can only have top-level elements of the table (recipient not being the one), this seems impossible as per guideline https://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/GSI.html

The index key attributes can consist of any top-level String, Number, or Binary attributes from the base table; other scalar types, document types, and set types are not allowed.

So, I am hopeless this can be achieved without making another table, where I should copy all the recipients, updating them everything update happens in the table. Is there any other alternative or better approach?

Since a deliveries seems one to one relation of couriers with the recipient, is it okay to keep the ids and to iterate over all the recipient and couriers it at the time of listing all the deliveries?

Probably DynamoDB is not the best solution for your needs. As I understood, you have a relational data structure, mainly in the table related to deliveries. DynamoDB was designed to keep unstructured data, requested mainly by one or two keys. It was not designed to maintain relational integrity or provide table joins.

I don't know if you have experience with cloud development, but using cloud infrastructure you can have one database per kind of data structure. For example:

  1. You can maintain your building and courier data in DynamoDB, and use it mainly requesting by ids; maybe even in the same table. You can have different documents/objects with different structure;
  2. You can save the deliveries information in a relational database using RDS. Depending on your AWS region you may try Aurora Serverless. It will cost you less.
  3. If your information in DynamoDB objects does not become big (DynamoDB objects/documents have a limit of 400 kB) you can save all the data in one object only and use a DynamoDB stream to sent the information to a search solution (CloudSearch or ElasticSearch). Than when you need to query a single delivery you use the search endpoint. When you need the building or tenant history you can use the DynamoDB endpoint.

There are a lot of other solutions, if you expand your view and use more than one resource in conjunction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM