I'm building a WebSocket API Gateway in AWS. When a device connects, API Gateway assigns the connection a unique connection ID. I need to store the relationship between the connection ID and device in a table:
| Connection ID | Device ID | Created At |
|---|---|---|
| 123 | ABC | 2024-10-11T21:48:08.500Z |
| 456 | DEF | 2024-10-11T21:48:08.500Z |
All connection IDs and device IDs are unique. Connection IDs have a one-to-one relationship with device IDs.
In AWS's examples, the connection ID is often used as the partition key in DynamoDB, allowing for fast lookups and deletions by connection ID. However, I also need fast lookups and deletions by device ID. If the connection ID is the partition key, finding a connection by device ID would require scanning the entire table.
In other words, the operations I need to support are (written using SQL):
SELECT connection_id FROM connections WHERE device_id = xxxx; SELECT device_id FROM connections WHERE connection_id = xxxx; DELETE FROM connections WHERE device_id = xxxx; DELETE FROM connections WHERE connection_id = xxxx; I've looked into adding a Global Secondary Index on the device ID, but GSI updates are eventually consistent. This means if I delete a connection by connection ID, someone might still see the connection if, immediately after the delete, they look up the connection using a device ID.
My question: Is it possible to model this relationship efficiently in DynamoDB, or would a SQL database be more appropriate?
1on both sides, not0..1? If you've answered "yes" twice, what would be the benefit of distinguishing between a device and a connection from a data structure perspective? If you answered "no" at least once, then you'll need to refine your question to explain exactly what relationship you're looking to model.