Home > OS >  Catch DynamoDB changes
Catch DynamoDB changes

Time:11-02

I am developing a game where 2 players are matched against each other using AWS API Gateway Websockets.

Both players submit their moves and update a table in my database. How can I know that I received the two moves, so I can notify both sockets that the round finished?

Now I tried this:

  • When the player submits his moves, I read the db beforehand to check if the other player already submitted his move.

But this approach fails if they submit their moves at the same time, as both reads would give no results.

My DB design is:

  • primaryKey: 1$

  • secondaryKey: user1

  • moves: [move1, move2, move3...]

  • primaryKey: 1$

  • secondaryKey: user2

  • moves: [move1, move2, move3...]

(I have 2 records for each game because I need to be able to query games based on users too, which helps with the statistics and other functionalities of the game)

As a temporary solution, I am using DynamoDB Streams to invoke a lambda every time users submit their moves, and if I have 2 moves, I send through their sockets what I need to send. However, I think that using Streams is only for logging purposes, and not business logic.

CodePudding user response:

Streams is not intended for just logging purpose and is very much used for business logic especially when coupled with Lambda as it provides a cost efficient mechanism for event driven architectures.

I would continue to use Streams as it suits your needs and is exactly what they are intended for.

  • Related