DEV Community

Anudeep Rallapalli
Anudeep Rallapalli

Posted on

How to Build a Real-Time Leaderboard with Amazon Aurora Serverless and Amazon ElastiCache?

Every Kid inside us loves to play Games. Especially the one's where we compete with our peers. We all loved to see our name on the top of that Leaderboard! Lets see how that Leaderboard is build using Amazon Aurora Serverless and Amazon ElastiCache.

What is Amazon Aurora Serverless?
• Amazon Aurora is a highly performant, cloud-native relational database offering from AWS that offers both MySQL-compatible and PostgreSQL-compatible editions.
• The Serverless offering of the Aurora database provides on-demand automatic scaling capabilities as well as the Data API, a fast, secure method for accessing your database over HTTP.

What is the Amazon ElastiCache?
• Amazon ElastiCache is a fully managed, in-memory data store service from AWS for use cases that require blazing-fast response times.
• You can use Redis or Memcached with ElastiCache.

Why use both Amazon Aurora Serverless and ElastiCache for a Game Application?
• Amazon Aurora Serverless and Amazon ElastiCache are both commonly used individually in game applications.
• Together, they provide a combination of top-tier speed of an in-memory cache with the reliability and flexibility of a relational database.
• We will be using Amazon ElastiCache for high-volume performance, low-latency leaderboard checks for different games, and Amazon Aurora Serverless to store all historical data and provide redundancy for the leaderboard data

Technologies Used:
• Amazon Aurora Serverless for data storage, including the Data API for HTTP-based database access from your Lambda function.
• AWS Secrets Manager for storing your database credentials when using the Data API.
• Amazon ElastiCache for data storage of global leaderboards, using the Redis engine and Sorted Sets to store your leaderboards.
• Amazon Cognito for user registration and authentication.
• AWS Lambda for compute.
• Amazon API Gateway for HTTP-based access to your Lambda function.

As part of the application, we need a leaderboard system where users can compare their total score against other players as well as view the leaderboard for specific levels in the game. Finally, a user may want to see all of the scores they’ve received in a particular level.

Let's see how we use Amazon Aurora Serverless and Amazon ElastiCache to handle these access patterns.

• Set up an AWS Account and Configure an AWS Cloud9 Instance.
Alt Text
• Provision Amazon Aurora Serverless database and save the database credentials to AWS Secret Manager for use with Data API.
Alt Text
Alt Text
• Create an Entity-Relationship Diagram (ERD) to plan your data model.
Alt Text
• Create a table that matches your ERD and load some data into the database.
• Run sample queries on the Database to handle some of the use cases.
• Launch an ElastiCache for Redis Cluster.
Alt Text
• Configure the Security Group on the Redis Instance. This will help us access the Instance from the Cloud9 development environment.
• Test the configuration by connecting to the Redis Instance.
• We will be using Redis Sorted Sets for fast lookups in ElastiCache.
• Load your Sample Data into multiple Sorted Sets and read the top items from the Sorted Sets.
• Create and Configure an Amazon Cognito User Pool & Client for the User Pool for User Registration, Login and Verification.
Alt Text
• Deploy a Lambda Function and Configure REST API with API Gateway. Invoke an Endpoint to test the application.
Alt Text
• Test the Application:
a. First, you start with a Registration endpoint, where a new user signs up and creates their account.
b. Second, you use a Login endpoint where a user can use a client (such as a web application or a mobile app) to authenticate and receive an ID token.
c. Third, you use a AddUserScore endpoint to record a score for a user.
d. Fourth, you use the FetchUserScores endpoint to retrieve the top scores for a particular user.
e. Finally, you use the FetchTopScores endpoint to retrieve the global top scores for the current day and month as well as the top scores of all time.

Top comments (2)

ravavyr profile image

Nice write up, just wondering, what is the expected cost for this setup?
I know AWS charges for usage, but lets say it's the average blog with like <1k visits a month?

mrgunneramz profile image
Anudeep Rallapalli

Thank you for asking the right question. As far as, I have seen so far. It costs less than $3/day. I believe, it should be the same if there are <1k visits a month.