Who doesn’t love sports? Almost everyone has a sport they follow religiously or a team they defend earnestly. But how many times has a busy schedule or an emergency at work caused you to miss a game, especially when your favorite team is playing? We’ve all been there. That’s where the concept of a leaderboard/scoreboard comes in—it displays rankings or data points in a well-organized manner to keep you updated on what you missed.
This is where NCache comes in. By leveraging this distributed caching solution, you can ensure your leaderboard applications’ capacity for handling real-time updates. It minimizes latency and maximizes performance, ensuring you stay updated without missing any crucial information. Let’s explore how NCache delivers a seamless scoreboard experience.
Issues with Live Scoring Applications
Let’s suppose you want to create an application that takes in live updates from the match venues and displays them to all the users who have subscribed for the updates. If a match has already ended, users get a detailed score and the match results. Similarly, the subscribed user is also notified when a new match starts.
While many systems offer this functionality, live scoring applications often face issues with network glitches and delays. By utilizing distributed in-memory computing, NCache provides these applications with the speed and reliability needed to handle hundreds or thousands of users. It efficiently manages traffic, avoiding performance bottlenecks and crashes. As a distributed, linearly scalable datastore, NCache supports numerous client requests and ensures stable cache client connection. We have created a sample application demonstrating these capabilities, available on GitHub for you to explore.
NCache Leaderboard Application: An Overview
Let us start with an overview of what the application does. The basic functionality is divided into two parts:
- Subscribed user: A user subscribed to view the live score.
- Score Update User: A user updating the live score for the subscribed user.
Once the match begins, the score update team starts posting live updates to their APIs, which are then made available to licensed or subscribed users. Below is an overview of the application architecture:
The live notifications will be published to the NCache servers using the Publish Subscribe Model (where a publisher publishes a message associated with a specific topic). The subscriber receives an update whenever a change has been made to that topic. In our case, whenever a match update occurs, it will be published and all users subscribed to the match will receive immediate updates to the match score.
To ensure these updates are delivered instantly and efficiently, we use the NCache SignalR Backplane integration. This extension of ASP.NET SignalR facilitates smooth data flow across multiple servers in a web farm, ensuring all subscribers receive real-time notifications. Additionally, to ensure that users viewing match results after their conclusion can view complete match breakdowns. Moreover, the match handler utilizes NCache’s Backing Source providers to synchronize cache data with the database, maintaining consistency and reliability.
Using Pub/Sub for Match Updates
Using NCache Pub/Sub’s model, users subscribe to a topic for match updates. When the score updates, the team posts a score update, a message is published to that topic, and all subscribed users receive immediate notifications about the update. This ensures that subscribers always access the most current score. The code given below describes how to publish a message on a specific topic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Mention the name of the topic string topicName = "matchScore"; // Create the topic ITopic topic = cache.MessagingService.CreateTopic(topicName); // create start update MatchUpdate start = new MatchUpdate(datetime, "match_start", "stadium", "Match has Started"); // create new Message packet with the match update Message message = new Message(start); // Get the topic from the cache ITopic orderTopic = cache.MessagingService.GetTopic(topicName); // Publish the topic with delivery option set as all // and register message delivery failure matchScore.Publish(message, DeliveryOption.All, true); |
The code below shows how can a user subscribe to a specific topic
1 2 3 |
// Create and register subscribers for order topic // Message received callback is specified ITopicSubscription matchSubscription = topic.CreateSubscription(MessageReceived); |
Using SignalR Backplane to Receive updates on the Match
NCache’s SignalR Backplane integration is an advanced extension that maintains persistent connections with the users, subscribed for real-time updates. This integration eliminates frustrating delays and enhances the overall responsiveness of your application. Below is an example demonstrating how to register for NCache SignalR Backplane in your application:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Startup { public void Configuration(IAppBuilder app) { string cache = ConfigurationManager.AppSettings["cache"]; string eventKey = ConfigurationManager.AppSettings["eventKey"]; GlobalHost.DependencyResolver.UseNCache(cache, eventKey); // using NCache SignalR app.MapSignalR(); } } |
Backing Source for Reading/Writing data from the Data Source
To ensure your application remains synchronized with your database, especially for live updates like match scores, NCache offers data source providers that streamline this process. To learn about the Read-through and Write-through provider configuration, refer to the documentation.
- Read-through Provider: This provider retrieves data directly from the database when it is not available in the cache. It ensures that any cache misses are seamlessly handled by fetching the latest data from the data source.
- Write-through Provider: This provider keeps your data source synchronized with the cache by writing data to both locations simultaneously. This approach guarantees that your data remains consistent across both the cache and the database, effectively eliminating any risk of data inconsistency.
You need to implement the providers and add and deploy these using the NCache Management Center or the PowerShell Tools. Now, keeping the above two factors in mind, refer to the code below to synchronize your data source with your cache. Here we are discussing the Write-through provider.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public ICollection WriteToDataSource(ICollection operations) { var operationResult = new List(); foreach (WriteOperation operation in operations) { ProviderCacheItem cacheItem = operation.ProviderItem; MatchScore matchScore = cacheItem.GetValue(); // Assuming MatchScore is the relevant data type switch (operation.OperationType) { case WriteOperationType.Add: // Logic to add a new match score to the cache and the database break; case WriteOperationType.Delete: // Logic to delete a match score from the cache and the database success = DeleteMatchScore(matchScore); break; case WriteOperationType.Update: // Logic to update an existing match score in both the cache and the database success = UpdateMatchScore(matchScore); break; } // Write Thru operation status can be set according to the result operationResult.Add(new OperationResult(operation, OperationResult.Status.Success)); } return operationResult; } |
Conclusion
With NCache, you’ve learned to create your live scoreboard that effortlessly handles a large number of users and live updates. You can now say goodbye to delays and inconsistencies, and enjoy every goal and moment in real-time, no matter where you are. But that’s not all—NCache offers a range of powerful features and innovative use cases to enhance your applications even further. With NCache, you’re always in for a winning experience!
Digital scoreboards play a crucial role in providing real-time updates and information to sports fans. In the context of a live scoring application, having an efficient and reliable system is paramount. NCache offers a distributed caching solution that ensures optimal performance and scalability for handling a large number of users and their requests. By leveraging distributed in-memory computing, NCache minimizes network glitches and delays, providing a seamless experience for users. The importance of a well-functioning digital scoreboard cannot be overstated, and solutions like NCache contribute to enhancing the overall experience of sports enthusiasts.