Bird
0
0

How should you implement a Flask health check endpoint that returns JSON with keys 'status' and 'cache' indicating 'ok' or 'fail' based on a cache service availability check?

hard📝 Conceptual Q8 of 15
Flask - Deployment
How should you implement a Flask health check endpoint that returns JSON with keys 'status' and 'cache' indicating 'ok' or 'fail' based on a cache service availability check?
AUse Flask's abort(500) if cache is down, else return 'OK' text
BReturn a static JSON {'status': 'ok', 'cache': 'ok'} without checking cache
CCheck cache connection, then return jsonify(status='ok' if cache_ok else 'fail', cache='ok' if cache_ok else 'fail') with appropriate HTTP status
DReturn a plain string 'status: ok, cache: ok' regardless of cache state
Step-by-Step Solution
Solution:
  1. Step 1: Perform cache availability check

    Implement logic to verify if cache service is reachable and operational.
  2. Step 2: Return JSON response

    Use jsonify with keys 'status' and 'cache' set to 'ok' or 'fail' based on the check.
  3. Step 3: Set HTTP status code accordingly

    Return HTTP 200 if all services are ok, or appropriate error code if not.
  4. Final Answer:

    Check cache connection, then return jsonify(status='ok' if cache_ok else 'fail', cache='ok' if cache_ok else 'fail') with appropriate HTTP status -> Option C
  5. Quick Check:

    Verify dynamic JSON response based on service status [OK]
Quick Trick: Dynamic JSON with service checks ensures accurate health status [OK]
Common Mistakes:
MISTAKES
  • Returning static responses without checks
  • Using plain strings instead of JSON
  • Ignoring HTTP status codes for failures

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes