How to gzip response in flask
Performance tuning your web server is super important. You can gain a significant speed boost while reducing server load by using caching and other techniques. From a client perspective, reducing network transmission and bandwidth is also important, that’s where gzip compression comes in.
What Exactly is gzip?
Gzip is a data compression algorithm capable of compressing and decompressing files quickly. The name also refers to two other technologies: the software used to compress and decompress files, and the format those files are stored in. Gzip can compress almost any file type, from plain text to images, and is fast enough to compress and decompress data on-the-fly.
Why it is important?
The main reason it is important is because it reduces the time it takes for a web server to transfer the page files, images, style sheets or json response which ultimately reduces the load time of your website.
When to Gzip response?
Before start gziping your server responses, it’s important to understand when you should and shouldn’t gzip the response to the client.
Each time a resource is compressed on the server, the CPU must perform the work to apply the compression. Granted, it’s not a ton of work, but it can quickly add up if you’re hosting a busy website or many sites. If you can avoid pointless compression, you can make better use of your server hardware.
There are two primary cases you want to avoid gziping your response:
1) Image and PDF compression
You should not allow your web server to compress image files or PDF files. The reason being that these files are already compressed and by compressing them again you’re not only wasting CPU resources but you can actually make the resulting file larger by compressing them again.
2) Small files
This one is a little less obvious and there are two components. For starters, if you’re compressing files that are smaller than the MTU size of a TCP packet, you’re wasting your time. 1500 bytes is the MTU size for the internet since that is the largest size allowed at the network layer. If you take a file that is 1300 bytes and compress it to 800 bytes, it’s still transmitted in that same 1500 byte packet regardless, so you’ve gained nothing.
Code Snippet
import gzip, functools from io import BytesIO as IO from flask import after_this_request, request def gzipped(f): @functools.wraps(f) def view_func(*args, **kwargs): @after_this_request def zipper(response): accept_encoding = request.headers.get('Accept-Encoding', '') if 'gzip' not in accept_encoding.lower(): return response response.direct_passthrough = False if (response.status_code < 200 or response.status_code >= 300 or 'Content-Encoding' in response.headers): return response gzip_buffer = IO() gzip_file = gzip.GzipFile(mode='wb', fileobj=gzip_buffer) gzip_file.write(response.data) gzip_file.close() response.data = gzip_buffer.getvalue() response.headers['Content-Encoding'] = 'gzip' response.headers['Vary'] = 'Accept-Encoding' response.headers['Content-Length'] = len(response.data) return response return f(*args, **kwargs) return view_func @gzipped def get_data(): return response
Summary
To sum up, gziping your server response can make a huge difference in the server and website performance; however, it’s important to pay attention to your files type and size before applying such compression technique