Introducing a new way to review your PRs.

Read the announcement

iv-org / invidious

Invidious is an alternative front-end to YouTube

View on GitHub

19 documents · Updated Oct 12, 2026

docs / background-jobs-and-notifications

Background jobs and notifications

Invidious uses long-running background jobs to keep channel data, user feeds, shared caches, instance data, statistics, and expired records current. New channel videos move from a feed event or channel refresh into a shared notification path, where they become stored user updates and live events for connected browsers.

Job system

The job system starts independent maintenance loops with the main Invidious process. Each job has a small configuration block and can be enabled or disabled without changing the job’s purpose. The process registers only the jobs allowed by the main feature settings, then starts the registered jobs together.

References: src/…/base_job.cr• L1-33 src/…/jobs.cr• L1-40 src/invidious.cr• L163-193

Maintenance work

Maintenance jobs update the data that users and public pages rely on. They use bounded concurrent work for channel and feed refreshes, while simpler jobs run at fixed intervals.

Job areaSystem purposeUser-facing result
Channel refreshFind new channel videos and update channel dataSubscription feeds and notifications gain new videos
Feed refreshRebuild a user’s subscription view after a changeThe subscription feed reflects current subscriptions
PubSub feed subscriptionKeep external feed subscriptions activeNew channel events can arrive without waiting for a full scan
Popular videosRefresh the shared popular-video setPopular pages use recent data
StatisticsRefresh service and usage figuresStatistics endpoints report current values
Expired-item cleanupRemove old cached videos and noncesStale records do not remain in the data store
Instance list refreshKeep the redirect target list currentRedirects can use healthy and recent instances

Channel refresh is the main source of discovered videos. It scans channels on a recurring schedule and slows its work after fetch errors. Feed refresh then handles users marked as needing an update, so channel changes and subscription changes do not require a request to rebuild the feed immediately.

References: src/…/refresh_channels_job.cr• L1-71 src/…/refresh_feeds_job.cr• L1-75 src/…/subscribe_to_feeds_job.cr• L1-50

The remaining jobs maintain shared service state rather than a single user feed. Cleanup retries sooner after a database error. Popular videos refresh often, statistics refresh on a longer cycle, and the instance list accepts only suitable remote instances before replacing the current list.

References: src/…/clear_expired_items_job.cr• L1-27 src/…/pull_popular_videos_job.cr• L1-20 src/…/statistics_refresh_job.cr• L1-70 src/…/instance_refresh_job.cr• L1-97

Notification flow

The notification flow turns a newly stored channel video into two user paths: a saved update in the account data and a live event for browsers that are watching subscribed channels. A VideoNotification is the small event record that carries the video ID, channel ID, and publish time between the discovery source and the notification job.

References: src/…/notification_job.cr• L1-29 src/…/feeds.cr• L404-456 src/…/channels.cr• L244-255

This diagram shows how a new video moves from discovery to stored and live notification results.

flowchart LR
    A[Channel refresh or PubSub webhook] --> B[Store new channel video]
    B --> C[Video notification event]
    C --> D[Notification job batches by channel]
    D --> E[Store IDs for subscribed users]
    D --> F[PostgreSQL notification event]
    F --> G[Authenticated event stream]
    G --> H[Browser ticker or system alert]
    E --> I[Subscription feed]

Both channel refresh and the PubSub webhook send an event only after a new video enters the channel-video data set. The notification job groups events by channel and removes duplicates during its short batch window. User notifications also mark affected feeds for refresh. If live user notifications are off, the system keeps the feed-update signal without sending the user-notification events.

References: src/…/channels.cr• L244-255 src/…/feeds.cr• L404-456 src/…/notification_job.cr• L32-99 src/…/users.cr• L122-165 src/…/config.cr• L154-155

User notification routes

The authenticated notification route exposes a Server-Sent Events stream. Server-Sent Events, or SSE, is a one-way HTTP stream that lets a browser receive updates without polling. The client supplies channel IDs as topics, and the server sends matching video data as events.

References: src/…/routing.cr• L318-321 src/…/authenticated.cr• L481-489 src/…/helpers.cr• L64-170

