0
0
DynamoDBquery~30 mins

Many-to-many with GSI overloading in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Many-to-many relationship with GSI overloading in DynamoDB
📖 Scenario: You are building a simple event management system where users can attend multiple events, and each event can have multiple attendees. You want to store this many-to-many relationship efficiently in a single DynamoDB table using Global Secondary Index (GSI) overloading.
🎯 Goal: Create a DynamoDB table schema and items to represent users, events, and their attendance relationships using GSI overloading. Then write queries to fetch all events a user is attending and all users attending an event.
📋 What You'll Learn
Create a DynamoDB table called EventAttendance with PK and SK as primary keys.
Insert user items with PK as USER#userId and SK as PROFILE#userId.
Insert event items with PK as EVENT#eventId and SK as DETAILS#eventId.
Insert attendance items with PK as USER#userId and SK as EVENT#eventId.
Create a GSI named GSI1 with GSI1PK and GSI1SK to overload keys for querying both users by event and events by user.
Write queries to get all events for a user and all users for an event using the GSI.
💡 Why This Matters
🌍 Real World
Many applications need to represent complex relationships like users attending events, products in orders, or students in classes. DynamoDB single-table design with GSI overloading helps store and query these efficiently.
💼 Career
Understanding many-to-many modeling and GSI usage is essential for backend developers and cloud engineers working with DynamoDB in real-world projects.
Progress0 / 4 steps
1
Create the DynamoDB table and insert user and event items
Create a DynamoDB table called EventAttendance with primary keys PK and SK. Insert two user items with PK as USER#u1 and USER#u2, and SK as PROFILE#u1 and PROFILE#u2. Insert two event items with PK as EVENT#e1 and EVENT#e2, and SK as DETAILS#e1 and DETAILS#e2.
DynamoDB
Need a hint?

Use the pattern PK and SK with prefixes USER# and EVENT# to distinguish item types.

2
Add attendance items and define GSI keys for overloading
Insert attendance items representing users attending events. For user u1 attending event e1 and event e2, and user u2 attending event e1, create items with PK as USER#userId and SK as EVENT#eventId. Add attributes GSI1PK and GSI1SK to overload keys: set GSI1PK to EVENT#eventId and GSI1SK to USER#userId for attendance items.
DynamoDB
Need a hint?

Attendance items use PK as user and SK as event. GSI keys are reversed to allow querying by event.

3
Write a query to get all events attended by a user
Write a DynamoDB query to get all events attended by user u1. Use the primary key PK with value USER#u1 and filter items where SK begins with EVENT#.
DynamoDB
Need a hint?

Use begins_with on SK to filter attendance items for the user.

4
Write a query to get all users attending an event using the GSI
Write a DynamoDB query using the GSI named GSI1 to get all users attending event e1. Use GSI1PK with value EVENT#e1 and filter items where GSI1SK begins with USER#.
DynamoDB
Need a hint?

Use the GSI with GSI1PK and GSI1SK to query by event.