Singleton Entity

lagom
akka
scala
akka-cluster

(Jules Ivanic) #1

Hi everyone,

We have a service which only role is to send notifications (emails, etc.).
For tracking purpose, sending a notification is an event.

We feel that there’s no possible entity in this service so we’re thinking about implementing something like that:

object SingletonEntity {
  final val singletonEntityId = "0"
}

trait SingletonEntity extends PersistentEntity {

  final override type State = String

  final override def initialState: String = SingletonEntity.singletonEntityId

  final override def behavior: Behavior = _ => constantState orElse singletonActions

  def singletonActions: Actions

  private final val constantState: Actions =
    Actions()
      .onEvent {
        case (_, state) => state
      }

}

I’d like to have your opinions.


(Andrew Moreland) #2

We model one entity per notification request for a few reasons:

(1) can easily implement scheduled delivery for delayed sending
(2) can track “attempted to send this notification but got a weird response from 3rd party provider” on a per-notification basis so you can implement custom retry logic.
(3) you get sharded processing of notifications. If you don’t need global sequencing on notifications to different end-users this lets things scale more easily.


(Pawel) #3

Does it work?