Tu Le Thanh
Tu Le Thanh

Reputation: 661

How speed up sending email in Django rest framework?

I have a backend server that allows users to track the price of products on an ecommerce site. Whenever the price drops down, I'll have to send an informing email to my users.

I have been written an API that updates the price of products and at the same time, sending an email if the price has dropped. So, the are thousands of emails have to be sent in one API call.

Currently, I'm using django.core.email to sending email to users but it takes so long each time sending an email (about 3s-4s for each. If it has 5 product's prices dropped, the time of the API call will be around 20s).

Is there any way to make a lot of emails sent within the shortest amount of time in my case?

Upvotes: 1

Views: 835

Answers (1)

Gabriel Muj
Gabriel Muj

Reputation: 3805

I think there are two things you need to consider:

1. You don't want to make the api call wait to send email.

You should consider using celery together with this library django-celery-email as email backend. In this case sending an email will happen asynchronously (the actual email send will happen on a celery worker) and api call will not wait for it, thus api call will be faster.

Note that in this case you won't be able to know when/if the email was delivered in the api view (since it will happen asynchronous), so keep that in mind. If you really need it, there are ways to find out by storing celery task results, check django-celery-results so you can query that table using celery task ids.

2. You want to send mass emails faster.

Have a look at django mass-email, this help sending multiple emails by using a single connection to the mail server. This works also if you use django-celery-email.

Upvotes: 1

Related Questions