Manage events entities is pretty easy. You define few informations like a subject, a content, and maybe the most important, dates (a start and a end date of course).
You’re ready to create calendar or timeline features. Baooom! Now you need to be able to create recurrent event ! And everything is going to fuck’up.
I’ve work on this kind of feature, and here are my small advices:
Don’t store recurring information like an asshole
Start well, don’t create your own format like this:
Don’t use the cron format too.
Use the correct format for it, it’s called RRULE and it have been define since 1998 by major actor like apple (for it iCalendar).
Using this rule will provide you well documented procedures, already developed and tested plugins and a solid exchangeable format (Javascript, Php, and others).
Should I store the recurrent event or calcul it ?
This is the most important question, and maybe the most difficult choice too made. It took me month to find a correct answer and clearly understand why I’ve choose one.
I’ve read a lot of article about it, how major companies works, how small companies does it, how senior developer does it, how juniors does it… Reading documentations, study exemples case, read, test and then read more and test more…
To understand the challenge between calculation and storage, let’s see a basic exemple:
Scenario: I create a recurring event the first of january 2010. It will be repeat every friday (limitless). On this monday (21/12/2015) I’m asking my calendar to display me event’s of the week (so including a friday).
The backstage: Application will have to retrieve all old and terminate events with a recurrence, calculate for each all the recurrencies of them until they match the requested period (21/12/2015 00h00 to 27/12/21015 23h59).
Storage solution issue: If we have thousand of daily recurrency events with very old starting date, calculation become a serious problem. Storing calculated event allow your application to focus on a simple task, retrieve events for a period, that’s all. Like a simple date period query, it’s very fast to retrieve. But if user do modification to the recurrency rules, it can be hard to save it well. You will have to answer simple question like, when should I stop saving events ? With a date limit ? With max count limit ? If I have thousand of users each creating few weekly recurring event, storing them for the next ten years, I will have to store a huge quantity of events.
Calculate solution issue: Calculate event is the simplest solution. You apply calcul with your RRULE, then you have the answer. If user edit the RRULE, no problem, nothing to do on server side. But for our scenario, we should calculate the 310 previous dates to get our matching event. If we create 10 recurrent events like this one each year, that will need a lot of calcul for each period request.
Blue pill or Red pill ?
Will I have an intensive use of my events feature ? Will I have thousand of users using it daily ? If no maybe you should look for the calculating solution.
Is my language slow for calculating dates* ? Do I have complex filtering that ruin caching result bonus ? Do I need to keep extra information like “alarm reminder notification” status for a recurrent event ? If yes, maybe you should look for the storage solution.
*: PHP 5.X is not fast for this kind of task
Both of two world
If you’re like me, working on a Symfony application with complex business logic, you may probably wan’t to speed up this.
We wan’t to retrieve events recurrence very fast without storing them.
Redis seems perfect for this job !
To me, the perfect solution look like redis key with date and event identifier. They will be generate by a command, during a small traffic hour. Generate them for the next 2 year seems enough for most of the usage. If a user need to see events out of this period, fallback should be able to calcul it (and cache it in redis). If a user edit a recurrence rule, we can re-calcul new ones.
It sound fast with a small storage footprint. What do you think about it ?
I hope it will help you. If you have any ideas or tips about recurring events, just tell me.