I'm facing slow Time to First Byte (TTFB) for my web app in Flask, after changing my linux server. The problem seams to show up only on the first time access or after cache expires (5 minutes). Sometimes TTFB reaches 16 seconds.
I confirmed the problem through:
- Gunicorn logs (running in debug mode with all logs specified)
- Nginx logs (using custom logging
upstream_response_time
andrequest_time
) - Running Flask in development mode.
I verified the problem doesn't exist (TTFB of some ms) on the same server running for example:
- Python single line web server
'Hello world'
Flask app
Looking at htop when the server receives a request I identified sometimes gunicorn workers entering in Uninterruptible Sleep (D) State
and then coming back.
Putting all together I suspect that is something related to Flask-Caching package and specially the FileSystemCache option. Since trying 'SimpleCache' cache option the problem disappeared (but cache doesn't really work with Gunicorn multiple workers).
For now my app.config is like bellow (worked flawlessly on the other ubuntu-server):
app.config['CACHE_DIR'] = 'cache'
app.config['CACHE_TYPE'] = 'FileSystemCache'
app.config['CACHE_THRESHOLD'] = 10000
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 300
I hope someone can point what I am missing here.