An alert set for 08:15 should ring at 08:15 for a user in Paris and for a user in New York alike. On Android and iOS, a background job takes care of it every minute.
Our first version queried the database every minute. The current one reads a Redis index that only holds the alerts for the current UTC hour. This article explains why we switched, how the index stays up to date, and how it survives daylight saving time.
In short
- Problem: send each notification at the user's local time, wherever they live.
- Solution: a Redis index of the alerts for the current UTC hour, rebuilt every hour and read by a job every minute.
- Main trap: daylight saving time (DST), handled by the hourly rebuild and by a snapshot of time zone offsets.
A few definitions: a time zone is a region that shares the same legal time, for example Europe/Paris. Its UTC offset is the gap from universal time: +1 h or +2 h in Paris depending on the season. An index here is a lookup table of "UTC minute → alerts to send", kept in Redis so we don't query the database every minute.
The first, naïve version
For the MVP, we needed a working system quickly, even if that meant setting performance aside. The principle: a background job runs every minute, reads from the database the users with their time zone (saved at sign-up) and their habits, converts each alert's local time, then sends the ones that fall within the current minute.
What this version does well
- It is simple to build and maintain.
- It depends only on the database: a single source of truth, so no inconsistent data.
What it costs
- Database reads every minute.
- Performance that degrades as the number of users and habits grows.
Why it wasn't a problem at first
With few users, those reads cost almost nothing: there was no reason to build anything more complex. And since the system relies on a job that runs every minute, a few seconds, or even a minute, of delay makes no difference to the user.
An intermediate solution would have been to add a database index: the reads would have remained, but faster. We preferred to go further once growth justified it.
The current version: an index of the current UTC hour
One observation unlocked everything: between two time zones, the gap is a matter of hours, almost never of minutes. An alert at 08:15 local time lands on minute 15 of some UTC hour, whether the user is in Paris or New York.
A few time zones are offset by half an hour or 45 minutes (India at UTC+5:30, Nepal at UTC+5:45), but the principle holds: each alert is converted individually to a precise UTC minute (HH:mm). 08:15 in Delhi simply becomes 02:45.
So we only need to know the alerts of the current UTC hour, not those of the whole day. We keep that hour in a dictionary of 60 keys (from HH:00 to HH:59), rebuilt every hour and stored in RAM in Redis. The job reads this index every minute and the database is left alone.

What the index brings
- Only Redis is called every minute: reads are fast.
- The database is only hit once an hour, for the rebuild.
What it costs
- More complex logic.
- Two sources of truth, hence a risk of out-of-sync data.
- 60 keys and their values loaded in RAM.
Side by side
| Naïve version | Redis index of the UTC hour | |
|---|---|---|
| Source of truth | The database alone | Database and index (two sources) |
| Database reads | Every minute | One rebuild per hour |
| Cost per minute | Grows with users and their habits | One Redis access |
| Complexity | Low | Higher (synchronization, daylight saving) |
| Risk | Degrades as users grow | Index out of sync for at most one hour |
Keeping the index up to date
Two sources of truth is the classic problem: if the user changes an alert's time, the index is no longer up to date. Several remedies exist, and we combine them:
- Targeted update: when the user edits a habit, we remove that user's old alerts for the current hour, then recompute the new ones, without rebuilding the whole index.
- Hourly rebuild: the risk of desynchronization stays low, because it only affects the current hour.
- Daylight-saving detection: the index is rebuilt when a time zone's offset changes (see below).
- Database fallback: when a value is missing from the index, we fall back to the database query.
A special case: the automatically computed time
When the user doesn't set an exact time, the alert is computed from their last 10 completed tasks. A job that runs every 3 days at midnight takes care of it. The time of each task is expressed in the user's time zone, then we take a weighted average: the most recent task counts 10 times, the oldest only once. The notification therefore goes out at the best time for the user. A time chosen by the user is never overwritten.
This job does not update the index, and that has no consequence: the hourly rebuild picks up the new times by the next full hour at the latest. Only a habit whose old or new time falls within the current UTC hour can be shifted, by a few minutes, and the user isn't waiting for this background recalculation.
The daylight saving trap
The indexes are keyed on UTC minutes, while users think in local time. When a time zone changes its clock, an alert at "08:00 local" moves, for example, from 07:00 UTC to 06:00 UTC. An index built before the change would be off by one hour. We handle this with two mechanisms, one for each type of index.
1. Habits and todos: hourly self-healing
These indexes only cover the current UTC hour and are rebuilt every hour. The local → UTC conversion uses today's date, so it applies the offset in effect at that moment.
The next hour's rebuild therefore picks up the new offset, with no dedicated daylight-saving code. The shift is only visible between the time zone's clock change and the next full UTC hour: at most 59 minutes.
2. Daily reports: detection through an offset snapshot
This index is riskier: it covers all 1,440 minutes of the day (23:59 local time for each user) and isn't rebuilt every hour. It could have stayed wrong for months.
At build time, rebuildIndex() stores in Redis, along with the index, a snapshot of each time zone's UTC offset:
timezoneOffsets[user.timezone] = userNow.offset // e.g. { "Europe/Paris": 60 }
On every tick, the job rebuilds the index in three cases:
- it doesn't exist;
- the offset snapshot is missing (index built before this detection existed);
- the current offset of a time zone (
now.setZone(tz).offset, using Luxon) differs from the one in the snapshot.
A time zone missing from the snapshot (a new user's time zone) also triggers a rebuild, so that its offset is tracked from then on. The check stays cheap: a SELECT DISTINCT timezone and an in-memory comparison, instead of scanning 1,440 keys.
Why this approach holds up
- No hard-coded daylight-saving calendar. We compare the real offset provided by the IANA time zone database, so every country's rules are handled, including the ones that change.
- Rare rebuilds. They only happen at a real offset change, a few times a year.
- Safety net. If Redis or the rebuild fails, the database fallback recomputes with the current offset.
Why loading all of this in RAM isn't risky
Redis acts here as a disposable cache, not a database. It only stores the current UTC hour (60 keys) for habits and todos, or tiny lists of IDs (1,440 keys) for daily reports, with a TTL. The data stays very small.
Without an index, the cron job would scan every user and all their habits every minute: an O(N×M) cost per tick. With the index, a tick comes down to a single Redis GET, O(1) and with no database access. RAM is what makes a per-minute cron viable.
If Redis is flushed or goes down, the index is rebuilt from the database on the next tick, as soon as Redis is available again. Until then, the database fallback answers correctly. We get the speed of RAM without any risk to the data.
A possible improvement
The daily report index currently writes a key for each of the 1,440 minutes, even empty ones. Those empty keys distinguish "no alert this minute" ([]) from "index missing" (fall back to the database). Yet almost all of them are empty: users only land on a few minutes, one per time zone.
The next step is to replace them with a single Redis hash, with one entry for each minute at which users reach 23:59. If the hash exists, a missing minute means "no alert"; if it doesn't, we fall back to the database. The rebuild would write to a temporary key, then rename it with RENAME. That rename is atomic: a half-written index is never exposed. The memory gain is negligible, but the index becomes simpler and more robust.
Sources
- Time Zone Database, IANA: rules and history of time zones and clock changes.
- Zones, Luxon documentation: converting between time zones and reading the offset.
- Intl.DateTimeFormat, MDN: the
timeZoneoption, a native alternative to Luxon for converting a date to an IANA time zone. - RENAME and EXPIRE, Redis documentation: atomic key renaming and key lifetime.