The browser starts one stream across its open tabs. It loads the account’s subscriptions, claims a shared browser-storage flag, and reconnects after a stream error. Each event increases the local notification count. When browser permission is granted, the client also shows a system alert that links to the new video.

References: assets/…/notifications.js• L1-75 assets/…/notifications.js• L90-131

The stream supports two data sources. A client can request stored notifications newer than a supplied time, and the active PostgreSQL event path can deliver new events while the connection stays open. Heartbeats keep the connection active. The stream is limited to the requested topics so one user does not receive unrelated channel updates.

References: src/…/helpers.cr• L64-170 src/…/notification_job.cr• L73-85

Notification state

Stored notification IDs provide the durable part of the feature. They are attached to users who subscribe to the channel and are also used to mark the affected subscription feed for a later refresh. The subscription feed can present those videos as a notification-only view or separate them from normal feed results according to user preferences. See accounts, sessions, and preferences for the wider account state model and persistence and schema migrations for the data store life cycle.

References: src/…/users.cr• L122-165 src/…/users.cr• L25-105 src/…/0004_create_users_table.cr• L5-19

Reading the subscription feed clears its stored notification markers because the feed view counts as the user seeing the update. Opening a notified video also removes that video’s marker. This creates a clear boundary between durable account state and the browser’s temporary live-event count.

References: src/…/feeds.cr• L94-103 src/…/watch.cr• L70-78 src/…/users.cr• L133-150 assets/…/notifications.js• L45-65

Configuration boundaries

Job registration and notification delivery are controlled at different levels. Main settings decide whether a feature-specific job is registered, while the job configuration can disable a registered job. User notifications have a separate global switch that controls their routes and delivery behavior.

References: src/invidious.cr• L163-193 src/…/jobs.cr• L12-40 src/…/base_job.cr• L4-30 src/…/config.cr• L121-155 src/…/routing.cr• L40-42

The main settings that shape this area are:

  • Channel and feed worker limits control how much refresh work can run at once.
  • PubSub feed settings control whether Invidious maintains external feed subscriptions.
  • Popular, statistics, and user-notification settings control optional system features.
  • The database URL and HMAC key support the event store and signed PubSub requests.

Change checklist

When these files or sections are changed, remember to consider these:

  • A change to the new-video event shape must stay aligned across channel discovery, the notification job, PostgreSQL events, and the SSE response builder.
  • A change to stored notification IDs must preserve the link between the users table, subscription-feed selection, feed-read clearing, and video-read removal.
  • A change to notification route availability must stay aligned between the feature switch, authenticated routing, and the browser stream startup.
  • A change to PubSub verification or delivery must preserve both signed hub requests and the handoff from webhook work to the shared notification channel.
  • A change to job registration or job names must preserve the YAML job configuration mapping generated from job types.

Important failure modes

TriggerConsequenceGuard
A new channel video is not insertedNo notification event is createdDiscovery sends the event only after a successful insert
A PubSub signature is invalidThe payload is ignored and no video update is processedThe webhook checks the HMAC signature before parsing the payload
A notification event names a channel outside the client topicsThe connected browser does not receive the eventThe stream filters PostgreSQL events by requested topics
A channel or feed refresh keeps failingRefresh work slows and data can remain oldRefresh jobs cap concurrent work and apply backoff or retry cycles
The notification database update failsStored markers and live delivery can lag behind discoveryThe notification loop logs the error and wakes again after its batch delay

Key files

FileWhy to read it
src/invidious.crShows which jobs start and which feature settings gate registration.
src/invidious/jobs.cr and src/invidious/jobs/base_job.crDefines the shared job lifecycle and job configuration model.
src/invidious/jobs/notification_job.crConnects discovered videos to stored user markers and live database events.
src/invidious/jobs/refresh_channels_job.crShows the main recurring source of channel-video discovery.
src/invidious/routes/feeds.crShows signed PubSub intake and the event created for each inserted video.
src/invidious/helpers/helpers.crShows how authenticated clients receive stored and live SSE events.
assets/…/notifications.jsShows how the browser claims a stream and surfaces updates to the user.
src/invidious/users.crShows how stored notification state affects the subscription feed.
src/invidious/database/users.crShows how user notification markers and feed-update flags change.