70K Views

Redis pub-sub vs Cron job in Symfony

The most common requirement of a website is to send an email notification whenever any task or event occurs. For Example – Email notification to the employee on his/her birthday. We will be going to see the difference between Cron Job and Redis pub-sub.

Using Cron Job : 

If we set email notification using the crone job then we have to run the cron job every day. It will check employee data in the database and find the people that who’s birthday it is.

Each day we have to filter data if someone’s birthday is on the search day. This means every day we have to interact with the database and filter all the employee data. Also, it checks if someone’s birthday is coming on the current day.

It will obviously increase the load on-site to filter employees’ data every day.

Using Redis :

Instead, we can use Redis pub-sub in which we can set a key with employee email and time remain from his/her date of birth at the time of employment creation and expire that key just after that remaining time for his/her birthday.

A Redis subscriber will listen to the events and when it gets an expired event we will get an expired key.

If the expired key matches our pattern, we will get employee detail from that key and it will send an email instantly and again set a key with one year time for expiring again.

This cycle will be continuing and we will get an email notification for a birthday without checking all data every day.

Implementation:

The first thing we need to ensure we have Redis installed or not if not then install Redis first and then proceed further.

We can create a service named with employee.bithday.notification in services.yaml.

EmailBirthdayNotificationService.php

 

Now In the controller, we can call EmailBirthdayNotificationService just after the employee created:

$setBdayNotificationKey = $this->container->get(’employee.bithday.notification’)->setBdayKey($email, $timeToExpire);

Here $timeToExpire will be remaining time form current year date of birth to next year date of birth time in seconds.

We have to make a Symfony command file, which will check if any key gets expired and it matches with our key pattern it will send an email and at the same time again set a key for next year with expiration time of one year.

I’ve explored this while contributing to Symfony based project UVdesk, there are a lot more things to learn and you could also contribute to an enterprise-level open source helpdesk.

Thanks for reading this blog. I hope you’ll get an idea of how Redis pub-sub can replace the crone job as per requirement. Please share your reviews on this, that will support me to write more.

 

UVdesk Forum!          Developer Visit!            Contact Us!          Live Demo!

Category(s) Symfony UVdesk
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php