Skip to content

Ensure Backend Starts Only If Database is Connected #127

@Parth06102006

Description

@Parth06102006

Currently:

The backend server calls app.listen(...) independently.

Database connection logic is handled separately in a controller/service.

This means the API starts even if the database is down, leading to runtime errors when endpoints are called.

Proposed Change:

Modify the startup flow so that the backend only starts (app.listen) after a successful database connection.

If the database fails to connect, the server should not run, and an error should be logged.

Advantages:

✅ Fail Fast Principle – prevents the server from running in a broken state.

✅ Better Reliability – avoids confusing runtime errors caused by missing DB connections.

✅ Cleaner Error Handling – startup logs clearly show database connection issues.

✅ Consistency – ensures that all APIs depending on the DB will work once the server is running.

Why This Should Be Added:
Right now, the app can misleadingly appear “healthy” (server listening), even if the database is unavailable. This could cause problems in production monitoring, CI/CD pipelines, or health checks. By enforcing database connectivity at startup, we guarantee the app only runs when fully functional.

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