Skip to content

Parallel bean initialization during startup [SPR-8767] #13410

Closed

Description

@spring-projects-issues

Tomasz Nurkiewicz opened SPR-8767 and commented

Spring should provide a way (possibly a BeanFactory with a different ConfigurableListableBeanFactory#preInstantiateSingletons implementation) to initialize singleton non-lazy beans on startup in parallel using a thread pool. This could significantly reduce startup (and maybe shutdown) time by creating and initializing independent beans concurrently.

The algorithm is pretty simple in principle. Whereas the normal bean factory creates beans in single thread in rather random order, this implementation should:

  1. Find all bean definitions that don't have any unresolved dependencies.
  2. Schedule creation of each bean found in 1. in a separate concurrent task to allow parallel creation
  3. When any of the tasks scheduled in 2. is completed go to 1.

The algorithm stops when all beans are created.

Implementation notes:

  • circular dependencies might require extra care
  • bean factory should create a thread pool with configurable number of threads and shut it down after all beans are created

Affects: 3.1 RC1

Reference URL: http://forum.springsource.org/showthread.php?105896-Initialize-spring-beans-in-parallel-at-startup

Issue Links:

80 votes, 77 watchers

Activity

  1. spring-projects-issues commented on Oct 17, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Hi Tomasz,

    Reading through the linked forum thread, Marten's suggestion hits the nail on the head

    The problem here, imho, is that you are mixing bean construction and bean initialization.

    The latter is something you (judging from your post) want to do in parallel. The easiest approach, I guess, is to create an ApplicationListener which listens to ContextRefreshedEvents. (This is fired when the context is up) and which starts initializing the caches, you could plugin a TaskExecutor for this which utilizes the servers threadpool, which in turn should utilize the underlying hardware to its fullest...

    Best of both worlds imho...

    (http://forum.springsource.org/showthread.php?105896-Initialize-spring-beans-in-parallel-at-startup&p=352702#post352702)

    While it is conceptually straightforward to imagine the container introducing concurrent bean initialization, in practice it would be anything but. We would need to see quite a bit of feedback and demand that Spring container initialization is fundamentally too slow to seriously consider this kind of change. Again, given the specific scenario in the forum post, it appears that the user can make changes that would both solve the startup time problem and probably improve the design of his program in the process.

    Feel free to comment further if you think there is a compelling case that's been missed here, and we can leave this open for further comments and votes to that effect. Otherwise I'll close as won't fix for now.

  2. spring-projects-issues commented on Oct 25, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Niklas Schlimm commented

    Hi Chris,
    we're currently working on a migration of over 50 web applications to SpringIOC. Startup time is a big issue 'cause we need quick startup during development and (more important) in production environment. Since Spring does not support concurrent singleton bean instantiation we have a startup time of out complete production environment of ~2h 30 Minutes. which is not acceptable to our operations teams. We just ,igrated from a proprietary container solution that we developed. That proprietary container supported parallel instantiation. Now, we have to argue why we migrated to Spring ... to us, this is not a minor issue and we would highly appreciate any progress on this.

    With regards to the solution it should be something like a "managed task executor" 'cause ideally the concurrent threads have the container managed thread context (JNDI, JPA resources must be accessible etc.). Therefore the solution is not so straight formward imho.

    Best regards,
    Niklas

  3. spring-projects-issues commented on Nov 23, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Niklas,

    2h 30m is a very long time indeed, but again, I would encourage a more pragmatic approach.

    For the majority of Spring applications, container startup time is not an issue. We can be fairly certain of this simply because it is not a complaint we often encounter here in JIRA, in our forums, at conferences or with paying customers. When it does come up as a concern, we usually advise profiling the application to determine exactly which beans are causing the slowdown, and taking specific action to reduce the impact. Marten's suggestion that I quoted above would be perfectly adequate in many cases.

    The upside of parallelizing bean initialization in the Spring container could be significant for a minority of applications using Spring, while the downsides - the inevitable bugs, added complexity and unintended side effects - would affect potentially every application using Spring. Not an attractive outlook, I'm afraid.

    I'm resolving this issue as Won't Fix because indeed it is very unlikely that we would introduce a change of this magnitude into the core framework at this point without a very strong rationale.

    Users are free to reopen this issue and add new comments, and continue to add votes if there are arguments that have not yet been heard.

  4. spring-projects-issues commented on Nov 23, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Niklas Schlimm commented

    Hi Chris,

    understand your view point. Thanks for the comprehensive reply.

    Cheers,
    Niklas

  5. spring-projects-issues commented on Sep 8, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Adib Saikali commented

    I think parallel startup of Spring is very important, in my application spring is taking up 50% of my startup time, reducing that would be very helpful during development a major time saver.

  6. spring-projects-issues commented on Oct 23, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Ragnar Rova commented

    Please see https://github.com/gredler/spriths for an experimental example implementation. Seems like the spriths implementation goes far into making it work. The author of spriths mentions #10033 that needs to be solved in order to make parallel startup thread-safe.

    It would be nice if the framework at least did not stand in the way of users wanting parallel startup.

  7. spring-projects-issues commented on Jun 19, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Jonatan Jönsson commented

    I think a ConfigurableListableBeanFactory#setConcurrentInstantiationOfSingletons(boolean) would be great. Default to non-concurrent but make it super easy to get a parallel version.

  8. spring-projects-issues commented on Sep 19, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Scott Murphy commented

    For a single instance web application, the speed of Spring initialization is fine. However, when you have an application that uses 20+ instances, the slowness of Spring initialization begins to a have a detrimental impact on the ability to scale dynamically. We are currently feeling major pain using Spring on App Engine. If Spring supported a multi-threaded start up, we would see a significant improvement in our ability to scale as well benefit from enormous cost savings.

  9. spring-projects-issues commented on Sep 19, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Scott Murphy commented

    Is this worth revisiting now that it is 2 years later?

  10. spring-projects-issues commented on Sep 19, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Scott Murphy commented

    "Users are free to reopen this issue"

    Maybe keep this issue open so it can be voted upon?

  11. spring-projects-issues commented on May 7, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Adib Saikali commented

    I think this problem has two distinct parts parallel discovery of beans and parallel initialization of beans. Both of which can be implemented separately to improve performance unless component scanning for a large application can not be made any faster.

  12. spring-projects-issues commented on May 9, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Julien Dubois commented

    Start-up performance is a very important issue to me, and I think a lot of beans could be initialized in parallel. So yes this issue should stay open, it should even have a higher criticity level as far as I'm concerned.

  13. spring-projects-issues commented on Jun 11, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Attila Király commented

    Our applications have really big spring contexts with a lof ot spring beans. Our cache initializations are already running in parallel but the applications still take a long time to start up because simply some of our beans require time (for example because loading reference data from various sources, which we need for other bean creation already) in creation/initialization before we can say they are "done".

    So we would definitely benefit from a build in bean init parallelization.

  14. spring-projects-issues commented on Jul 29, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Deryl Spielman commented

    We are struggling with this as well. Lazy loading beans helped a bit but I believe @Controller beans end up lazy loading its autowired fields anyway which doesn't improve the speed that much. Also entity manager and other data source beans in the config must be carefully set to eagerly be loaded, which is cumbersome. With all of this talk of microservices and Spring Boot it is obvious that separating out the modules in to separate projects may improve the speed, but this increases the overhead of managing independently deployed and managed services + it is a project in its own to figure out how to refactor the Spring bean autowiring dependencies.

    I also attempted to define all beans in XML dynamically at startup using a custom implemented component scanning method found in some of these links. I found that it did improve the speed but not dramatically. The maintenance of ensuring the bean was dynamically created correctly was also cumbersome and did not work completely based on the complexities in having @Scope and @Qualifier.

    Summary is we need monolith applications to increase their speed time and this ticket should be evaluated! Thanks.

  15. spring-projects-issues commented on Sep 18, 2015

    @spring-projects-issues
    CollaboratorAuthor

    caiwei commented

    I'm thinking to enhance below project: https://github.com/gredler/spriths
    The basic idea of this project is Analyze bean dependencies -> Form DAG -> Parallel Bean Initialize.

    To analyze the dependencies, I should handle dependencies that are set via Setter, Constructor and annotations including:
    @Resource TYPE, FIELD, METHOD
    @Inject METHOD, CONSTRUCTOR, FIELD
    @AutoWired METHOD, CONSTRUCTOR, FIELD
    For dependencies introduced by ApplicationContextAware, Lookup method injection, Class.forName, Reflect..., may unable to handle...

    User is allowed to specify the parallel bean init in a context.xml as below:
    <context:parallel-bean-init enabled="true" alwaysGenerate="true" dag="dag.xml"/>

    a DAG is quite straightforward.
    <dag>
    <bean name="A">
    <dependOn bean="C"/>
    </bean>
    <bean name="B">
    <dependOn bean="C"/>
    <dependOn bean="D"/>
    </bean>
    <bean name="C">
    <dependOn bean="E"/>
    </bean>
    <bean name="D">
    <dependOn bean="E"/>
    </bean>
    <bean name="E">
    <dependOn bean="G"/>
    </bean>
    <bean name="F">
    <dependOn bean="G"/>
    </bean>
    <bean name="G"/>
    </dag>

    User can always let Spring generate a dag file or specify a dag file as they wish(set alwaysGenerate="false").
    We know It is almost impossible for users to create the DAG from scratch. They have to know the bean dependencies and find out all beans defined via annotation.
    The typical use case is user will first generate the DAG, then they may adjust the sequences in DAG if there are any server startup failures caused by the concurrent bean initialization.

    Just my 2 cents. Please share your expertise and let me know your concerns.

  16. 30 remaining items

  17. dikaewiwurae7 commented on Nov 18, 2022

    @dikaewiwurae7
  18. singasong1995 commented on Dec 13, 2022

    @singasong1995

    Marked.

  19. biuabiu commented on Jun 30, 2023

    @biuabiu

    I hope the spring team can take this issue seriously. My current project has an average startup time of over 5 minutes

  20. bclozel commented on Jun 30, 2023

    @bclozel
    Member

    @biuabiu it's very unlikely that this feature would help in your case. A single component is probably responsible for most of the startup time.

    Can you try measuring startup time with the tools we provide? See https://docs.spring.io/spring-framework/reference/core/beans/context-introduction.html#context-functionality-startup if your app is plain Spring or https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.spring-application.startup-tracking if this is a Spring Boot app. You can send the data our way if you need help analyzing it.

  21. biuabiu commented on Jul 13, 2023

    @biuabiu

    @biuabiu it's very unlikely that this feature would help in your case. A single component is probably responsible for most of the startup time.

    Can you try measuring startup time with the tools we provide? See https://docs.spring.io/spring-framework/reference/core/beans/context-introduction.html#context-functionality-startup if your app is plain Spring or https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.spring-application.startup-tracking if this is a Spring Boot app. You can send the data our way if you need help analyzing it.

    ths for reply,in project,some bean initialization tasks,but there are dependencies some bean

  22. nicolasmafraintelipost commented on Aug 24, 2023

    @nicolasmafraintelipost

    Could the bean initialization approach be delegated to a library? In that case, most of the Spring application would continue to use the same initialization approach. Those who want parallel bean initialization can include library to override behavior.

  23. modified the milestones: 6.x Backlog, 6.2.0-M1 on Feb 19, 2024
  24. jhoeller commented on Feb 27, 2024

    @jhoeller
    Contributor

    Following up on the locking revision in #23501, we are introducing a backgroundInit flag on AbstractBeanDefinition and a corresponding @Bean(bootstrap=BACKGROUND) enum. This is still a bit hot from the oven, a first cut to be committed soon.

    This new setting allows for singling out specific beans for background initialization, covering the entire getBean step for each such bean in preInstantiateSingletons. The corresponding Future is stored internally so that dependent beans automatically wait for the bean instance to be completed. Also, all regular background initializations are forced to complete at the end of preInstantiateSingletons; only for beans additionally marked as @Lazy, the completion is allowed to happen later (up until first actual access).

    Note that this typically goes together with @Lazy (or ObjectProvider<...>) injection points, otherwise the main bootstrap thread is going to block when a background-initialized bean needs to be injected. Also, background initialization applies to individual beans: If such a bean depends on other beans, they need to have been initialized already, either simply through being declared earlier or through @DependsOn which is going to enforce initialization in the main bootstrap thread before background initialization for the affected bean is triggered.

    Last but not least, a bootstrapExecutor needs to be specified on DefaultListableBeanFactory for this to be actually active. In an ApplicationContext, a bean of that name and of type Executor will be automatically detected. I suppose that Spring Boot will set a corresponding alias for its application task executor by default, but there is also room for a custom executor with a maximum concurrency setting for bootstrapping to be configured - independent from a larger-scale thread pool for other purposes in the same application.

  25. added a commit that references this issue on Feb 28, 2024
    17b2087
  26. added a commit that references this issue on Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

has: votes-jirain: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions