All blogs

Backend · · 11 min read

How I Scaled a Leaderboard for 250K+ Users

From MySQL Bottlenecks to Redis and Kubernetes

How I solved a leaderboard performance bottleneck, tested the application under simulated traffic, and scaled the infrastructure to handle a growing user base.

Raviteja Salva

Tech stack: NestJS · MySQL · Redis · Inngest · Artillery · Docker · Kubernetes

Everything Was Working Fine. Until the Traffic Spiked.

One of the most challenging problems I've faced as a full-stack developer involved a leaderboard system for a client application.

The concept was simple.

Users completed daily missions, earned points, and competed against each other on daily, weekly, and monthly leaderboards.

Initially, everything was working as expected.

Then the application started attracting a large number of users in a short period.

Suddenly, thousands of users were completing missions, earning points, and checking their leaderboard rankings.

The application had grown to more than 250,000 users, and the leaderboard started becoming a serious performance bottleneck.

The problem wasn't with the leaderboard functionality itself. It was with how the rankings were being calculated.

What worked perfectly with a smaller user base was no longer efficient at scale.

Here's how I approached and fixed the problem.

Stage 1: The bottleneck

1Understanding the Scale

Before changing anything, I needed to understand the workload the application was handling.

The application had:

  • More than 250,000 users.
  • At least 15 daily missions available to each user.
  • Points awarded for completing missions.
  • Daily, weekly, and monthly leaderboards.
  • Users frequently checking their rankings.

Let's calculate the potential workload.

Potential mission completions
  • Daily3.75M
  • Weekly26.25M
  • Monthly (30 days)112.5M

If all 250,000 users completed 15 missions every day, the application could generate 3.75 million mission completions daily.

Over a week, that would be 26.25 million.

Over a 30-day month, more than 112 million.

These numbers represent potential activity, not actual recorded completions.

Even if only a portion of users completed their missions, the number of points transactions would still grow significantly over time.

And every leaderboard request needed to return rankings based on those transactions.

That's where the problem started.

2The Original Problem: MySQL Was Doing Too Much Work

Initially, I was calculating leaderboard rankings directly from MySQL.

Whenever a user completed a mission, the points transaction was stored in the database.

When someone opened the leaderboard, the backend calculated rankings by fetching the relevant transactions, grouping them by user, calculating total points, and sorting users based on their scores.

This approach worked well with a smaller dataset.

But as the application grew, the transaction table became increasingly large.

Every leaderboard request required MySQL to perform expensive aggregation operations.

Now imagine thousands of users requesting leaderboard rankings while others were continuously completing missions.

The database was handling both transactional operations and repeated leaderboard calculations.

The same expensive calculations were being performed again and again.

I realized something important.

The problem wasn't just that the database queries were slow. The problem was that I was asking the database to repeatedly calculate information that could already be maintained.

That realization changed my approach.

Stage 2: Redis sorted sets

3Moving Leaderboard Calculations to Redis

Instead of trying to optimize the same MySQL queries repeatedly, I started looking for a better way to maintain rankings.

That's when I decided to use Redis Sorted Sets.

A Redis Sorted Set is a data structure that stores users along with their scores and keeps them ordered based on those scores.

This makes it particularly suitable for leaderboard systems.

Instead of calculating a user's total points from thousands of historical transactions every time someone opened the leaderboard, I could maintain the user's score as they earned points.

Whenever a user completed a mission, their leaderboard score could be incremented.

When someone requested the leaderboard, Redis could return the highest-ranked users directly.

The difference was significant from an architectural perspective.

Previously, every leaderboard request triggered expensive database calculations.

After the change, leaderboard requests could retrieve already-maintained rankings.

I moved the expensive aggregation work out of the leaderboard request path.

Before · every requestGET/leaderboardMySQLfetch, group, total, sort

Every leaderboard request required MySQL to perform expensive aggregation operations.

After · out of the request pathMissioncompletedMySQLtransactionRedisscore +pointsGET/leaderboardRedistop users, in order
The leaderboard request path, before and after.

MySQL continued storing the actual points transactions, while Redis handled ranking and retrieval.

4Why I Used Redis Only for Temporary Leaderboards

One important decision was how long leaderboard data should remain in Redis.

The application only required three types of leaderboards:

  • Daily leaderboard
  • Weekly leaderboard
  • Monthly leaderboard

I didn't need to store leaderboard rankings permanently in Redis.

MySQL already contained the actual points transaction history.

So I decided to use Redis as a temporary ranking layer.

Each leaderboard period had its own data.

When a new day started, the application used a new daily leaderboard.

The same approach applied to weekly and monthly leaderboards.

I also used expiration policies so that old leaderboard data wouldn't remain in Redis indefinitely.

This was important because continuously storing new leaderboard data without removing old periods would unnecessarily increase memory consumption.

Redis supports persistence, but I deliberately used it for temporary, rebuildable ranking data in this architecture.

This approach gave me three advantages.

First, faster leaderboard retrieval. Redis maintained the ranking order, so I didn't need to calculate it from MySQL for every request.

Second, controlled memory usage. Old leaderboard periods could expire automatically after their retention window.

Third, data reliability. MySQL remained the source of truth, meaning the leaderboard data could be reconstructed if necessary.

I wasn't replacing MySQL with Redis.

I was giving each technology a responsibility that suited it.

5Handling Leaderboard Updates With NestJS and Inngest

My backend was built using NestJS, and Inngest was part of the background-processing infrastructure.

I separated the responsibilities of storing points transactions and maintaining leaderboard rankings.

When a user completed a mission, the backend validated the completion and stored the points transaction in MySQL.

The corresponding leaderboard score then needed to be updated in Redis.

Background processing helped keep the responsibilities separated and allowed work to be processed outside the immediate leaderboard retrieval flow.

However, there was another important consideration.

What if a background job failed and retried?

If the same points transaction was applied twice, the user could receive extra leaderboard points.

That meant the system needed to handle duplicate events and retries carefully.

I had to consider not only performance but also consistency between MySQL transactions and Redis rankings.

Another trade-off was that asynchronously updated leaderboards might briefly lag behind completed missions.

For this kind of architecture, reliable event processing, duplicate protection, and reconciliation are just as important as fast ranking queries.

A fast leaderboard is useless if the rankings are incorrect.

Stage 3: Load testing

6The Leaderboard Was Fixed. But Was Production Ready?

After redesigning the leaderboard, I had addressed the expensive MySQL aggregation problem.

But I had another question.

Would the entire application actually handle a sudden spike in traffic?

At that point, the application was running in a single Docker-hosted environment.

The API, background services, and supporting infrastructure depended on a single deployment host.

Even though the leaderboard logic was more efficient, the infrastructure still had limitations.

A single machine has finite CPU, memory, and network capacity.

If traffic increased significantly, the application could still struggle even with optimized leaderboard queries.

I didn't want to wait for another real-world traffic spike to discover those limitations.

So I decided to simulate production-like traffic in my development environment.

7Load Testing With Artillery

To test the application under heavier workloads, I used Artillery, an open-source load-testing tool.

Artillery allowed me to simulate multiple users making requests concurrently.

Instead of manually opening the application and checking whether it worked, I could generate controlled traffic and observe how the backend behaved.

I focused on scenarios such as users completing missions, earning points, and requesting daily, weekly, and monthly leaderboards.

I wanted to understand what happened when many users interacted with the system at the same time.

During testing, I could examine important metrics such as response times, request throughput, failed requests, and resource utilization.

I also wanted to identify whether the application remained responsive as the number of concurrent operations increased.

This was particularly useful because manual testing rarely exposes the same problems as high-concurrency workloads.

A feature might respond quickly when one user requests it, but behave very differently when hundreds or thousands of users access it simultaneously.

Artillery helped me test those conditions in a repeatable way.

Of course, a local development environment isn't identical to production. Differences in hardware, networking, and database capacity affect the results.

But load testing gave me a much better understanding of the application's behavior and helped me evaluate the architectural changes before relying on them under real traffic.

Stage 4: Kubernetes

8The Next Challenge: A Single Docker Deployment

After addressing the leaderboard bottleneck, I turned my attention to the production infrastructure.

The application was still deployed on a single Docker host.

This was convenient during the initial development stages because the services could be managed together.

But as the user base grew, I saw the limitations of keeping everything on one machine.

Even if the application logic was efficient, the services were still competing for the same underlying resources.

And if that host experienced problems, multiple parts of the application could be affected.

I needed an architecture where the API, background workers, and databases could scale more independently.

So I made two major infrastructure changes.

First, I moved MySQL and Redis out of the Docker-hosted setup and into managed database services.

This separated the databases from the application host and allowed database resources to be managed independently.

Second, I migrated the application workloads to Kubernetes.

Instead of running the application as a single-host deployment, Kubernetes allowed me to run multiple instances of the services and distribute workloads across them.

Docker containers were still used, but Kubernetes now handled their orchestration.

This was a major improvement in how I could operate and scale the application.

9Scaling the Application With Kubernetes

After moving to Kubernetes, I configured multiple replicas for the application services.

The deployment included:

Kubernetes cluster
  • NestJS API4 replicas
  • Henchmen4 replicas · background workers
  • Inngest2 replicas
  • ManagedMySQL2 instances/replicas
  • ManagedRedisManaged service
The deployment after moving to Kubernetes.

The database configuration was managed separately from the Kubernetes application replicas.

API — 4 Replicas

The NestJS API handled incoming application requests.

Instead of relying on one instance, I could distribute incoming traffic across four API replicas.

This allowed multiple application instances to handle requests concurrently and reduced dependency on a single API process.

Henchmen — 4 Replicas

Henchmen handled background jobs.

I configured four replicas to provide additional processing capacity for background workloads.

With multiple workers, I also needed to ensure that jobs were coordinated correctly and that retries wouldn't result in duplicate processing.

Inngest — 2 Replicas

Inngest was another part of the background workflow infrastructure.

I configured two instances as part of the deployment.

The important consideration was making sure the instances operated correctly together, rather than simply assuming that additional replicas would automatically improve throughput.

Managed MySQL and Redis

I also separated MySQL and Redis from the application containers.

The databases no longer needed to compete directly with API and worker containers for resources on the original host.

Managed infrastructure also reduced the operational responsibility of running those databases inside the application deployment.

The main advantage of the new architecture was flexibility.

I could adjust application replicas based on traffic and background workload requirements without treating the entire application as one inseparable deployment.

10The Architecture After the Changes

The final architecture separated the system into distinct responsibilities.

NestJS handled incoming API requests and application logic.

MySQL stored mission activity, points transactions, and other transactional data.

Redis maintained temporary daily, weekly, and monthly leaderboard rankings.

Inngest and Henchmen supported background processing.

Kubernetes orchestrated multiple application and worker replicas.

Managed database services provided database infrastructure independently from the application workloads.

The most important change was how these components worked together.

When users completed missions, the system recorded their points transactions and updated the corresponding leaderboard scores.

When users requested rankings, the application retrieved them from Redis rather than recalculating them from the entire transaction history.

And when traffic increased, multiple API replicas could serve incoming requests.

This architecture addressed both the original leaderboard bottleneck and the limitations of the single-host deployment.

It also created a better foundation for future scaling, although additional replicas and managed services still required appropriate monitoring, capacity planning, and failure handling.

What I learned.

11What I Learned From This Experience

This experience taught me that performance optimization and system scalability are related, but they aren't the same thing.

I started with a leaderboard performance problem.

Then I realized that fixing the leaderboard wasn't enough if the underlying infrastructure couldn't handle the application's traffic.

A few lessons stood out.

  1. Don't repeatedly calculate what you can maintain incrementally.

    The biggest improvement came from changing how leaderboard rankings were maintained, not just optimizing the original SQL queries.

  2. Temporary data should have a clear lifecycle.

    Daily, weekly, and monthly leaderboards didn't need to remain in Redis forever. Expiration policies helped keep the data lifecycle manageable.

  3. Choose technologies based on their responsibilities.

    MySQL was the source of truth for transactions. Redis was responsible for fast ranking retrieval. Each solved a different problem.

  4. Load testing is an essential part of backend development.

    Artillery helped me evaluate how the application behaved under simulated traffic instead of relying only on manual testing.

  5. A single-host deployment can become an infrastructure bottleneck.

    Even with efficient application logic, the underlying infrastructure needs enough capacity and resilience.

  6. Horizontal scaling requires careful architecture.

    Running multiple replicas introduces additional considerations around background jobs, database connections, event processing, and consistency.

Final Thoughts

What started as a simple leaderboard performance issue turned into one of the most valuable scalability challenges I've worked on.

The application had grown to more than 250,000 users, with the potential to generate millions of mission completions every day.

Initially, I was calculating leaderboard rankings directly from MySQL transactions.

As the data grew, those repeated aggregations became a bottleneck.

I redesigned the leaderboard using Redis Sorted Sets, maintained temporary daily, weekly, and monthly rankings, and kept MySQL as the transactional source of truth.

After fixing the leaderboard, I used Artillery to simulate heavier traffic and evaluate the application's behavior.

That led me to address the infrastructure itself by moving MySQL and Redis to managed services and migrating application workloads to Kubernetes with multiple replicas.

The biggest lesson I took away from this experience is simple:

Scaling isn't just about making a slow query faster. It's about understanding how every part of the system behaves when the number of users suddenly increases.

Sometimes, solving one bottleneck is only the beginning of discovering the next one.

Raviteja SalvaFull-Stack Developer · NestJS · Redis · MySQL · Kubernetes · Scalable Backend SystemsLet's talk
Keep readingAll blogs
Contact

Let's build something remarkable.

Have a role, a project, or a hard problem worth talking through? Email is the fastest way to reach me, and everything else below works too.

Available for full-time roles
ResponseWithin 24 hours
LocationPune, India · IST
Work modeRemote / relocation

Open to full-time engineering roles, contract work, and technical conversations. No pitch necessary; a few lines about what you're building is enough.