|
| 1 | +# Chat System Data Modeling with Amazon DynamoDB |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document outlines a use case using DynamoDB as a datastore for a chat system. For example, it is intended to be used within a team of several people in a game, or in a live video streaming situation where comments are added to the video. The flow of the system is as follows: create a room, join a room, speak in a room, leave a room, and delete a room. |
| 6 | + |
| 7 | +## Key Entities |
| 8 | + |
| 9 | +1. user |
| 10 | +2. room |
| 11 | +3. comment |
| 12 | + |
| 13 | +## Design Approach |
| 14 | + |
| 15 | +We employ a single table design coupled with a global secondary index (GSI). |
| 16 | +The following key structures are used: |
| 17 | + |
| 18 | + - Base table |
| 19 | + - For a user item: |
| 20 | + - Partition key (PK) |
| 21 | + - User:\<UserID\> |
| 22 | + - Sort key (SK) |
| 23 | + - \<timestamp\> |
| 24 | + - For a room item: |
| 25 | + - Partition key (PK) |
| 26 | + - Room:\<RoomID\> |
| 27 | + - Sort key (SK) |
| 28 | + - "meta" |
| 29 | + |
| 30 | + - Examples: |
| 31 | + |
| 32 | + | PK | SK | Sample Attributes | |
| 33 | + | ----------- | ----------- | ----------- | |
| 34 | + | User:UserA | 2023-04-01T14:00:00.001Z | RoomID, Comment, CreatedAt | |
| 35 | + | Room:Art | `meta` | CreatedBy | |
| 36 | + |
| 37 | + - GSI |
| 38 | + - Partition key (RoomID) |
| 39 | + - \<RoomID\> |
| 40 | + - Sort key (CreatedAt) |
| 41 | + - \<timestamp\> |
| 42 | + |
| 43 | + - Example: |
| 44 | + |
| 45 | + | PK | SK | Sample Attributes | |
| 46 | + | ----------- | ----------- | ----------- | |
| 47 | + | Music | 2023-04-01T12:00:00.001Z | PK, SK, Comment | |
| 48 | + |
| 49 | + |
| 50 | +## Access Patterns |
| 51 | + |
| 52 | +The document covers 9 access patterns. For each access pattern, we provide: |
| 53 | +- Usage of Base table or GSI |
| 54 | +- Relevant DynamoDB operation (PutItem, DeleteItem, Query) |
| 55 | +- Partition and Sort key values |
| 56 | +- Other conditions or filters |
| 57 | + |
| 58 | + | Access pattern | Base table/GSI | Operation | Partition key value | Sort key value | Other conditions/Filters | |
| 59 | + | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | |
| 60 | + | createChatRoom | Base table | PutItem | PK=\<RoomID\> | SK="meta" | if not exists | |
| 61 | + | deleteChatRoom | Base table | DeleteItem | PK=\<RoomID\> | SK="meta" | CreatedBy=\<UserID\> | |
| 62 | + | joinChatRoom | Base table | PutItem | PK=\<UserID\> | SK="Join:"\<roomID\> | | |
| 63 | + | leaveChatRoom | Base table | DeleteItem | PK=\<UserID\> | SK="Join:"\<roomID\> | | |
| 64 | + | addComments | Base table | PutItem | PK=\<UserID\> | SK=\<timestamp\> | | |
| 65 | + | getAllComments | GSI | Query | PK=\<RoomID\> | | Limit 1 | |
| 66 | + | getLatestComments | GSI | Query | PK=\<RoomID\> | | Limit 10 & ScanIndexForward = false | |
| 67 | + | getFromLatestToSpecifiedPositionComments | GSI | Query | PK=\<RoomID\> | SK > \<FromPosition\> | | |
| 68 | + | getFromPositionToPositionComments | GSI | Query | PK=\<RoomID\> | SK between \<FromPosition\> and \<ToPosition\> | | |
| 69 | + |
| 70 | + |
| 71 | +Please note: We add “Limit 1” for getAllComments since GSIs can have duplicate values. GSIs do not enforce uniqueness on key attribute values like the base table does. |
| 72 | + |
| 73 | +## Goals |
| 74 | + |
| 75 | +- Model relationships between users and posts efficiently |
| 76 | +- Ensure scalability using Amazon DynamoDB's single table design principles |
| 77 | + |
| 78 | +## Schema Design |
| 79 | + |
| 80 | +A comprehensive schema design is included, demonstrating how different entities and access patterns map to the DynamoDB table structure. [ChatSystemSchema.json](https://github.com/aws-samples/aws-dynamodb-examples/blob/master/schema_design/SchemaExamples/ChatSystem/ChatSystemSchema.json) |
0 commit comments