Disable Redundant Gunicorn Access Logs on Heroku

When hosting an application on Heroku, managing logs efficiently is crucial for maintaining system health and keeping costs down. Heroku provides built-in logging for all incoming requests, but by default, Gunicorn, the Python HTTP server often used in Heroku deployments, also logs incoming requests. This duplication can clutter your logs, making them harder to parse and more expensive to store. Let’s explore why this redundancy exists and how to fix it. Heroku Router Logs Heroku’s Router automatically logs all incoming HTTP requests, providing a wealth of data for monitoring and debugging your application. These logs are always enabled and include detailed information, such as the HTTP method and URL path of the request, the response status code, the client’s IP address, and the request processing time (see Heroku Router Log Format). ...

November 25, 2024 · 2 min · Johnny Metz

5 Ways to Get the Latest Book Per Author in Django

In a Django application, fetching the latest record for each group is a common yet challenging task, especially when working with large datasets. Whether you’re building an analytics dashboard or managing grouped data, finding an efficient solution is key. In this blog post, we’ll explore five different approaches to tackle this problem, ranked from least to most effective based on performance and readability, using the following Book model: class Book(models.Model): title = models.CharField(max_length=255) author = models.ForeignKey( Author, on_delete=models.CASCADE, related_name="books" ) genre = models.CharField(max_length=255) published_at = models.DateTimeField() is_featured = models.BooleanField(default=False) Solution 1: Python Max with Prefetch latest_books = [ max(author.books.all(), key=lambda x: x.published_at, default=None) for author in Author.objects.prefetch_related("books") ] Performs heavy computation in Python rather than leveraging the database, which is bad for performance. Also makes two database queries (better solutions do it in one). ...

November 14, 2024 · 2 min · Johnny Metz

Supercharge Your Django App: 7 Sneaky Tricks to Crush Slow Database Queries

Optimizing Django query performance is critical for building performant web applications. Django provides many tools and methods for optimizing database queries in its Database access optimization documentation. In this blog post, we will explore a collection of additional and essential tips I’ve compiled over the years to help you pinpoint and resolve your inefficient Django queries. Kill Long-Running Queries with a Statement Timeout PostgreSQL supports a statement_timeout parameter that allows you to set a maximum time limit per query. This is useful for preventing long-running queries from tying up precious resources and slowing down your application. My team at PixieBrix experienced an incident where a few long-running queries resulted in a full database outage. Setting a statement timeout in your Django settings can help prevent this from happening. ...

August 13, 2023 · 6 min · Johnny Metz

Optimize Django Query Performance by combining Select Related and Prefetch Related

Introduction When building a Django application, one of the key challenges developers face is optimizing database query performance. Django provides two tools,select_related and prefetch_related, that reduce the number of database queries, and increase the performance of your application. This blog post explores the power of these two methods and how to combine them to maximize your application’s query performance. My team at PixieBrix implemented these techniques and have seen a significant improvement in query performance and customer satisfaction. ...

May 13, 2023 · 3 min · Johnny Metz

[Cross-Post] Django performance: Sort by a Custom Order

See the post here: Sort a Django Queryset by a Custom Order

January 2, 2023 · 1 min · Johnny Metz