In this module, you design the data model for use in your application.
When using an in-memory data store like Redis, your main goal is speed and throughput. You want to serve a high number of requests as quickly as possible. To accomplish this, you organize your data in use-case specific ways. Rather than designing your data to be queried flexibly, like in a relational database, you arrange your data so that it handles your needs as quickly as possible. When designing your data model with Redis, you need to think about your needs and access patterns first.
For your leaderboard application, you want to quickly access the highest scores around the world. You have three main access patterns for your data:
- Fetch the highest scores for all time;
- Fetch the highest scores in the last month;
- Fetch the highest scores for a given day.
To handle these access patterns, you can use Redis Sorted Sets. Sorted Sets are a data structure that enables fast, ordered lookups of values. You can save the highest score for a particular user in a Sorted Set, then quickly retrieve the top 5 scores in the Sorted Set.
In your application, you create a Sorted Set for each potential access pattern you need -- overall, by week, and by day. As a new high score comes in, you load it into the relevant Sorted Sets. As users request the top scores, you can query for the top scores from the proper Sorted Set.
In the following steps, you load your sample data into your ElastiCache Redis instance and query your data for the top scores.
Time to Complete Module: 20 Minutes
In this module, you learned about modeling your data in ElastiCache for fast lookups. Then, you loaded your sample data into multiple Sorted Sets to satisfy your use cases. Finally, you saw how to read the top items from a Sorted Set.
In the next module, you configure Amazon Cognito to add authentication to your application.