Skip to content

Spring is slow with LaunchDarkly enabled #105

Closed

Description

@jeromedalbert

Config

We are using the 5.0.1 version of this gem.

Here is our LaunchDarkly Spring config, as per the docs:

# config/spring.rb

Spring.after_fork do
  config = LaunchDarkly::Config.new(logger: Logger.new('/dev/null'))
  Rails.configuration.ld_client = LaunchDarkly::LDClient.new(ENV['LD_KEY'], config)
end

Problem

When spring is already loaded, Spring takes 1.5 seconds to boot:

$ time (rails c)
Loading development environment (Rails 5.1.6)
[1] pry(main)>
( rails c; )  0.24s user 0.10s system 22% cpu 1.521 total

1.5 seconds is too long, booting rails should feel instant since spring is already loaded.

Notes

  • Removing the LD config from Spring makes Rails boot back to 0.5 seconds, which is ideal. But then the LD client will fail to receive updates.
  • Is there any way to make the LD client work with Spring while not impacting Spring boot time? Because at the moment this makes all our Spring commands (console, rspec, rake tasks...) slower by a good 1 second.

Activity

  1. changed the title [-]Spring is slow[/-] [+]Spring is slow with LD enabled[/+] on Jul 27, 2018
  2. changed the title [-]Spring is slow with LD enabled[/-] [+]Spring is slow with LaunchDarkly enabled[/+] on Jul 27, 2018
  3. eli-darkly commented on Jul 27, 2018

    @eli-darkly
    Contributor

    If I understand correctly, it sounds like what you want is a client that doesn't actually get initialized until you need to use it. This is somewhat problematic because initialization of the client is not thread-safe, and if you wait until some later point in the application to create the client on demand, you could end up with several threads each trying to call LdClient.new.

    Unfortunately, we haven't used Spring with Ruby, so I'm not sure what other options there may be to control the boot process. I can only hope that there is some way to detect if your application is really being started, or if it is just initializing Spring because you're running some other Spring command, and only call LdClient.new in the former case.

  4. jeromedalbert commented on Aug 8, 2018

    @jeromedalbert
    Author

    It sounds like what you want is a client that doesn't actually get initialized until you need to use it

    We ended up doing just that. This is approximately what we did:

    # config/initializers/launchdarkly.rb
    
    $ld_client = nil
    # lib/ld_client.rb
    
    class LdClient
      def self.client
        if !$ld_client
          config = ...
          $ld_client = LaunchDarkly::LDClient.new(ENV['LD_KEY'], config)
        end
        $ld_client
      end
    end
    # config/spring.rb
    
    # No LD config at all

    Then we make our code call LdClient.client whenever LaunchDarkly needs to be called. This means that Spring is not slowed down at startup, and the client will be lazily initialized for each of our web server processes. This may not be thread-safe, but we use the Unicorn web server which uses forked processes, not threads. So this works for us for now.

  5. added a commit that references this issue on Aug 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions