How to make Spring server to start even if database is down?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
By default, many Spring Boot applications fail startup when the database is unavailable. That behavior is reasonable for strict systems, but some architectures need the process to start and report not ready while dependencies recover. The safe pattern is startup tolerance plus explicit readiness gating and strong observability.
Configure DataSource for Non Fail Fast Startup
With HikariCP, set initialization behavior so startup does not fail immediately on first connection attempt.
initializationFailTimeout: 0 allows app boot even if the first connection attempt fails.
Keep connection timeout bounded so blocked calls do not hang worker threads.
Separate Startup from Readiness
Starting process and serving traffic are different concerns. Use health probes so orchestrators hold traffic until dependencies are available.
Readiness endpoint check:
Liveness can remain up while readiness stays down during database outage.
Avoid Early Database Access During Boot
Even with tolerant datasource settings, startup can fail if code touches repositories in boot hooks such as @PostConstruct, ApplicationRunner, or eager cache loaders.
Move database work to request time or scheduled background workers with retry logic.
This keeps startup lightweight while still exposing dependency state.
Handle Flyway or Liquibase Behavior
Migration tools can force startup failure when DB is down. Decide policy explicitly:
- strict mode for production critical consistency.
- tolerant mode where migrations run separately.
Example tolerant profile:
If migrations are disabled at startup, ensure a separate controlled migration pipeline exists before enabling traffic.
Use Profiles for Environment Specific Strategy
Different environments may need different startup rules.
Run with profile:
This keeps strict and tolerant behavior explicit and avoids accidental drift.
Add Retry and Alerting
Startup tolerance should not hide prolonged outages. Add operational controls:
- alert when readiness remains down beyond threshold.
- monitor pool exhaustion and connection failure rates.
- expose dependency health in dashboards.
Application startup success should not be treated as dependency success.
Controller and Job Design Considerations
When database may be unavailable during startup, controllers and scheduled jobs should fail gracefully rather than throwing unhandled exceptions. Return clear temporary dependency error responses and backoff in background tasks.
Practical guidance:
- Wrap repository calls in resilience patterns where appropriate.
- Keep retry intervals bounded.
- Emit structured logs with dependency state context.
This keeps runtime behavior predictable while infrastructure recovers. It also gives operations teams clearer evidence when deciding whether to scale, restart, or fail over components.
Common Pitfalls
- Allowing startup without readiness gating and serving immediate failing requests.
- Leaving repository calls in startup hooks that still force DB dependency.
- Disabling migration tools without a replacement schema workflow.
- Confusing liveness success with full application readiness.
- Adding long synchronous retries in request threads and exhausting pools.
Summary
- Configure datasource startup to avoid fail fast when outage tolerance is required.
- Separate process startup from readiness driven traffic acceptance.
- Remove early repository calls from boot lifecycle hooks.
- Define clear migration strategy for tolerant startup modes.
- Add alerting so tolerated startup does not mask persistent dependency failure.
Related reading
- How to manually create a mdf file for localdb to use?
- How to map a composite key with JPA and Hibernate?
- How to model dimension tables in TiDB?
- How to modify `sql_mode` using the configuration file in TiDB?
- how to make StdIn.isEmpty return true?
- How to manage exceptions thrown in filters in Spring?
- How to modify tidb default GLOBAL variables like tidb_replica_read value in TiDB
- How to mount a postgresql volume using Aws EBS in Kubernete

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.