Schedulers in Akka using Scala — Use Case: Flipkart
Introduction
Akka provides powerful scheduling capabilities to execute tasks at a fixed interval or after a certain delay. This feature is useful for scenarios like background tasks, periodic health checks, cache invalidation, and more. In this blog, we’ll explore Akka Schedulers using Scala and demonstrate how Flipkart (an e-commerce platform) can leverage them to enhance its operations.
Why Use Schedulers in Akka?
Schedulers in Akka help perform asynchronous and periodic operations without blocking threads. Some common use cases include:
- Refreshing Cache: Refreshing frequently accessed product details, prices, or offers.
- Sending Notifications: Scheduling push notifications for order updates.
- Periodic Health Checks: Ensuring microservices are running efficiently.
- Retry Mechanism: Retrying failed tasks, like payment processing.
Flipkart Use Case: Auto-Refreshing Product Deals
Consider Flipkart’s Deals of the Day feature, where top deals and discounts update every hour. Instead of manually refreshing deals, Flipkart can use an Akka Scheduler to update offers periodically.
Implementing Akka Schedulers in Scala
Step 1: Setting Up Akka ActorSystem
First, define an ActorSystem to handle scheduled tasks:
import akka.actor.{Actor, ActorSystem, Props}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
object FlipkartScheduler extends App {
// Create an ActorSystem
implicit val system: ActorSystem = ActorSystem("FlipkartSystem")
// Schedule periodic refresh
system.scheduler.schedule(initialDelay = 0.seconds, interval = 1.hour) {
println("Refreshing Deals of the Day...")
}
}
Step 2: Using an Actor to Handle Scheduled Tasks
Instead of printing, let’s use an Actor to refresh deals:
class DealRefresher extends Actor {
override def receive: Receive = {
case "refresh" =>
println("Fetching latest deals from the database and updating the cache...")
// Business logic to fetch & update deals
}
}
object FlipkartScheduler extends App {
implicit val system: ActorSystem = ActorSystem("FlipkartSystem")
val dealRefresher = system.actorOf(Props[DealRefresher], "dealRefresher")
// Schedule refresh every hour
system.scheduler.schedule(0.seconds, 1.hour, dealRefresher, "refresh")
}
Explanation
schedule
: Executes the task at fixed intervals.dealRefresher ! "refresh"
: Sends a message to the actor, triggering the deal refresh logic.
Advanced Use Case: Dynamic Scheduling Based on Traffic
Flipkart experiences high traffic during the Big Billion Days sales. To handle this, we can dynamically adjust the scheduling interval:
import akka.actor.{ActorRef, Cancellable}
var refreshTask: Option[Cancellable] = None
def adjustScheduler(interval: FiniteDuration): Unit = {
// Cancel the previous schedule if it exists
refreshTask.foreach(_.cancel())
// Start a new schedule with updated interval
refreshTask = Some(system.scheduler.schedule(0.seconds, interval, dealRefresher, "refresh"))
}
// Example: Increase refresh frequency during sales
adjustScheduler(30.minutes) // Refresh deals every 30 minutes instead of 1 hour
Conclusion
Akka Schedulers provide a reliable and non-blocking way to handle periodic tasks. Flipkart can leverage this for automated deal updates, order notifications, and dynamic scheduling based on traffic. This ensures a seamless shopping experience for millions of users.