Using Redis with GCP for Real-Time Leaderboards
Real-time leaderboards are widely used in gaming, fitness apps, competitions, quizzes, and streaming platforms to display rankings that update instantly.
Redis—because of its in-memory speed, sorted sets, and atomic operations—is the ideal tool for this.
On Google Cloud Platform (GCP), Redis is provided through:
✅ Google Cloud Memorystore for Redis
A fully managed Redis service with high performance and easy scaling.
๐น 1. Why Use Redis for Leaderboards?
Redis has a special data type called a Sorted Set, which stores elements with scores and keeps them ordered automatically.
This makes Redis perfect for tasks like:
Adding points to a player
Retrieving top players instantly
Finding a player’s rank in real time
Updating scores with atomic (safe) operations
All operations run in O(log n) time and are extremely fast.
๐น 2. Basic Architecture on GCP
A typical setup for a real-time leaderboard on Google Cloud uses:
Client → API Layer (Cloud Run / GKE / App Engine) → Redis (Memorystore)
Recommended GCP Components:
Cloud Run or GKE → to run your app/service
Cloud Memorystore (Redis) → for leaderboard storage
Cloud Load Balancer → for traffic distribution
VPC Connector → required for Cloud Run ↔ Memorystore connection
Cloud Pub/Sub → optional, for async score updates
Cloud Monitoring → performance metrics
๐น 3. Setting Up Redis on GCP
Step 1 — Create a Redis Instance:
Go to GCP Console → Memorystore → Redis → Create Instance.
Choose:
Tier: Basic (dev) or Standard (prod)
Size: based on leaderboard scale
Region & zone
VPC network
This gives you a private IP address (for internal access only).
๐น 4. Using Redis Sorted Sets for a Leaderboard
Redis command summary:
Task Redis Command
Add or update score ZINCRBY
Set (replace) score ZADD
Get top N players ZREVRANGE
Get a player's rank ZREVRANK
Get a player's score ZSCORE
๐น 5. Example: Leaderboard Commands
Add or update a player’s score:
ZINCRBY game_leaderboard 50 "player_123"
Get top 10 players:
ZREVRANGE game_leaderboard 0 9 WITHSCORES
Get a player's rank:
ZREVRANK game_leaderboard "player_123"
Get a player's score:
ZSCORE game_leaderboard "player_123"
๐น 6. Using Redis with Node.js on GCP
Example Cloud Run service using Redis:
import express from "express";
import { createClient } from "redis";
const app = express();
const client = createClient({
socket: {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT
}
});
await client.connect();
// Increase score
app.post("/score/:playerId/:points", async (req, res) => {
const { playerId, points } = req.params;
const score = await client.zIncrBy("leaderboard", parseInt(points), playerId);
res.json({ playerId, score });
});
// Get top N players
app.get("/top/:n", async (req, res) => {
const n = parseInt(req.params.n);
const results = await client.zRangeWithScores("leaderboard", -n, -1, { REV: true });
res.json(results);
});
app.listen(8080);
Deploy using Cloud Run:
gcloud run deploy leaderboard-api \
--source . \
--set-env-vars REDIS_HOST=10.0.0.3,REDIS_PORT=6379 \
--vpc-connector=YOUR_CONNECTOR
๐น 7. Performance Best Practices
✔ Use Redis Sorted Sets
Fast, memory-efficient for leaderboards.
✔ Avoid scanning all items
Use ZREVRANGE or pagination.
✔ Keep scores small
Prefer ZINCRBY increments instead of replacing big values.
✔ Use pipelining
Batch multiple updates for better performance from Cloud Run.
✔ Enable eviction policies
To limit memory usage for very large leaderboards.
✔ Replication + Failover (Standard Tier)
Improves availability in production.
๐น 8. Scaling Leaderboards on GCP
If many users update scores concurrently:
✔ Use Cloud Run + Autoscaling
Instances scale based on traffic.
✔ Use separate leaderboards
Per region, per game mode, etc.
✔ Use sharded Redis (manually or via Redis Cluster on GKE)
For extremely large leaderboards.
✔ Use Pub/Sub for async scoring
If updates are heavy.
๐น 9. Optional Enhancements
You can extend Redis leaderboards on GCP with:
๐ฏ Caches
Store frequently used rank ranges.
๐ฏ Multi-leaderboard system
Daily, weekly, monthly leaderboards using key namespaces.
๐ฏ Backup to BigQuery
Periodic export of leaderboard snapshots.
๐ฏ Realtime updates
Use WebSockets or Firebase to push updates to users.
๐ฏ Summary
Redis + GCP is an excellent combination for building real-time leaderboards, thanks to:
Ultra-fast Redis Sorted Sets
Cloud Memorystore’s managed operation
Cloud Run/GKE scaling
Secure VPC-access integration
Simple API-based implementation
Redis handles score updates and rank queries with near-zero latency, making it perfect for gaming and real-time applications.
Learn GCP Training in Hyderabad
Read More
Processing Clickstream Data for Personalization in Real-Time
Real-Time Social Media Sentiment Analysis with Dataflow and BigQuery ML
Building an IoT Event Hub on Google Cloud
Using Cloud Run for On-Demand Real-Time Data Transformations
Visit Our Quality Thought Training Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments