[django-allauth] Django responds very slowly when user is signing up and first logging in

I’m using django-allauth to handle user signup/login. The current setup works, but the response from the server is incredibly slow (>5s, other types of responses are usually <500ms).

In base.py (I used cookiecutter-django to setup everything), the relevant settings are

LOGIN_REDIRECT_URL = "myapp:home"
LOGIN_URL = "account_login"
ACCOUNT_SIGNUP_REDIRECT_URL = reverse_lazy("myapp:accept-terms-of-service")
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_ADAPTER = "project.users.adapters.AccountAdapter"

In adapters.py,

class AccountAdapter(DefaultAccountAdapter):
    def is_open_for_signup(self, request: HttpRequest) -> bool:
        return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)

    def send_mail(self, template_prefix, email, context):
        mailing_thread = threading.Thread(
            target=super().send_mail,
            args=(template_prefix, email, context),
        )
        mailing_thread.start()

In views.py

class CustomLoginView(LoginView):
    def form_valid(self, form):
        response = super().form_valid(form)
        user_id = self.request.user.id
        if not UserProfile.objects.filter(user_id=user_id).exists():
            return redirect("myapp:accept-terms-of-service")
        return response

def home(request):
    return render(request, "myapp/home.html")

The idea is to send a verification email to the user when he signs up. And when he first signs in, he will need to go to the process of accepting the legal documents.

The responses of two requests are very slow: 1. When the user is signing up, there is a significant delay before the server makes a response, though ultimately the user receives the correct email. 2. When the user first logs in, there is a significant delay before showing the accept terms page.

I have tried to use both django celery and threads to send the email, but the delay doesn’t change at all (BTW all of them works, just very slow). So it’s probably not a blocking problem. I am not sure if this is related to how boto3 is configured because it seems that AWS SES is ultimately sending the right email. But the following appears in the terminal each time when a user signs up:

DEBUG 2024-07-17 22:31:32,245 loaders 32086 139956360050368 Loading JSON file: /home/admin/.pyenv/versions/3.11.9/envs/django/lib/python3.11/site-packages/boto3/data/s3/2006-03-01/resources-1.json
DEBUG 2024-07-17 22:31:32,248 loaders 32086 139956360050368 Loading JSON file: /home/admin/.pyenv/versions/3.11.9/envs/django/lib/python3.11/site-packages/botocore/data/endpoints.json
DEBUG 2024-07-17 22:31:32,270 loaders 32086 139956360050368 Loading JSON file: /home/admin/.pyenv/versions/3.11.9/envs/django/lib/python3.11/site-packages/botocore/data/sdk-default-configuration.json
DEBUG 2024-07-17 22:31:32,287 loaders 32086 139956360050368 Loading JSON file: /home/admin/.pyenv/versions/3.11.9/envs/django/lib/python3.11/site-packages/botocore/data/s3/2006-03-01/service-2.json.gz
DEBUG 2024-07-17 22:31:32,321 loaders 32086 139956360050368 Loading JSON file: /home/admin/.pyenv/versions/3.11.9/envs/django/lib/python3.11/site-packages/botocore/data/s3/2006-03-01/endpoint-rule-set-1.json.gz
DEBUG 2024-07-17 22:31:32,337 loaders 32086 139956360050368 Loading JSON file: /home/admin/.pyenv/versions/3.11.9/envs/django/lib/python3.11/site-packages/botocore/data/partitions.json
DEBUG 2024-07-17 22:31:32,345 configprovider 32086 139956360050368 Looking for endpoint for s3 via: environment_service
DEBUG 2024-07-17 22:31:32,345 configprovider 32086 139956360050368 Looking for endpoint for s3 via: environment_global
DEBUG 2024-07-17 22:31:32,345 configprovider 32086 139956360050368 Looking for endpoint for s3 via: config_service
DEBUG 2024-07-17 22:31:32,345 configprovider 32086 139956360050368 Looking for endpoint for s3 via: config_global
DEBUG 2024-07-17 22:31:32,346 configprovider 32086 139956360050368 No configured endpoint found.
DEBUG 2024-07-17 22:31:32,355 loaders 32086 139956360050368 Loading JSON file: /home/admin/.pyenv/versions/3.11.9/envs/django/lib/python3.11/site-packages/botocore/data/_retry.json
DEBUG 2024-07-17 22:31:32,357 factory 32086 139956360050368 Loading s3:s3
DEBUG 2024-07-17 22:31:32,358 factory 32086 139956360050368 Loading s3:Bucket
DEBUG 2024-07-17 22:31:32,358 model 32086 139956360050368 Renaming Bucket attribute name

I would choose Celery for this task. What is your configuration about Celery, workers, tasks etc?

Turns out it was not an email problem at all. It has something to do with caches settings.