Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared database connection pools #216

Merged
merged 1 commit into from
Oct 30, 2020

Conversation

slashdotdash
Copy link
Member

@slashdotdash slashdotdash commented Aug 20, 2020

By default each event store will start its own Postgrex database connection pool. The size of the pool is configured with the pool_size config option.

When you have multiple event stores running you will also end up with multiple connection pools. If they are all connecting to the same physical Postgres database then it can be useful to share a single pool amongst all event stores. This pull request adds the shared_connection_pool config option to specify a name for the shared connection pool. Then you can configure the event stores you'd like to share the pool by using the same name.

This can be done in config:

# config/config.exs
config :my_app, MyApp.EventStore1, shared_connection_pool: :shared_pool
config :my_app, MyApp.EventStore2, shared_connection_pool: :shared_pool
config :my_app, MyApp.EventStore3, shared_connection_pool: :shared_pool

Or when starting the event stores, such as via a Supervisor:

Supervisor.start_link(
  [
    {MyApp.EventStore, name: :eventstore1, shared_connection_pool: :shared_pool},
    {MyApp.EventStore, name: :eventstore2, shared_connection_pool: :shared_pool},
    {MyApp.EventStore, name: :eventstore3, shared_connection_pool: :shared_pool}
  ],
  opts
)

Not specifying the shared_connection_pool config option indicates the event store will start its own connection pool. This is the existing behaviour. Not using this new shared pool feature should have no affect.

Closes #198.

@slashdotdash
Copy link
Member Author

slashdotdash commented Aug 21, 2020

Database connections used by an event store instance are tied to whatever schema has been configured for the event store. This is because the Postgres search_path is set for the session once connected to the database by using the :after_connect config option passed to Postgrex. The purpose of doing this was to not need to prefix the database queries with the schema name as it would require looking up the schema from the event store config each time a database request is made via the EventStore public API.

This limitation means that sharing a connection pool won't work as expected if the event stores have been configured to use different schemas which is highly likely. Thus sharing the connection pool requires further work to include the event store schema when making all database requests.

slashdotdash added a commit that referenced this pull request Oct 18, 2020
@slashdotdash slashdotdash force-pushed the feature/shared-database-connections branch from d7bd0c9 to 1a8b65a Compare October 18, 2020 22:37
slashdotdash added a commit that referenced this pull request Oct 30, 2020
@slashdotdash slashdotdash force-pushed the feature/shared-database-connections branch from 1a8b65a to b990cde Compare October 30, 2020 12:33
slashdotdash added a commit that referenced this pull request Oct 30, 2020
@slashdotdash slashdotdash force-pushed the feature/shared-database-connections branch from b990cde to 84e04dc Compare October 30, 2020 14:41
slashdotdash added a commit that referenced this pull request Oct 30, 2020
@slashdotdash slashdotdash force-pushed the feature/shared-database-connections branch from 84e04dc to 5bc89fa Compare October 30, 2020 14:55
@slashdotdash slashdotdash force-pushed the feature/shared-database-connections branch from 5bc89fa to 9c67f78 Compare October 30, 2020 15:37
@slashdotdash slashdotdash merged commit 33dea31 into master Oct 30, 2020
@slashdotdash slashdotdash deleted the feature/shared-database-connections branch October 30, 2020 15:49
@apolkk
Copy link

apolkk commented Nov 19, 2024

Database connections used by an event store instance are tied to whatever schema has been configured for the event store. This is because the Postgres search_path is set for the session once connected to the database by using the :after_connect config option passed to Postgrex. The purpose of doing this was to not need to prefix the database queries with the schema name as it would require looking up the schema from the event store config each time a database request is made via the EventStore public API.

This limitation means that sharing a connection pool won't work as expected if the event stores have been configured to use different schemas which is highly likely. Thus sharing the connection pool requires further work to include the event store schema when making all database requests.

Thank you for making this work on #217 🎖️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Share database connections between EventStore instances
3 participants