27. 7. 2023 v IT

Resolving slow sorl thumbnail when using cloud storage

Most Django applications today are deployed in the cloud. To make them horizontally scalable, it definitely makes sense to make them stateless. And one of the things to do when making a Django app stateless is not to store media files on the file system, which is Django's default behaviour. Typically you will use some file S3-like storage instead. 

The problem I ran into with cloud storage was that sort thumbnail (library for generating image thumbnails) was super slow when using cloud storage, resulting in time to first byte increasing from milliseconds to seconds.

It took me hours of investigation to find out why and the solution is to set the AWS_S3_CUSTOM_DOMAIN variable.

Once the variable is set correctly, the slowdown is gone.

The cause is hidden deep in the botos3 storage. If the AWS_S3_CUSTOM_DOMAIN variable is not set, the library queries the S3 API to construct an image URL.

 For more details check this stack overflow answer. Kudos to James Lin who figured it out.


Čtěte dále