-1

I'm trying to keep a count of people in a room. Currently, people can join and leave rooms (many-many) relationship. I'm modeling this currently by a two-way roomId <-> userId PK/SK swap via a GSI. So I can look up who is in a room, and which room a user is in.

However, I'd also like to know the top N rooms by population. For this, I could increment a count per room when a user enters it, and decrement on them leaving it (say in another table), but then I have to do an extra write. I feel like there could also be issues with keeping it in sync, and perhaps DynamoDB streams could handle the sync issue.

Thoughts on a better approach?

e.g

PK SK
user1 room1
user1 room2
user2 room2
user3 room2

topRooms(n) = {room1: 1, room2: 3}

Joe
  • 112
  • 7

1 Answers1

1

What about using a Materialised aggregate query to do this? https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-gsi-aggregation.html

So essentially what you would do is use streams and attach a lambda function to it.

Whenever someone joins or leaves a room, it’s written to the DB which triggers the lambda function and does the aggregation.

JS-Dev
  • 51
  • 4