I've been studying Akka for some time and I am building an app that utilizes the actor model and requires to maintain a connection to the database, but I have a dilemma: Where do I put this connection and how can I manage it?
This is my reasoning so far:
- The connection should be initialized only once.
- Since the connection represents a state, it should reside inside an actor. Let's call this actor DatabaseConnection (just to be creative).
- Since I don't want to share the actor's state, all querying should happen inside this actor.
- The database driver I am using is reactive-mongo so each query returns a Future that can be piped to the sender.
Even though the querying is done through Futures, I can't help thinking that this model cannot scale. Only one thread managing all database access messages? It sounds unreasonable even utilizing Futures. I have thought of making child workers to manage database querying but I would have to share the connection with the children. I like this last idea because if the DatabaseConnection actor dies, all its children die as well in theory. But I don't know if there is another better way to solve this problem without sharing state. Is there?