heroku-buildpack-ruby: Bootsnap cache incorrect or not being used?
Linked internal tickets
- 952389
Issue
The bootsnap cache is present after a deploy:
$ heroku run bash
~ $ du -hs tmp/cache
64M tmp/cache
~ $ ls tmp/cache
bootsnap-compile-cache bootsnap-load-path-cache
However if a rails process runs, the cache size doubles:
~ $ rails runner puts 'done' >> /dev/null
~ $ du -hs tmp/cache
125M tmp/cache
And running the same command twice makes it faster, this makes me think that bootstrap isn’t being used:
$ heroku run bash
Running bash on ⬢ issuetriage... up, run.2775 (Standard-1X)
~ $ time rails runner puts "done"
# ...
real 0m7.197s
user 0m5.760s
sys 0m1.048s
~ $ time rails runner puts "done"
# ...
real 0m2.953s
user 0m2.384s
sys 0m0.492s
We need to investigate why. My first guess would be that the difference between build and runtime file structure prevents the original cache from being used. But that’s just a guess.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 25 (8 by maintainers)
Commits related to this issue
- Canonicalize $BUNDLE_PATH in cache keys Ref: https://github.com/heroku/heroku-buildpack-ruby/issues/979 Heroku buildpacks build the application in one location and then move it elsewhere. This cause... — committed to Shopify/bootsnap by byroot 3 years ago
- Canonicalize $BUNDLE_PATH in cache keys Ref: https://github.com/heroku/heroku-buildpack-ruby/issues/979 Heroku buildpacks build the application in one location and then move it elsewhere. This cause... — committed to Shopify/bootsnap by byroot 3 years ago
@sergiopantoja @geoffharcourt Thank you for providing those timings - glad to hear it helped! It will be a bit longer before we can make this new behaviour the default, but that flag is fine to use in the meantime 😃
I’ve just added support for a beta opt-in labs
build-in-app-dir, that makes the Heroku build system build in/app(the same location that the app will be run at runtime) rather than the previous location of/tmp/build_<hash>.This can be enabled via:
I’d be very interested to hear how much of a difference this makes to app boot times at runtime if anyone tries it out 😃
In our Rails app we reduced boot time from ~16 seconds to ~8 seconds after enabling
build-in-app-dir.Are there any caveats to be aware of?
Any progress to report regarding making this the default?
Theres’ a recent article about this as well that I want to link https://dev.to/dbackeus/cut-your-rails-boot-times-on-heroku-in-half-with-a-single-command-514d
Yes, though unfortunately since
postdeployruns after the initial build, enablingbuild-in-app-dirat that point will be too late (unless the script also triggers a rebuild, but that will require a bunch of manual wiring up, and also mean even slower provision times for review apps). At that point for many people I would imagine the slight loss of review-app to prod parity would be preferable to the workaround, particularly if they have a staging environment that can catch any rarebuild-in-app-dirrelated issues not seen in the review app. (Ultimately a review app is never going to be identical to production; datastore differences, load differences, env var differences etc mean that it’s only ever going to be mostly-similar at best.)Just want to chime in that we are also seeing great results on our production app after enabling
build-in-app-dir.Before:
After:
One problem we ran into and was able to address after identifying the issue is our app’s use of a third-party buildpack which cleans the slug after build (https://elements.heroku.com/buildpacks/devforce/heroku-buildpack-cleanup).
We had
tmp/cachelisted in.slugcleanup, which removed the folder before slug compression. Thus, the app was still experiencing “cold” boots. Removing thetmp/cachefrom the clean up config addressed the cold boot issue due to thetmp/cachefolder now persisting in the compiled slug.— Edit: One other thing to mention. The changes we made did increase the slug size by 10M (8% for our particular app). An increase is an expected result. Just wanted to share this detail.
After enable
build-in-app-dirfor Production, the boot up time went from 9.968s to 6.367s, which is (9.968 - 6.367) / 9.968 = 36.125…% fasterBefore
After
I just tried
build-in-app-dir. Sadly, with no other changes, my build is hanging onrake assets:precompile. I filed an issue with support so it can be looked at.@edmorley My boot time was cut in half! No issues so far. Thank you!
Before:
After enabling the labs feature and redeploying: