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}