|
| 1 | +# Social Network Data Modeling with Amazon DynamoDB |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document outlines a use case using DynamoDB as a social network. A social network is an online service that lets different users interact with each other. The social network we'll design will let the user see a timeline consisting of their posts, their followers, who they are following, and the posts written by who they are following. |
| 6 | + |
| 7 | +## Key Entities |
| 8 | + |
| 9 | +1. user |
| 10 | +2. post |
| 11 | + |
| 12 | +## Design Approach |
| 13 | + |
| 14 | +We employ a single table design with the following key structure: |
| 15 | + |
| 16 | +- Partition Key (PK): Identifies the key entity type (u#<userID> for user, p#<postID> for post) and optionally, a second # followed by a descriptor of what is stored in the partition. |
| 17 | + - u#\<userID\> - Given user |
| 18 | + - u#\<userID\>#follower - Given user's followers |
| 19 | + - u#\<userID\>#following - The users that the given user is following |
| 20 | + - u#\<userID\>#post - Given user's posts |
| 21 | + - p#\<postID\>#likelist - The users that have liked the given post |
| 22 | + - p#\<postID\>#likecount - The count of the given post's likes |
| 23 | + - u#\<userID\>#timeline - Given user's timeline |
| 24 | + |
| 25 | +- Sort Key (SK): Contains the ID of an entity in the Partition Key collection |
| 26 | + **or** |
| 27 | + a descriptor of the attributes ("count", "info") for the primary key of \<PK\>\<SK\> |
| 28 | + |
| 29 | + - Examples: |
| 30 | + |
| 31 | + | PK | SK | Sample Attributes | |
| 32 | + | ----------- | ----------- | ----------- | |
| 33 | + | u#12345 | `count` | follower#, following#, post# | |
| 34 | + | u#12345 | `info` | name, content, imageUrl | |
| 35 | + | u#12345#follower | u#34567 || |
| 36 | + | u#12345#following | u#34567 || |
| 37 | + | u#12345#post | p#12345 | content, imageUrl, timestamp | |
| 38 | + | p#12345#likelist | u#34567 || |
| 39 | + | p#12345#likecount | `count` | etc | |
| 40 | + | u#12345#timeline | p#34567#u#56789 | ttl | |
| 41 | + |
| 42 | + |
| 43 | +## Access Patterns |
| 44 | + |
| 45 | +The document covers 7 access patterns. For each access pattern, we provide: |
| 46 | +- Specific PK and SK used |
| 47 | +- Relevant DynamoDB operation (GetItem, Query) |
| 48 | + |
| 49 | + | Access pattern | Operation | Partition key value | Sort key value | |
| 50 | + | ----------- | ----------- | ----------- | ----------- | |
| 51 | + | getUserInfoByUserID | Query | PK=\<userID\> | |
| 52 | + | getFollowerListByUserID | Query | PK=\<userID\>#follower | |
| 53 | + | getFollowingListByUserID | Query | PK=\<userID\>#following | |
| 54 | + | getPostListByUserID | Query | PK=\<userID\>#post | |
| 55 | + | getUserLikesByPostID | Query | PK=\<postID\>#likelist | |
| 56 | + | getLikeCountByPostID | GetItem | PK=\<postID\>#likecount | |
| 57 | + | getTimelineByUserID | Query | PK=\<userID\>#timeline | |
| 58 | + |
| 59 | +## Goals |
| 60 | + |
| 61 | +- Model relationships between users and posts efficiently |
| 62 | +- Ensure scalability using Amazon DynamoDB's single table design principles |
| 63 | + |
| 64 | +## Schema Design |
| 65 | + |
| 66 | +A comprehensive schema design is included, demonstrating how different entities and access patterns map to the DynamoDB table structure. [SocialNetworkSchema.json](https://github.com/aws-samples/aws-dynamodb-examples/blob/master/schema_design/SchemaExamples/SocialNetwork/SocialNetworkSchema.json) |
| 67 | + |
| 68 | +## Additional Information |
| 69 | +[Social network schema design in DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/data-modeling-schema-social-network.html) |
0 commit comments