Developing a Game Leader Board using Spring Boot + Redis in AWS Cloud

Shazin Sadakath
4 min readDec 19, 2021

I have always been excited about the world of gaming and we have hit a stage where all gamers are online and play against each other in a networked environment. So it is becoming a challenge as well as a necessity to keep track of the Game Leader Board online in realtime.

Thus we are going to have a look on how to build a Game Leader Board using Spring Boot and deploy it in AWS Cloud with an Application Load Balancer and three EC2 instances connecting to an ElastiCache Redis instance.

The Game Leader Board Deployment Architecture

We are going to implement the above mentioned architecture in AWS Cloud.

Step 1

We need to develop the Spring Boot application using Spring Data Redis and Spring Boot Actuator as main dependencies. The complete source code can be found at https://github.com/shazin/spring-boot-redis. Also there are releases of this project available at https://github.com/shazin/spring-boot-redis/releases. The application makes use of Redis Sorted Set to maintain the leaderboard.

The main classes are GameLeaderBoardService

@Service
public class GameLeaderBoardService {

@Autowired
private RedisTemplate<String, String> redisTemplate;

@Resource(name = "redisTemplate")
private ZSetOperations<String, String> zSetOperations;

public List<Gamer> add(Gamer gamer) {
zSetOperations.add("leaderboard", gamer.getName(), gamer.getRank());

return zSetOperations.rangeWithScores("leaderboard" ,0,10)
.stream().map(e -> new Gamer(e.getValue(), e.getScore()))
.collect(Collectors.toList());
}

public void deleteAll() {
zSetOperations.getOperations().delete("leaderboard");
}
}

and GameLeaderBoardController

@RestController
@RequestMapping("/gameleaderboard")
public class GameLeaderBoardController {

@Autowired
private GameLeaderBoardService gameLeaderBoardService;

@PostMapping
public List<Gamer> add(@RequestBody Gamer gamer) {
return gameLeaderBoardService.add(gamer);
}

@DeleteMapping
public void deleteAll() {
gameLeaderBoardService.deleteAll();
}

}

Step 2

We need to create ElastiCache Redis instance. This step will take some time so better to do it as soon as possible.

Creating an ElastiCache Redis instance

Step 3

We need to create 3 EC2 instances and add those to a Target Group. When Creating the EC2 instances the following script needs to be used as User Data so that the EC2 instances will be created and initialized to run the latest version of the Game Leader Board Spring Boot.

#!/bin/bash
yum update -y
sudo yum install -y java-1.8.0-openjdk
wget https://github.com/shazin/spring-boot-redis/releases/download/v0.0.3/spring-boot-redis-0.0.3.jar
sudo java -Dserver.port=80 -Dspring.redis.host=<ELASTIC_CACHE_REDIS_HOST> -jar spring-boot-redis-0.0.3.jar

Just need to replace <ELASTIC_CACHE_REDIS_HOST> placeholder with the ElastiCache Redis Host URL created in the Step 2.

Launching EC2 Instances

Step 4

Now we need to create a Target Group and register the 3 EC2 Instances created as part of the Target Group. We can use /actuator/health URL since have added Spring Boot Actuator so that the Target Group can ping that for heart beat checks.

Creating Target Group
Registering EC2 Instances in Target Group

Once the heart beats return as expected the Target Group will show all the EC2 Instances registered as healthy.

Target Group Provisioning EC2 Instances as healthy

Step 5

Now it is time to create the Application Load Balancer which redirects traffic to our Target Group created in Step 4. Once it is done we can copy the Load Balancer DNS Name and start sending requests.

Creating Application Load Blancer

Step 6

Now if everything is done correctly we should be able to send POST requests to <ALB_DNS_NAME>/gameleaderboard URL.

Initial POST Request to Add to Leaderboard

And subsequent requests to the same URL with different values will add to the Leaderboard in correct order.

Subsequent POST Requests to Modify the Leaderboard

Conclusion

This project showed us how we can leverage the capabilities of Spring Boot, Redis and AWS Cloud to create a scaleable realtime game leader board solution. This solution can be improved and if you have any comments or suggestions please mention them in the comments section.

--

--