Skip to main content
The 2024 Developer Survey results are live! See the results

Pros and Cons of DynamoDB structure?

Created
Active
Viewed 47 times
0 replies
0

Hello,

I'm using DynamoDB as the primary database for a barbershop app. During development, I've encountered an issue that I'd like some advice on. I don't have much experience with DynamoDB, so I'm looking forward to your insights.

Context

I have a barbershop application where the admin can create service groups and add services to them. The relationships between entities are as follows:

Barbershop -> (1:N) -> Service Groups -> (1:N) -> Services

One Barbershop may have multiple Service Groups
One Service Group may have multiple Services

Required Queries

The app needs to support the following operations:

  1. Get all service groups of a barbershop

  2. Get a service group with all services in it

  3. Get a specific service

  4. Create a service group inside a barbershop

  5. Create a service inside a service group

  6. Delete a service group (along with all services in it)

  7. Delete a specific service

Proposed DynamoDB Model

Currently, the DynamoDB structure is modeled as follows:

  • Beauty Page

    • PK: BARBERSHOP#1

    • SK: BARBERSHOP#1

  • Service Group

    • PK: BARBERSHOP#1

    • SK: SERVICE_GROUP#1

  • Service

    • PK: BARBERSHOP#1

    • SK: SERVICE_GROUP#1#SERVICE#1

Issue

The problem arises when I need to update a service's property, such as its name. To update an item in DynamoDB, we need to provide the full primary key (PK) and sort key (SK). This means the REST API endpoint to update a service's name would look like this:

PATCH /beauty-pages/{beautyPageId}/service-groups/{serviceGroupId}/services/{serviceId}/name

This approach requires specifying the full path each time we update a small entity, which seems cumbersome.

Alternative Model

Another way to model the service entity is:

  • Service

    • PK: SERVICE#1

    • SK: SERVICE#1

    • GSI1PK: BEAUTY_PAGE#1

    • GSI1SK: SERVICE_GROUP#1#SERVICE#1

With this model, the update URL can be simplified:

PATCH /services/{serviceId}/name

Alternatively:

PATCH /beauty-pages/{beautyPageId}/services/{serviceId}/name

Question

Is it better to include the relationship in the PK and SK, requiring a full path for updates, or should we use simple IDs (UUIDs) for the PK and SK and manage relationships using GSIs, thereby keeping the update URL shorter? The latter approach still allows us to query data effectively using GSIs.

I appreciate your insights on which approach would be more effective and maintainable.

Thank you!