1

I have a FastAPI application which sends emails when the users register on the website. The app is implemented in such a way that there is a cron task (scheduled every minute) which checks the database and if a flag is set, it will try to send an email.

Deployment: Two instances of the FastAPI application are running connected to a locally hosted MYSQL database

Problem: Since, there are two instances of the Application running, each one will trigger a cron job every minute and this results in sending an email twice.

How to synchronise between multiple processes? Please help me with this issue. Thanks

  • Which database are you using? Most databases allow you to acquire a lock for a table, so that you can implement locking across multiple processes that read or write to the same database. Another option is to generate a random id on each node for each invocation, then use an UPDATE query to set this value in a `handled_by` column with a `WHERE handled_by IS NULL` expression- then select the rows that got updated (make sure to end a transaction by committing so that you actually get the assigned the row to your process). That way only one process will be assigned the task. – MatsLindh Mar 15 '22 at 09:01
  • @MatsLindh But, what is the rationale of having two instances of the app running in the first place (and having a task running every minute to perform checks)? Why not running a single instance, and use [`BackgroundTasks`](https://fastapi.tiangolo.com/tutorial/background-tasks/#using-backgroundtasks) to send emails upon registration? – Chris Mar 15 '22 at 09:30
  • 1
    @Chris Usually because you've decided to use background tasks in a container (using apscheduler or something similar), and that container is then deployed in multiple instances. Using k8s cron is another option in that case; but it all depends on the platform and environment. Having a standardized image that have the cron functionality built-in reduces the number of special cases that are necessary to handle from the deployment side. – MatsLindh Mar 15 '22 at 09:42
  • For such a use case, you'd need semaphores. Check out celery, that might help. – Irfanuddin Mar 15 '22 at 12:14

0 Answers0