45K Views

Redis pub-sub vs Cron job in symfony

Most Common requirement of a website is to send email notification whenever any task or some event occurs.

For example – Email notification to employee on his/her birthday.

Using Cron job : 

If we will do this thing using the crone job then we have to run crone job everyday, it will check employee data in database and find the people those b’day date coming on the same day and each day we have to filter data if someone b’day date is on the search day.

Means everyday we have to interact with database and filter all the employee data and check if someone b’day date is coming on current day. It will obviously increase the load on site as to filter employees data everyday.

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 employee creation and expire that key just after that remaining time for his/her birthday.

A redis subscriber will listen the events and when it get a expire event we will get a expire key if expire key match with our pattern we will get employee detail from that key and instantly will send an email and again set a key with one year time for expire again and this cycle will be continue and we will get email notification for birthday without checking all data everyday.

Implementation:

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 controller we can call EmailBirthdayNotificationService just after 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 get expire 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 expire 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 me. I hope you’ll get a idea how crone job can be replaced by using redis pub-sub 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