Extending Akka

Akka extensions can be used for almost anything, they provide a way to create an instance of a class only once for the whole ActorSystem and be able to access it from anywhere. Akka features such as Cluster, Serialization and Sharding are all Akka extensions. Below is the use-case of managing an expensive database connection pool and accessing it from various places in your application.

You can choose to have your Extension loaded on-demand or at ActorSystem creation time through the Akka configuration. Details on how to make that happens are below, in the Loading from Configuration section.


Since an extension is a way to hook into Akka itself, the implementor of the extension needs to ensure the thread safety and that it is non-blocking.

Building an extension

Let’s build an extension to manage a shared database connection pool.

class ExpensiveDatabaseConnection {
  def executeQuery(query: String): Future[Any] = ???
public static class ExpensiveDatabaseConnection {
  public CompletionStage<Object> executeQuery(String query) {
    throw new RuntimeException("I should do a database query");
  // ...

First create an ExtensionExtension, this will be created only once per ActorSystem:

class DatabasePool(system: ActorSystem[_]) extends Extension {
  // database configuration can be laoded from config
  // from the actor system
  private val _connection = new ExpensiveDatabaseConnection()

  def connection(): ExpensiveDatabaseConnection = _connection
public static class DatabaseConnectionPool implements Extension {
  private final ExpensiveDatabaseConnection _connection;

  public DatabaseConnectionPool(ActorSystem<?> system) {
    // database configuration can be loaded from config
    // from the actor system
    _connection = new ExpensiveDatabaseConnection();

  public ExpensiveDatabaseConnection connection() {
    return _connection;

This is the public API of your extension. Internally in this example we instantiate our expensive database connection. The DatabaseConnectionPool can be looked up this way any number of times and it will return the same instance.

Then create an ExtensionIdExtensionId to identify the extension.

object DatabasePool extends ExtensionId[DatabasePool] {
  // will only be called once
  def createExtension(system: ActorSystem[_]): DatabasePool = new DatabasePool(system)

  // Java API
  def get(system: ActorSystem[_]): DatabasePool = apply(system)
public static class DatabaseConnectionPoolId extends ExtensionId<DatabaseConnectionPool> {

  private static final DatabaseConnectionPoolId instance = new DatabaseConnectionPoolId();

  private DatabaseConnectionPoolId() {}

  // called once per ActorSystem
  public DatabaseConnectionPool createExtension(ActorSystem system) {
    return new DatabaseConnectionPool(system);

  public static DatabaseConnectionPool get(ActorSystem<?> system) {
    return instance.apply(system);

Then finally to use the extension it can be looked up:

Behaviors.setup[Any] { ctx =>
  DatabasePool(ctx.system).connection().executeQuery("insert into...")
    (context) -> {
          .executeQuery("insert into...");
      return initialBehavior();

Loading from configuration

To be able to load extensions from your Akka configuration you must add FQCNs of implementations of the ExtensionId in the akka.actor.typed.extensions section of the config you provide to your ActorSystem.

akka.actor.typed.extensions = ["docs.akka.extensions.DatabasePool"]
ruby akka.actor.typed { extensions = ["jdocs.akka.extensions.ExtensionDocTest$DatabaseConnectionPoolId"] }
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.