Build a real-time leaderboard with Amazon Aurora Serverless and Amazon ElastiCache
Published: 2022-01-26 17:44:57

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:

• Active AWS Account**
• Browser: AWS recommends Chrome
• Amazon Aurora
• Amazon ElastiCache
• AWS Secrets Manager
• Amazon Cloud9
• Amazon Cognito
• AWS Lambda
• Amazon API Gateway
• AWS SDK for Node.js

• 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aq1dzd0z5xbojku19fhs.png)
• Provision Amazon Aurora Serverless database and save the database credentials to AWS Secret Manager for use with Data API.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3uafwmafsgvs8c21nmfa.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmy8hf9on4ekkr9yefa9.jpg)

• Create an Entity-Relationship Diagram (ERD) to plan your data model.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zec67gt5j7wmxu43wjj4.jpg)

• 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.  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fohq99e2qvrub81rl7fg.jpg)

• 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0xmy7nj5lzm83ypd8zt.jpg)

• Deploy a Lambda Function and Configure REST API with API Gateway. Invoke an Endpoint to test the application.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbcm6jb9cvoh7ysn27na.jpg)
• Test the Application:
1. Start with a Registration endpoint, where a new user signs up and creates their account.
2.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.
3. Use a AddUserScore endpoint to record a score for a user.
4. Use the FetchUserScores endpoint to retrieve the top scores for a particular user.
5. 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.