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:
upstream_response_time
and request_time
)I verified the problem doesn't exist (TTFB of some ms) on the same server running for example:
'Hello world'
Flask appLooking 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.
It was a permission issue. The folder somehow had wrong permissions for my user and I solved changing the cache dir to tmp
and my config became:
app.config['CACHE_DIR'] = '/tmp/my_site_cache/'
app.config['CACHE_TYPE'] = 'FileSystemCache'
app.config['CACHE_THRESHOLD'] = 10000
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 300
No more 10 or so seconds of Time To First Byte (TTFB).