How to design many-many relationships in DynamoDB so that I can fetch all the items where an entity is present at any end of the relationship?


I have a call application where one user can call another user. Only 1v1 call is possible. So I want to store details of calls in my dynamodb table. There are a handful of access patterns but the main and only access pattern I'm struggling to find a good solution for is:

List all, that is, both incoming and outgoing calls of a user sorted by timestamp.

The solutions I came up with are:

  1. Create 2 entries for each call - 1 with user1 as pk and 1 with user2 as pk. But this will mean I will have to update both entries when I want to update call status or timestamps.

  2. PK as user1 and GSI1-PK as user2 - But with this approach, I will have to fetch certain number of calls received, sorted by timestamp, then get the timestamp of the last item, and fetch all calls made after that timestamp. Then I'll have to concatenate both those lists and sort it by timestamp in application logic.

Is there a better way to fulfill this access pattern? If not, which one of the above methods would you suggest?

asked a year ago294 views
1 Answer
  • I have gone through those blogs already. Their access pattern is different and more straight-forward to solve using adjacency list. I have used the adjacency method already in my solution 2 but I will need to do 2 queries and more work in the application layer to fulfill my access pattern.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions