Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Building a real-time leaderboard with Red Hat Data Grid and Quarkus on a hybrid Kubernetes deployment

May 28, 2021
Katia Aresti Don Naro Ryan Emerson
Related topics:
KubernetesQuarkus
Related products:
Red Hat Data Grid

Share:

Red Hat Data Grid, built on the Infinispan community project, has been a key component of the Red Hat Summit keynote demonstration for several years, and the first part of our virtual summit in April 2021 was no exception. This year, we built an online Battleship game that was deployed across three continents and hosted on Amazon Web Services (AWS), Google Cloud Platform (GCP), and Microsoft Azure. If you missed the live action with Burr Sutter, you can catch the video replay on YouTube.

In this article, we’re going to take a closer look at Data Grid's role in the demonstration, explain the architecture, and break down some of the technical details behind what Burr calls “that Data Grid magic.”

Note: Visit Red Hat Summit 2021 for information and a schedule for the second part of this year's virtual summit, coming June 15 to June 16.

Leaderboard: Determining the global winners

Let’s start with the use case for the live demonstration. The purpose of the leaderboard is to track scores in real time and then calculate a global ranking of all the players. Figure 1 shows the user interface for the Battleship game.

A screenshot from the Battleship game demo at Red Hat Summit 2021.
Figure 1: The Battleship game interface.
Figure 1: Battleship game interface.

The game application and all related services were hosted on three cloud providers in different geographical regions. We used Knative Eventing (CloudEvents) functions built with Quarkus Funqy, along with a few other amazing components, to track each player's points in every match. For everything to work together, Data Grid stored all the game data (players, current game, matches being played, and so on) at each site.

However, the leaderboard represents a global ranking of players, so we needed all the player scores across each cluster to determine the overall winners.

The leaderboard's architecture

Each of the three data centers hosted a Data Grid cluster alongside a scoring service and a leaderboard service, as shown in Figure 2.

The leaderboard for the online battleship game.
Figure 2: The leaderboard.
Figure 2: The architectural components of the leaderboard.

Let's take a closer look at the scoring service and leaderboard service.

The scoring service

During each match, players battled it out, trying to sink each other’s ships and score points. A separate service used Cloud Functions to calculate points according to a defined business logic. But we needed a way to collect all those points and save them in our Data Grid cluster.

The scoring service was our solution. We implemented a REST API with Quarkus and the RestEasy extension. We then added the infinispan-client extension for Quarkus to establish a connection to our players-scores cache.

Putting all this together resulted in a service that adds an entry to the players-scores cache the first time the player scores a point. For any subsequent points, the scoring service updates the entry with the delta score.

Here is the ScoringService.java implementation:

@Path("/scoring")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApplicationScoped
public class ScoringResource {

   @Inject
   @Remote(PlayerScore.PLAYERS_SCORES)
   RemoteCache<String, PlayerScore> playersScores;

   @POST
   @Path("/{gameId}/{matchId}/{userId}")
   public Response score(@PathParam("gameId") String gameId,
                         @PathParam("matchId") String matchId,
                         @PathParam("userId") String userId,
                         @QueryParam("delta") int delta,
                         @QueryParam("human") boolean human,
                         @QueryParam("timestamp") long timestamp,
                         @QueryParam("bonus") Boolean bonus,
                         @QueryParam("username") String username) {

      String key = getKey(gameId, matchId, userId);
      PlayerScore playerScore = playersScores.get(key);

      if(playerScore == null) {
         playerScore = new PlayerScore(userId, matchId, gameId, username, human, delta, timestamp, GameStatus.PLAYING, 0);
      } else {
         playerScore.setScore(playerScore.getScore() + delta);
         playerScore.setTimestamp(timestamp);
      }

      playersScores.put(key, playerScore);
      return Response.accepted().build();
   }

Cache configuration

Our Data Grid cluster has multiple nodes, so a distributed cache provides a good tradeoff between scalability and availability. The players-scores cache supports indexing with the org.redhat.PlayerScore entity and uses the application/x-protostream media type to encode caches.

Here's the cache configuration we used:

<distributed-cache name="player-scores" statistics="true" owners="2"> 
   <encoding>
      <key media-type="application/x-protostream" />
      <value media-type="application/x-protostream" />
   </encoding>
   <indexing enabled="true">
      <indexed-entities>
         <indexed-entity>org.redhat.PlayerScore</indexed-entity>
      </indexed-entities> 
   </indexing>
</distributed-cache>

PlayersScore.java

PlayerScore.java is the class we created to provide a data model. The most important attributes for our use case are score, timestamp, and whether the player is human or AI (artificial intelligence).

The infinispan-client extension uses the ProtoStream library to generate a .proto schema file and marshaller implementation from the minimal annotations in our class.

As shown in this snippet, the @ProtoField annotation determines which POJO (plain old Java object) fields are included in the schema. @ProtoFactory recreates an instance of the POJO during deserialization. The @ProtoDoc annotation adds indexing metadata:

package com.redhat.model;

import org.infinispan.protostream.annotations.ProtoDoc;
import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;

@ProtoDoc("@Indexed")
public class PlayerScore {  

   private String userId;
   private String matchId;
   private String gameId;
   private String username;
   private Boolean human;
   private Integer score;
   private Long timestamp;

   @ProtoFactory
   public PlayerScore(String userId, String matchId, String gameId, String username, Boolean human, Integer score, Long timestamp) {
      this.userId = userId;
      this.matchId = matchId;
      this.gameId = gameId;
      this.username = username;
      this.human = human;
      this.score = score;
      this.timestamp = timestamp;
   }

   @ProtoField(number = 1)
   public String getUserId() {
      return userId;
   }

   @ProtoField(number = 2)
   public String getMatchId() {
      return matchId;
   }

   @ProtoField(number = 3)
   @ProtoDoc("@Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)")
   public String getGameId() {
      return gameId;
   }

   @ProtoField(number = 4)
   public String getUsername() {
      return username;
   }

   @ProtoField(number = 5)
   @ProtoDoc("@Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)")
   public Boolean isHuman() {
      return human;
   }

   @ProtoField(number = 6)
   @ProtoDoc("@Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)")
   @ProtoDoc("@SortableField")
   public Integer getScore() {
      return score;
   }

   @ProtoField(number = 7)
   @ProtoDoc("@Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)")
   @ProtoDoc("@SortableField")
   public Long getTimestamp() {
      return timestamp;
   }
}

Now, take a look at the generated schema in player-score.proto:

syntax = "proto2";

package com.redhat;

/**
 * @Indexed
 */
message PlayerScore {

   /**
    * @Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)
    */
   optional string userId = 1;
   optional string matchId = 2;

   /**
    * @Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)
    */
   optional string gameId = 3;
   optional string username = 4;

   /**
    * @Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)
    */
   optional bool human = 5;

   /**
    * @Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)
    * @SortableField
    */
   optional int32 score = 6;

   /**
    * @Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)
    * @SortableField
    */
   optional int64 timestamp = 7;
}

Note that the process to generate the Protobuf (Protocol Buffers) schema happens on the client side. We still needed to register the generated schema with our Data Grid cluster to marshal our entries correctly. Fortunately, that was very easy because the infinispan-client extension for Quarkus did the hard work.

The leaderboard service

Once the scoring service started tracking and updating scores in our players-scores cache, we needed to get the top 10 players in real time and display them on our leaderboard.

We created a full-text query with the Ickle query language to find the top 10 players at any given time. We used the following QueryFactory to order players by score and return a maximum of 10 results:

QueryFactory queryFactory = Search.getQueryFactory(playersScores);
Query topTenQuery = queryFactory
.create("from com.redhat.PlayerScore p WHERE p.human=true ORDER BY p.score DESC, p.timestamp ASC")
.maxResults(10);
List<PlayerScore> topTen = topTenQuery.execute().list();

Note: You can view the complete code in the scoring service and leaderboard service GitHub repositories.

To get continuous results in the correct order, we used the Quarkus Scheduler extension. We then needed to continuously push the top 10 players to the leaderboard, which we were able to do with the Quarkus Websockets extension:

@ServerEndpoint("/leaderboard")
@ApplicationScoped
public class LeaderboardEndpoint {
   ...
   private Map<String, Session> sessions = new ConcurrentHashMap<>();
   ...  

   @OnOpen
   public void onOpen(Session session) {
      sessions.put(session.getId(), session);
      LOGGER.info("Leaderboard service socket opened");
      broadcast();
   }

   @Scheduled(every = "1s")
   public void broadcast() {
      if(sessions.isEmpty()) {
         return;
      }

      List<PlayerScore> topTen = topTenQuery.execute().list()

      sessions.values().forEach(s -> s.getAsyncRemote().sendObject(topTen.toString(), result -> {
         if (result.getException() != null) {
            LOGGER.error("Leaderboard service got interrupted", result.getException());
         }
      }));
   }

   @OnClose
   public void onClose(Session session) {
      sessions.remove(session.getId());
      LOGGER.info("Leaderboard Service session has been closed");
   }

   @OnError
   public void onError(Session session, Throwable throwable) {
      sessions.remove(session.getId());
      LOGGER.error("Leaderboard Service session error", throwable);
   }
}

Configuring the global leaderboard across the hybrid cloud

Finally, after we had our services and Data Grid clusters set up in each region, we needed to calculate a global leaderboard of the top 10 players across Amazon Web Services, Google Cloud Platform, and Microsoft Azure.

For this, we used Data Grid's cross-site replication, as shown in Figure 3.

A diagram of the cross-site replication architecture.
Figure 3: Cross-site replication.
Figure 3: Cross-site replication between three hybrid cloud deployments.

Cross-site replication

To make data available across all regions, we configured each players-score cache to asynchronously replicate data to the other Data Grid clusters. As players actively battled each other on all three data centers, the players-score cache received updates from all Data Grid clusters simultaneously.

Here is the cache configuration on Azure:

<distributed-cache name="players-score" ...> 
<!-- index and encoding configuration. -->
   <backups>
      <backup site="AWS" strategy="ASYNC" enabled="true">
         <take-offline min-wait="60000" after-failures="3" /> 
      </backup>
      <backup site="GCP" strategy="ASYNC" enabled="true"> 
         <take-offline min-wait="60000" after-failures="3" />
      </backup> 
   </backups>
</distributed-cache>

Conclusion

In this article, we’ve explained how we built a system with Data Grid and Quarkus extensions to create a global ranking of game players across a hybrid cloud deployment. See Building cloud-native applications with AI/ML for a recap of Burr Sutter's live demonstration at Red Hat Summit 2021 in April.

We hope the demonstration and this article inspire you to use Red Hat Data Grid or the Infinispan project for your own use cases.

Last updated: November 6, 2023

Related Posts

  • Using JBoss DataGrid in Openshift PaaS

  • Enabling LDAP Security for DataGrid Cache

  • Infinispan’s Java 8 Streams Capabilities

  • JCache and Infinispan - standardize your application&#039;s cache

Recent Posts

  • GuideLLM: Evaluate LLM deployments for real-world inference

  • Unleashing multimodal magic with RamaLama

  • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

  • Streamline multi-cloud operations with Ansible and ServiceNow

  • Automate dynamic application security testing with RapiDAST

Red Hat Developers logo LinkedIn YouTube Twitter Facebook

Products

  • Red Hat Enterprise Linux
  • Red Hat OpenShift
  • Red Hat Ansible Automation Platform

Build

  • Developer Sandbox
  • Developer Tools
  • Interactive Tutorials
  • API Catalog

Quicklinks

  • Learning Resources
  • E-books
  • Cheat Sheets
  • Blog
  • Events
  • Newsletter

Communicate

  • About us
  • Contact sales
  • Find a partner
  • Report a website issue
  • Site Status Dashboard
  • Report a security problem

RED HAT DEVELOPER

Build here. Go anywhere.

We serve the builders. The problem solvers who create careers with code.

Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

Sign me up

Red Hat legal and privacy links

  • About Red Hat
  • Jobs
  • Events
  • Locations
  • Contact Red Hat
  • Red Hat Blog
  • Inclusion at Red Hat
  • Cool Stuff Store
  • Red Hat Summit

Red Hat legal and privacy links

  • Privacy statement
  • Terms of use
  • All policies and guidelines
  • Digital accessibility

Report a website issue