Skip to main content

DynamoDB Design

This system use DynamoDB.

Table Configs

Table Name: nishiki-table-prod-db
Deletion Policy: Retain (In develop environment, Delete)

note

In the development environment, the DB name is changed to the nishiki-table-dev-db.

Table Structure

Key

Partition Key is mostly ID of the entity. Sort key is normally, explains the type of data, if the data is about User, the sort key is USER. But this is relative.

Attributes

NameTypeNote
UserIdStringUUID
UserNameString
EMailAddressString
GroupIdStringUUID
GroupNameString
LinkExpiryDatetimeStringISO 8601 date and time
InvitationLinkHashStringThis hash is generated from the Group ID and Expiry Datetime using the MD5 algorithm
ContainerIdStringUUID
ContainerNameString
FoodsList[Object]Object Detail

Global Secondary Index (GSI)

UserAndGroupRelations

GSI Name: UserAndGroupRelationship
Projection Type: KEY_ONLY

KeyAttribute
PKGroupId
info

This index is used for the access pattern of retrieving a list of users by the Group ID.

GroupAndContainerRelationship

GSI Name: GroupAndContainerRelationship
Projection Type: KEY_ONLY

KeyAttribute
PKContainerId
info

This index is used for the access pattern of retrieving a Group ID by the Container ID.

EMailUserRelation

GSI Name: EMailAndUserIdRelationship
Projection Type: KEY_ONLY

KeyAttribute
PKEMailAddress

Non Key Attributes

  • InvitationLinkHash
note

This GSI's PK is "ExpiryDatetime". Used for querying the expired Datetime.

InvitationHash

GSI Name: InvitationHash
Projection Type: INCLUDE

KeyAttribute
PKInvitationLinkHash

Non Key Attributes

  • LinkExpiryDatetime

Contexts

note

The {} means that the value inside it will be dynamic value.

User

PK: User ID (UUID)

SKDetailAttributes
UserUser DataUserName, EMailAddress
Group#{GroupID}The group user belong toGroupId

Group

PK: Group ID (UUID)

SKDetailAttributes
GroupGroup DataGroupName, Users
Container#{ContainerID}Container DataContainerId
InvitationLinkHashInvitation link's hash stringLinkExpiryDatetime, InvitationLinkHash

Container

PK: Container ID (UUID)

SKDetailAttribute
ContainerContainer DataContainerName, Foods
info

Container can be defined in the Group context using PK as group ID and SK as Container#{ContainerID}. While it makes the item structure more straightforward, it requires the use of a Global secondary Index(GSI) to retrieve container data using the container ID.

In this system, updating food operations are the most frequent, and in DynamoDB, writing operations are more than 5 times more expensive than reading. Using GSI for writing operations result in twice the cost which is not cost-effective. Hence, we prioritize cost efficiency and opted to separate the container from the group.

Foods

Food is the object.

{
FoodId: string (UUID),
Name: String,
Unit: String | null,
Quantity: Number | null,
Category: String,
Expiry: String (Datetime) | null,
CreatedAt: String (Datetime)
}

Access Pattern

Access pattern nameKey (PK/SK)How to AccessDetailContext
GetUserUserId / UserGetGet a single user dataUser
GetUserUserId / Group#{GroupId}GetCheck user's existence. return boolean.Boolean (Primitive value)
GetUserByEMailEMailAddressQuery against GSIGet a single user's IDUser
ListOfUsersGroupUserId / Group#QueryList of groups user belonging toUser
GetGroupGroupId / GroupGetGet a group dataGroup
GetGroupContainerIdQuery against GSIGet data of a group a container belonging toGroup
ListOfContainersGroupId / Container#QueryList of containers belonging to groupGroup
ListOfUsersInGroupGroupIdQuery against GSIList of users belonging to groupGroup
GetContainerContainerId / ContainerGetGet a container dataContainer
GetInvitationLinkInvitationHashQuery against GSIGet an invitation link and related informationGroup
GetInvitationLinkByGroupIdGroupIdGetGet an invitation hash from the group IDGroup