Skip to content

Enhance health check endpoint #1115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

eugengi
Copy link

@eugengi eugengi commented May 4, 2025

Description

This PR introduces a patch to the current health_check endpoint to address potential performance and semantic issues.

Problem

  • Performance
    The health_check view executes a potentially expensive operation, specifically Petition.objects.count(). Under the hood, Django runs SELECT COUNT(*)..., which steps through each record one by one. For an endpoint that's hit frequently - depending on the number of rows in your table - this becomes computationally expensive and introduces unnecessary overhead.

  • Semantics
    Beyond performance, the semantics of the current code could be misleading. Given no assignment, it seems we don't need to do anything with the count. In addition, executing <model>.objects.count() returns an int. If there are no records in the DB for the Petition model, we simply return 0. This means we may never even raise based on the code we execute in the try block.

Yes, from a broader database connectivity perspective, this can be considered logically correct - the DB being down, disconnected, the table doesn't exist, etc., will raise an exception. However, the current code doesn't easily express or infer this intent.

Solution

  • Test Connectivity Only
    This update alternatively replaces the unnecessary overhead query and executes a pure database connection check. It's lightweight since we bypass the ORM and only interact with the database connection layer. It's expressive enough to communicate intent at first glance.

Disclaimer: If more is required from this health check, like ensuring we can execute a query, then this solution is insufficient. Prefer cursor.execute("SELECT 1") or <model>.objects.exists() as better yet, non-expensive alternatives in this respect.

Motivation

I enjoy giving back to open-source projects I either use or have learned from a thing or two.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Bug fix (non-breaking change which fixes an issue)
  • Chore (non-breaking change which does not add visible functionality but improves code quality)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation

eugengi added 2 commits May 4, 2025 17:56
Remove `version` property ignored in the latest compose spec.

[Note]
- Compose no longer uses `version` for backward compatibility
  instead it uses a new strategy to check file format.

[Docs]
See relevant docs:
- https://docs.docker.com/reference/compose-file/version-and-name/#version-top-level-element-obsolete
Replace expensive operation with lightweight conn test.

[Docs]
See Django docs on `count()`:
- https://docs.djangoproject.com/en/5.2/ref/models/querysets/#django.db.models.query.QuerySet.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant