Serhii Soldatov’s Post

Found a simple but powerful optimization for large API responses in Django! While profiling some of our heavier endpoints, I came across the django-http-compression library — and the results were impressive: Here are a couple of examples (Brotli compression): --- /api/media_orders/ → 3679 media orders → Response size: 60.94 KB → 6.37 KB with compression (~10× smaller) --- /api/report_booked_media/ → Response size: 16.71 KB → 1.57 KB with compression --- /api/contracts/ → Response size: 237.23 KB → 11.46 KB with compression --- That’s a huge gain with minimal setup, plug it in and configure your middleware. Big thanks to Adam Johnson, who created and maintains this library 👏

  • No alternative text description for this image

But if Django is used with Nginx, this middleware package makes no sense then. ❌ Avoid Double Compression and Wasted Resources Double Compression: If Django compresses the response and Nginx is also configured to compress the response, the system will usually end up with double compression or a performance hit. ✅ Best Practice: Let Nginx Handle It The standard and recommended architecture is: Remove any HTTP compression middleware (like GZipMiddleware) from your Django project's MIDDLEWARE setting. Ensure Nginx is properly configured to enable and handle compression (using gzip on; or Brotli settings, along with appropriate gzip_types and gzip_proxied directives).

Looks cool for a plug and play module, how about time improvements? Using gzip in FastAPI solved my lambdas size restriction issues but speed wise it didn’t help much. I guess compression/decompression is not cheap either

Glad it helped! It's such a cheap optimization, and it's a shame most PaaSes and web servers don't do it by default. Hopefully we can make it a default in Django.

what is the difference between built-in GZipMiddleware in django and your django-http-comparesion?

Like
Reply

it makes more sense to use brotli on the nginx where you know it’s done in C, rather than plugging a Python lib based middleware

Like
Reply

Bro congrats for success in compressing and quick question that is bugging me, lately: Fast API or Django REST and why exactly?

Like
Reply

Just adding 2-3 lines in django does some mind blowing optimization. How wonderful

Like
Reply

You guys use django, so backward.... 🤣 Just kidding, great job 💯👍 keep growing

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories