What is a health check endpoint in a Flask application?
A health check endpoint is a simple URL route that returns a status indicating if the Flask app is running properly. It helps monitoring tools know if the app is healthy.
Click to reveal answer
beginner
How do you create a basic health check endpoint in Flask?
You define a route like @app.route('/health') and return a simple response like {'status': 'ok'} with HTTP status 200.
Click to reveal answer
intermediate
Why should health check endpoints be lightweight and fast?
Because they are called frequently by monitoring systems to quickly verify app status without adding load or delays to the app.
Click to reveal answer
beginner
What HTTP status code is commonly returned by a healthy Flask health check endpoint?
HTTP status code 200 means the app is healthy and responding correctly.
Click to reveal answer
intermediate
How can you extend a health check endpoint to check database connectivity in Flask?
Inside the health check route, add a quick query or ping to the database. If it succeeds, return healthy; if it fails, return an error status.
Click to reveal answer
What is the main purpose of a health check endpoint in Flask?
ATo verify the app is running and responsive
BTo serve the main homepage
CTo handle user login
DTo store user data
✗ Incorrect
Health check endpoints are designed to confirm the app is up and running.
Which HTTP status code usually indicates a healthy Flask app in a health check?
A404
B500
C200
D302
✗ Incorrect
Status code 200 means OK, indicating the app is healthy.
What should a health check endpoint avoid doing?
APerforming heavy computations
BResponding quickly
CReturning a simple status
DBeing accessible via a URL
✗ Incorrect
Health checks should be lightweight and fast, avoiding heavy tasks.
How can you test database connectivity in a Flask health check?
ABy checking user sessions
BBy restarting the database
CBy ignoring the database
DBy running a quick database query
✗ Incorrect
A quick query or ping confirms the database is reachable.
Where do you define a health check endpoint in Flask?
AIn the database schema
BIn a route decorated with @app.route
CIn the HTML templates
DIn the CSS files
✗ Incorrect
Flask endpoints are defined with @app.route decorators.
Explain how to create a simple health check endpoint in Flask and why it is useful.
Think about a URL that tells if your app is alive.
You got /4 concepts.
Describe how you would enhance a Flask health check endpoint to verify database connectivity.
Check if the app can talk to its database quickly.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a health check endpoint in a Flask application?
easy
A. To connect to the database and run queries
B. To handle user login and authentication
C. To confirm the app is running and responsive
D. To serve static files like images and CSS
Solution
Step 1: Understand the role of health check endpoints
Health check endpoints are simple URLs that return a status message indicating the app is running properly.
Step 2: Compare options with this role
Only To confirm the app is running and responsive matches this purpose. Other options describe unrelated app functions.
Final Answer:
To confirm the app is running and responsive -> Option C
Quick Check:
Health check = Confirm app running [OK]
Hint: Health check endpoints confirm app health quickly [OK]
Common Mistakes:
Confusing health check with user authentication
Thinking health check serves static files
Assuming health check runs database queries
2. Which of the following is the correct way to define a simple health check endpoint in Flask?
easy
A. app.route('/health')
def health():
return {'status': 'ok'}
B. @app.route('/health')
health = {'status': 'ok'}
C. app.route('/health')
health = lambda: {'status': 'ok'}
D. @app.route('/health')
def health():
return {'status': 'ok'}
Solution
Step 1: Recall Flask route decorator syntax
Flask uses @app.route('/path') decorator above a function to define routes.
Step 2: Check each option for correct syntax
@app.route('/health')
def health():
return {'status': 'ok'} correctly uses @app.route decorator and defines a function returning a dictionary. Others miss the decorator or define incorrectly.
Final Answer:
@app.route('/health')\ndef health():\n return {'status': 'ok'} -> Option D
Quick Check:
Use @app.route decorator for routes [OK]
Hint: Use @ before app.route to define Flask routes [OK]
Common Mistakes:
Forgetting the @ symbol before app.route
Returning a dictionary without a function
Assigning route to a variable instead of a function
3. Given this Flask code, what will be the JSON response when accessing /health?
B. It will cause a syntax error because return type is wrong
C. It returns a Python dict but Flask needs a Response or JSON
D. The function name 'health' is reserved and cannot be used
Solution
Step 1: Recall Flask route decorator syntax
Flask uses the @app.route('/path') decorator above the function definition. The @ symbol is required to apply the decorator.
Step 2: Analyze the provided code
The code has app.route('/health') without the @, so it calls the route method but does not decorate the health function. Thus, the /health endpoint will not be registered and accessing it returns 404.
Final Answer:
The route decorator is missing the @ symbol -> Option A
Quick Check:
Missing @ means no route registration [OK]
Hint: Use @ before app.route to define Flask routes [OK]
Common Mistakes:
Returning plain dict without jsonify
Forgetting @ before route decorator
Thinking function names are reserved keywords
5. You want to create a health check endpoint in Flask that returns status 'ok' only if the database connection is alive, otherwise 'error'. Which approach is best?
hard
A. Return {'status': 'ok'} always, ignoring database state
B. Check database connection inside the route, return JSON accordingly
C. Use a separate thread to update status and return cached result
D. Raise an exception if database is down and let Flask handle it
Solution
Step 1: Understand health check purpose with dependencies
A health check should verify critical dependencies like database connectivity to report accurate status.
Step 2: Evaluate options for checking database state
Check database connection inside the route, return JSON accordingly directly checks the database connection inside the route and returns JSON status accordingly, which is clear and reliable.
Step 3: Consider other options
Return {'status': 'ok'} always, ignoring database state ignores database state, C adds unnecessary complexity, D relies on exceptions which is less clear for health checks.
Final Answer:
Check database connection inside the route, return JSON accordingly -> Option B
Quick Check:
Health check should verify dependencies live [OK]
Hint: Check dependencies live inside health endpoint for accuracy [OK]
Common Mistakes:
Ignoring database state in health check
Overcomplicating with threads or caching
Relying on exceptions instead of status checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.