Key-Range equivalent in Redis? Coming from DynamoDB
Problem Description:
In DynamoDB I use a composite key to model the one-to-many relationship in a table:
User (Key) - Order (Range)
A typical set of records there is like:
John - Burger
John - Fries
Sue - Pizza
Sue - Soda
Looks like Redis only supports primary keys in which case this won’t work because primary keys are unique. Is there a way to implement the above in Redis?
Solution – 1
Redis supports two different data types which you can use to model your one-to-many relationship:
- sets: unordered collections of unique strings;
- sorted sets: collections of unique strings (members) ordered by an associated score.
To some extents, one could even use Redis lists, which are linked lists of string values.
Is there a way to implement the above in Redis?
Yes and it depends on how you are going to use that relationship. Here is how you can do that with Redis sets, for example:
SADD users:John Burger Fries
> 2
SADD users:Sue Pizza Soda
> 2