Skip to content

AR Connection Management Guide - #29807

Closed
jjb wants to merge 6 commits into
rails:masterfrom
jjb:ar-connection-management-guide
Closed

jjb wants to merge 6 commits into
rails:masterfrom
jjb:ar-connection-management-guide

Conversation

@jjb

@jjb jjb commented Jul 16, 2017 •

Copy link
Copy Markdown
Contributor

A new guide for AR Connection management, coming out of discussion in #27759. see rendered markdown here: https://github.com/jjb/rails/blob/ar-connection-management-guide/guides/source/active_record_connection_management.md

This is my first pass with everything relevant I could think of. I've dealt with these issues in various ways over the years and I might be forgetting details or use cases. Please give me any and all feedback and I'll try to improve the document!

one question i have right off: am i remembering this correctly?

"In previous version of rails, you needed to think about managing connections when spawning threads, but this is no longer the case."

@rails-bot

Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @matthewd (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@jjb
jjb force-pushed the ar-connection-management-guide branch from 028410d to a7aa80a Compare July 16, 2017 02:38

@gaurish gaurish left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

However, should this we a separate guide or a seperate in the existing guide?

===================================

In general, Active Record will automagically manage database connections in an
effecient manner. See [Configuring a Database](/configuring.html#configuring-a-database) and [Database pooling](configuring.html#database-pooling) for information about the basics of optimally configuring your application to use available resources.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

efficient

* Configuring a multi-process web server
* Spawning processes

In previous version of rails, you needed to think about managing connections when

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for clarity, it would be useful to mention exact rails version since when this is no longer the case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know when this was and some research wasn't able to find it. Maybe someone else knows?

### Before forking

Before the server forks, you need to disconnect the pool so that the forked processes
don't get confused. Here's how to do that in a puma config:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"get confused" is too vague - we should describe the problem(s) that are avoided by disconnecting the pool.

@robin850 robin850 added this to the 5.2.0 milestone Jul 28, 2017
@robin850

Copy link
Copy Markdown
Member

Hi @jjb,

Thanks for the pull request ! Such guide would be effectively interesting. This pull request has been put under the 5.2 milestone as there most likely going to be improvements regarding connection management and particularly regarding multiple connection support.

I take the liberty to cross-link to this gist that might not be totally true but that can be relevant in the future once multiple connection support is present.

@arthurnn arthurnn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left two small comments.

* Spawning processes

In previous version of rails, you needed to think about managing connections when
spawning threads, but this is no longer the case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit confusing.
You still need to change the pool size depending on the number of threads you have.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arthurnn when i said "when spawning threads", i mean when creating new threads in your application code. regarding that, my statement is correct.

but it seems you thought i meant "you need to consider the number of threads your server is running when you decide how many connections to put in the pool".

if you have a suggestion how to clarify that it is the former and not the latter, let me know!

(or if i'm not understanding your feedback, let me know that too :-D)

my_worker.rb. In this script, you are going to start two long-running processes. Here's how you would go about doing that:

```ruby
ActiveRecord::Base.connection_pool.disconnect!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should mention that you won't be able to use the connection on the parent process anymore after this.
As you are disconnecting it.


When spawning processes, you must do a small amount of manual database pool management.

Let's say you you have a script that you are going to run as a working using `rails runner`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a worker ?

jjb added 2 commits August 19, 2017 18:16
whitespace only
clarifications
typo fixes
@jjb

jjb commented Aug 19, 2017

Copy link
Copy Markdown
Contributor Author

@gaurish

However, should this we a separate guide or a seperate in the existing guide?

in #27759, @arthurnn said that a standalone guide sounds like a good idea

@jjb

jjb commented Aug 19, 2017 •

Copy link
Copy Markdown
Contributor Author

@betesh @arthurnn @eugeneius @gaurish Thanks for the feedback! I addressed it all, take a look.

I also added a new section, The relationship between parent and child pools.

@jjb
jjb force-pushed the ar-connection-management-guide branch from b67dc7a to eb4f7f0 Compare August 19, 2017 22:21
@hmistry

hmistry commented Sep 13, 2017

Copy link
Copy Markdown
Contributor

@jjb I'm very pleased to see you writing about this as there is very little information on this topic and I couldn't find anyone who really understood how the connection pool behaves. I think your article is a good start but falls short in helping me truly understand how AR connection pool is working (at least for me, it doesn't answer all my questions regarding the connection pool).

Here's some things I'd like to see explained:

  1. What is the behavioral model of AR connection pool i.e. does it establish n connections on each process/thread/app, are those lazily created, when do they connect/disconnect in an app/request lifecycle, is it a total number or per process/thread/app number of connections, etc?
  2. Is there a general rule of db connections per thread/process/app/service/db? Heroku has a max db connection setting - should I simply set AR db connections to that or are there other things to consider like background services that use db?
  3. How does one go about calculating the correct number of connections (assume you're not CPU/RAM limited)? One way is to think of a formula e.g. # of connections = (# of processes x # of threads x fudge factor + something) OR (max database connections) AND (whatever else needs to be factored)
  4. How does this work with background jobs using AR or not using AR? Think if I were using Sidekiq with AR, is the Rails app AR connection pool shared with Sidekiq or not and so do we need to factor it into the connections count? What if I had my own background service using AR, is it still part of Rails AR or separate?
  5. How should I go about debugging AR connection timeouts in my log?
  6. Can a connection be stuck i.e. not released properly from db but no longer being used by app? How to detect that and release/reset it?

If you can explain all this in your guide, I think it would be tremendously helpful in understanding the AR connection management.

@hsgubert

hsgubert commented Sep 13, 2017 •

Copy link
Copy Markdown

Hey @jjb, interesting guide.

It took me a long time to understand how AR works, and a few months ago I wrote a general explanation about how connection managing plays out. It is a little more in depth than what you have written so far, but since people following this post are interested in the subject it might help:

https://github.com/hsgubert/rails-sharding/wiki/How-rails-sharding-connection-handling-works

@hmistry this could probably help you to answer some of your questions

@hmistry

hmistry commented Sep 15, 2017

Copy link
Copy Markdown
Contributor

@hsgubert Thank you for the link - it was very informative and helped me understand AR's connection architecture.

@jjb

jjb commented Sep 24, 2017

Copy link
Copy Markdown
Contributor Author

Hi folks - thanks for your feedback and ideas!

I think I've stretched my expertise on this topic and won't be able to improve the guide much more. If someone from rails has specific change requests, let me know. Or if someone with more knowledge on this than I wants to fork my repo and do another PR, feel free!

and [Database pooling](configuring.html#database-pooling) for information about the
basics of optimally configuring your application to use available resources.

Sometimes, you will need to manage Active Record connections and/or connecton

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connection**

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks!


Each process in a multi-process web server needs its own database connection pool.
If the server is also multi-threaded, the threads within each process will automatically
share that processes' connection pool.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe processes' is plural, and should be changed to either process's or process'.


```ruby
before_fork do
ActiveRecord::Base.connection_pool.disconnect!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or ActiveRecord::Base.clear_all_connections! ?

@matthewd

matthewd commented Dec 9, 2017

Copy link
Copy Markdown
Member

Good news!... and bad news. 😕

Between #31221, #31173, and #31241, I believe I've eliminated the need for all of this guidance: it should finally all Just Work without anyone needing to think about it.

@jjb

jjb commented Dec 9, 2017 •

Copy link
Copy Markdown
Contributor Author

@matthewd all good news as far as i'm concerned :-D

Skimming those PRs I see words like "Improve" and "seem to be". Are we sure that 100% of what's in this guide is now irrelevant? If so then we can close this, and I'll maybe make a gist and tweet it around for folks working with < 5.2(?) to find.

@matthewd

matthewd commented Dec 9, 2017

Copy link
Copy Markdown
Member

"Improve" is relative to the existing fork-safety measures, which already prevented connection re-use/stomping, and left us "only" with phantom closes.

And the seeming was more about whether #31221 is a reasonable solution for the full set of problems one might have wanted #23815 for.

Beyond that, my only hedging is in my feeling that some of these incantations have taken on mythical weight -- these changes address the last of the reasons I know of to add them.


A gist / blog post / etc somewhere sounds like a great idea! ❤️ (yep, < 5.2.)

@jjb

jjb commented Dec 9, 2017

Copy link
Copy Markdown
Contributor Author

@matthewd Gotcha.

If you've looked through my guide and are sure none of it is relevant for the cases described (yay!), go ahead and close this.

@matthewd matthewd closed this Dec 9, 2017
@jjb

jjb commented Feb 5, 2018

Copy link
Copy Markdown
Contributor Author

I've put my guide into this gist: https://gist.github.com/jjb/cacab45cbc5816238b633ed2228c8290

anthotsang added a commit to coinfi/coinfi that referenced this pull request May 15, 2023
anthotsang added a commit to coinfi/coinfi that referenced this pull request May 17, 2023
* Update to Rails 5.2.8.1 and Ruby to 2.7.6

* Update administrate assets

* Fix tests, including updating Faker format. Fix deprecated raw query methods

* Fix redis plan for heroku review apps

* Remove env vars from app.json that come from addons

* Change js compiler to terser to handle ES6

* Upgrade puma and associated config. Active Record management should no longer be necessary in puma rails/rails#29807 (comment)

* Update puma to v5 (wasn't updated in previous commit 68f4888)

* Add /tmp/pids folder for puma

* Update sidekiq and redis

* Downgrade sidekiq to v6 for now since redis cloud still doesn't support RESP3 protocol required by sidekiq v7 sidekiq/sidekiq#5594
anthotsang added a commit to coinfi/coinfi that referenced this pull request May 17, 2023
* Update to Rails 5.2.8.1 and Ruby to 2.7.6

* Update administrate assets

* Fix tests, including updating Faker format. Fix deprecated raw query methods

* Fix redis plan for heroku review apps

* Remove env vars from app.json that come from addons

* Change js compiler to terser to handle ES6

* Upgrade puma and associated config. Active Record management should no longer be necessary in puma rails/rails#29807 (comment)

* Update puma to v5 (wasn't updated in previous commit 68f4888)

* Add /tmp/pids folder for puma

* Update sidekiq and redis

* Downgrade sidekiq to v6 for now since redis cloud still doesn't support RESP3 protocol required by sidekiq v7 sidekiq/sidekiq#5594
anthotsang added a commit to coinfi/coinfi that referenced this pull request Oct 11, 2024
commit 5b270955973d409c454a154fc1322996ad4e54dc
Merge: fc9ee39 7ce2c92
Author: Anthony Tsang <[email protected]>
Date:   Fri Oct 11 14:24:17 2024 +0900

    Merge branch 'develop' into chore/merge-develop

commit 7ce2c92
Author: Anthony Tsang <[email protected]>
Date:   Fri Oct 11 14:21:32 2024 +0900

    fixup! Indicators dark mode (#948)

commit fc9ee39
Merge: 58d304c 60895ca
Author: Anthony Tsang <[email protected]>
Date:   Fri Oct 11 14:16:38 2024 +0900

    Merge branch 'develop' into chore/merge-develop

commit 60895ca
Author: anthotsang <[email protected]>
Date:   Fri Oct 11 13:37:52 2024 +0900

    Indicators dark mode (#948)

    * Add dark mode theme to indicators page. Remove old gauge-related code and assets

    * Add dark mode info icon

    * Update empty state dark mode text color

    * Add missing dark flask svg and chart bar color

commit 89fc147
Author: Anthony Tsang <[email protected]>
Date:   Thu Oct 3 14:18:54 2024 +0900

    Retry fetching github commit activity (sometimes returns nil even though activity exists)

commit 0f6b494
Author: Anthony Tsang <[email protected]>
Date:   Thu Oct 3 12:05:30 2024 +0900

    Remove npm declaration in favour of yarn

commit edd7b28
Author: Anthony Tsang <[email protected]>
Date:   Thu Oct 3 09:53:42 2024 +0900

    Provide results even if code frequency is missing

commit de2c9a4
Author: Anthony Tsang <[email protected]>
Date:   Fri Feb 16 15:16:22 2024 +0900

    Ignore code frequency for large repos - not available for repos with more than 10k commits

commit e31d443
Author: Anthony Tsang <[email protected]>
Date:   Fri Feb 16 15:15:47 2024 +0900

    Fix rounding error bug

commit 0a2a94a
Author: Anthony Tsang <[email protected]>
Date:   Thu Dec 7 10:17:59 2023 -0800

    Fix typo

commit e050fb1
Author: Anthony Tsang <[email protected]>
Date:   Thu Dec 7 08:56:31 2023 -0800

    Remove metadata argument for CopyPrices

commit c68d840
Author: Anthony Tsang <[email protected]>
Date:   Wed Dec 6 19:40:10 2023 -0800

    Add missing worker for bb930d6

commit 3dd94fc
Author: Anthony Tsang <[email protected]>
Date:   Wed Dec 6 16:07:18 2023 -0800

    Update price snapshot frequency (it's 200 coins/credit and it's based on the limit rather than the amount returned). Also refactor to avoid having to calculate start every time values are changed

commit d263a24
Author: Anthony Tsang <[email protected]>
Date:   Wed Dec 6 14:01:49 2023 -0800

    Reduce total credits spent for updating price snapshots

commit e044ef0
Author: Anthony Tsang <[email protected]>
Date:   Tue Dec 5 15:32:49 2023 -0800

    Reword UpdateCmcPrices worker for clarity

commit 7483ada
Author: Anthony Tsang <[email protected]>
Date:   Tue Dec 5 15:25:46 2023 -0800

    Stop updating market pairs

commit b6754ad
Author: Anthony Tsang <[email protected]>
Date:   Tue Dec 5 12:20:33 2023 -0800

    Add rake task to copy price data from production db

commit 2e04589
Author: Anthony Tsang <[email protected]>
Date:   Tue Dec 5 10:56:41 2023 -0800

    Remove market pairs

commit d7a89cb
Author: Anthony Tsang <[email protected]>
Date:   Mon Oct 23 13:48:03 2023 +0900

    Fix caching logic for Coin#prices_data

commit 8ee4a8b
Author: Anthony Tsang <[email protected]>
Date:   Mon Oct 23 13:13:25 2023 +0900

    Fix check time for CMC Ohcl check

commit 1e7980f
Author: Anthony Tsang <[email protected]>
Date:   Mon Oct 23 11:58:37 2023 +0900

    Fix incorrect where condition

commit abdc930
Author: Anthony Tsang <[email protected]>
Date:   Fri Oct 20 11:20:39 2023 +0900

    Delay adding user email to sendy until after the user is confirmed

commit 9d2a7f4
Author: Anthony Tsang <[email protected]>
Date:   Thu Oct 19 13:24:11 2023 +0900

    Add time to CheckCmcOhclvService. Add price check before updating indicators

commit 98c0f4e
Author: Anthony Tsang <[email protected]>
Date:   Wed Oct 18 17:15:36 2023 +0900

    Pass/Fail UpdateCMCDailyPrices worker based on check results. Add targeted downloads to avoid downloading all coins on retry

commit 8aadb60
Author: Anthony Tsang <[email protected]>
Date:   Wed Oct 18 13:14:55 2023 +0900

    Fix broken check (no coin_key for daily_ohcl_prices table). Add forced refresh to latest cached price data check

commit 7777539
Author: Anthony Tsang <[email protected]>
Date:   Wed Oct 18 12:30:34 2023 +0900

    Fix UpdateOhclService by adding error handling to get_cmc_coins API call and differentiating between coin ids and coin cmc_ids

commit 85d315e
Author: Anthony Tsang <[email protected]>
Date:   Wed Oct 18 11:09:23 2023 +0900

    Remove COINFI_POSTGREST_URL reliance and use local data instead. Remove unused hourly_prices_data. Reschedule cmc daily prices to better align with daily_prices refresh timing. Put off refactoring indicator calculations to rely directly on daily_ohcl_prices rather than prices_data

commit 4c858e0
Author: Anthony Tsang <[email protected]>
Date:   Wed Oct 18 09:51:57 2023 +0900

    Remove reliance on ETLDB for ohclv price data check. Fix unsuccessful response handling for ohcl update service

commit 5416ff1
Author: anthotsang <[email protected]>
Date:   Tue Oct 17 20:29:19 2023 +0900

    Remove etldb dependency (part one) (#947)

    * Remove remaining token metrics related code

    * Add daily ohcl price model

    * Schedule daily cmc price update and combine price check with said update

    * Revert unnecessary changes to schema

    * Fix SSL redis connections and use proper env vars for redis tasks

    * Upgrade review app postgres to minimum plan necessary for dblink

commit 35c992a
Author: anthotsang <[email protected]>
Date:   Wed May 17 09:57:33 2023 +0900

    Support SSL for Redis (#946)

    * Update to Rails 5.2.8.1 and Ruby to 2.7.6

    * Update administrate assets

    * Fix tests, including updating Faker format. Fix deprecated raw query methods

    * Fix redis plan for heroku review apps

    * Remove env vars from app.json that come from addons

    * Change js compiler to terser to handle ES6

    * Upgrade puma and associated config. Active Record management should no longer be necessary in puma rails/rails#29807 (comment)

    * Update puma to v5 (wasn't updated in previous commit 68f4888)

    * Add /tmp/pids folder for puma

    * Update sidekiq and redis

    * Downgrade sidekiq to v6 for now since redis cloud still doesn't support RESP3 protocol required by sidekiq v7 sidekiq/sidekiq#5594

commit 9ba5215
Author: Anthony Tsang <[email protected]>
Date:   Tue May 9 11:41:50 2023 +0900

    Remove unused views from schema

commit 32ca6c3
Author: Anthony Tsang <[email protected]>
Date:   Tue May 9 11:31:03 2023 +0900

    Add dataLoaded and dataLoadFailed iframe events

commit 7d115e2
Author: Anthony Tsang <[email protected]>
Date:   Fri Oct 7 14:32:36 2022 +0900

    Disable trading signals on coin#show PriceChart

commit d2011d1
Author: Anthony Tsang <[email protected]>
Date:   Fri Oct 7 11:06:13 2022 +0900

    Disable token metrics

commit d042108
Author: Anthony Tsang <[email protected]>
Date:   Fri Oct 7 10:44:10 2022 +0900

    Remove daily trading signal

commit a6524d3
Author: Anthony Tsang <[email protected]>
Date:   Tue Sep 27 14:23:49 2022 +0900

    Add links

commit f6c7836
Author: Anthony Tsang <[email protected]>
Date:   Tue Sep 27 14:12:54 2022 +0900

    Fix icon sizes in coin tables

commit a0eb9ef
Author: Anthony Tsang <[email protected]>
Date:   Mon Apr 11 11:30:48 2022 +0900

    Add links

commit 43aa148
Author: Anthony Tsang <[email protected]>
Date:   Fri Mar 18 13:12:03 2022 +0900

    Update bitcoin calculator page

commit 069d723
Author: Anthony Tsang <[email protected]>
Date:   Mon Feb 28 15:00:52 2022 +0900

    Remove rel attributes for external links in coin articles

commit 3acffe2
Author: Anthony Tsang <[email protected]>
Date:   Fri Oct 15 18:24:11 2021 +0900

    Update how to buy crypto article

commit 5258b9d
Author: Anthony Tsang <[email protected]>
Date:   Mon Sep 6 10:57:53 2021 +0900

    Remove special case for ETH for indicators

commit 7e13026
Author: Anthony Tsang <[email protected]>
Date:   Mon Sep 6 10:57:21 2021 +0900

    Fix id/cmc_id mismatch in update ticker service

commit 3c13580
Author: Anthony Tsang <[email protected]>
Date:   Fri Sep 3 17:00:10 2021 +0900

    Fix guard against division by 0 in market pair updates

commit db9df40
Author: Anthony Tsang <[email protected]>
Date:   Fri Sep 3 15:41:10 2021 +0900

    Guard against division by 0 in market pair updates

commit ca64b9a
Author: Anthony Tsang <[email protected]>
Date:   Fri Sep 3 14:18:27 2021 +0900

    Improve error checking/handling for git-related services

commit 60a2c36
Author: Anthony Tsang <[email protected]>
Date:   Wed Sep 1 10:07:20 2021 +0900

    Fix missing identifier in update ticker service

commit ad15015
Author: Anthony Tsang <[email protected]>
Date:   Tue Aug 31 16:03:57 2021 +0900

    Reduce coin query in update ticker service

commit 9126279
Author: Anthony Tsang <[email protected]>
Date:   Tue Aug 31 15:38:11 2021 +0900

    Fix connection timeout issue on sidekiq

commit dc5fc01
Author: Anthony Tsang <[email protected]>
Date:   Tue Aug 31 12:33:26 2021 +0900

    Update services to properly check out db connections from connection pool

commit e09fb9b
Author: Anthony Tsang <[email protected]>
Date:   Tue Aug 31 12:13:19 2021 +0900

    Fix errors with indicator changes

commit e7fb530
Author: anthotsang <[email protected]>
Date:   Tue Aug 31 11:30:52 2021 +0900

    Add new indicator coins (#945)

    * Fix centering on beaker for empty indicator page

    * Add coins and handle special case for SHIBxM

    * Point to replica url env vars, which are managed by Heroku, rather than directly at URLs that can change

    * Add EN translations of newly supported coins

    * Update indicators controllers to improve consistency between controllers

commit e25523b
Author: Anthony Tsang <[email protected]>
Date:   Mon Aug 23 09:52:19 2021 +0900

    Update react_on_rails minor version

commit a1adf1b
Author: Anthony Tsang <[email protected]>
Date:   Mon Aug 23 09:22:38 2021 +0900

    Add rails autoscale gem

commit 317d27a
Author: Anthony Tsang <[email protected]>
Date:   Mon Aug 16 17:31:00 2021 +0900

    Update bundler version and add linux platform for heroku compatibility

commit c526e4b
Author: Anthony Tsang <[email protected]>
Date:   Mon Aug 16 17:21:47 2021 +0900

    Improve coin articles index

commit a08d5ff
Author: Anthony Tsang <[email protected]>
Date:   Mon Aug 16 17:01:41 2021 +0900

    Update mini_race and ensure proper context after forking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.