Skip to content
Edit this page

The Redis store

The Redis store (dotnet add package Bellhop.Redis --prerelease) keeps every job, schedule and recurring job on a Redis or Redis-compatible server. It is fast, and it is not a database: read what it can lose, below, before you choose it for jobs you cannot afford to lose. When every job matters, use PostgreSQL or SQL Server, or one of the durable servers named under "Durable servers".

builder.Services.AddBellhop(bellhop => bellhop
    .UseRedis(builder.Configuration.GetConnectionString("Redis")!)
    .AddWorker());

1.0 supports Valkey 8.1, 9.0 and 9.1, Redis 7.2 and 8.x without Redis 8's modules, AWS ElastiCache for Valkey (serverless included), AWS MemoryDB and Google Cloud Memorystore for Valkey: standalone, with Sentinel and as a Cluster, with TLS and ACL users. Redis key layout lists every key the store writes and the commands an ACL user needs.

No transactional enqueue

Transactional enqueue is not available on the Redis store: a job cannot be created inside your application's database transaction, because Redis cannot take part in it. Passing EnqueueOptions.Transaction, or calling dbContext.EnqueueAsync, fails with NotSupportedException and stores nothing. When a job must exist exactly when your data is committed, keep the jobs in the database that holds the data: the PostgreSQL, SQL Server and SQLite stores support it, as enqueueing inside the application's transaction describes.

What can be lost

A Redis server acknowledges a write before it is safe on disk or on a replica. These are the windows in which a job the store has accepted can be lost, or run twice.

  • A crash of a server that writes no append-only file. A server that persists by snapshot only, or not at all, loses every write since its last snapshot when it crashes: enqueued jobs, outcomes, schedules. The store warns when it connects to such a server (PersistenceWithoutAof, 4308). With appendfsync everysec, Redis's usual setting, a crash can still lose about the last second of writes.
  • A failover to a replica. Replication is asynchronous, so a replica that takes over may not have the primary's last writes. The store notes this when it connects to a primary with replicas (ReplicationAsynchronous, 4310).
  • A restore or failover to older data. After a restore from a backup, or a failover to a replica that lagged behind, the jobs claimed since that copy may run again, and their outcomes may be lost, until a worker detects the rollback and reports it as a reset (StoreWasReset, 4315). Jobs run at least once, never at most once, so make handlers idempotent, as for every store.
  • A rollback to a recent copy is not detected. Worker nodes move a generation marker forward every RecurringReconcileInterval (5 minutes by default), and a rollback is detected when a worker finds the marker older than the one it saw. A rollback to a state less than one RecurringReconcileInterval old is not detected, as that copy can hold the very marker the workers last saw: nothing is reported, and the jobs claimed since that state may run again.
  • A flush first seen by a newly started worker. After a FLUSHALL, the first worker to read the empty store reports the reset if it had seen the marker before. A worker that started after the flush has not, so it initialises the store as a new one without a report. The flush then goes unreported if every worker that saw the previous marker is paused until the marker is next advanced: paused means it makes no read of the marker between the flush and that advance, which includes a worker whose store is still connecting. The outcomes of the runs those workers finish are still recorded.
  • What these undetected losses also remove. When a reset is detected, the store keeps a record of it until an operator acknowledges it, and the health check reports Degraded meanwhile. If the script that detected a reset ran but its reply was lost, no node remembers that record. Either undetected loss above, the rollback within one RecurringReconcileInterval or the unreported flush, can then remove the unacknowledged record of that earlier reset with the rest of the data: a flush always does, and a rollback does when the copy it brings back is older than the record. No node reports it, and health returns to Healthy without an acknowledgement.
  • A failover to a server whose clock is behind is reported as a reset, whether or not data was lost: the marker carries the server's time, so the next marker that server writes is older than the one the workers saw. Keep the servers' clocks synchronised.
  • Eviction. A server that evicts keys at maxmemory can delete any job's data, one key at a time, and no reset is detected. See the next section.

What a detected reset means, what to check after it and how to acknowledge it on the dashboard are under health checks on the production page.

The store refuses to start unless maxmemory-policy is noeviction

A node does not start when the server's maxmemory-policy is anything but noeviction: the host's start fails with a message that names the setting. Set it on the server, or in your hosted service's parameter group, before the first node starts. A running node that a failover moves to a server with another policy keeps running and logs ServerMayEvict (4311, Error) each time it connects: resets caused by eviction there are not detected, so restore the policy.

With noeviction the server refuses writes once it reaches maxmemory. The health check reports Degraded above MemoryDegradedShare (80%) of maxmemory, so you hear of it first.

The pause after a flush, and on a new server

After a flush, every worker node pauses claims and recurring occurrences for one LeaseDuration (60 seconds by default), while it writes its registration and code-defined recurring jobs again and running jobs write their records back. The first worker to start on a new server, which has no marker yet, pauses the same way, so the first claim on a new installation waits one LeaseDuration. Enqueues are not held: jobs wait, enqueued, until the pause ends. Due recurring instants are handled by their misfire policy when it ends.

A job enqueued during that pause with the unique key of a job that is still running is cancelled with reason DuplicateAfterReset, when the running job writes its record back (DuplicateCancelledAfterReset, 4324). It never ran; enqueue it again if it is still wanted.

Tests that start a fresh Redis server, or flush one between tests, wait out this pause before the first claim. Lower LeaseDuration in the test host to shorten it:

services.AddBellhop(bellhop => bellhop
    .UseRedis(connectionString)
    // On a new or flushed server, the first claims wait one LeaseDuration.
    .Configure(options => options.LeaseDuration = TimeSpan.FromSeconds(2))
    .AddWorker());

A worker renews a running job's lease every LeaseDuration / 3, so with a 2-second lease a renewal delayed by more than about a second, as on a test machine whose threads are all blocked, can lose the lease and let the job run again. Leave LeaseDuration at its default in production: it is also how long a crashed node's jobs wait before they run again.

Durable servers

For durability-critical workloads, run the store on a server that acknowledges a write only once it is durable:

  • AWS MemoryDB, which keeps a durable multi-zone transaction log, and is supported in 1.0.
  • AWS ElastiCache for Valkey 9 with synchronous durability, on node-based clusters, supported in 1.0.
  • Apache Kvrocks, which stores data on disk, for running your own server. It is a Tier 2 server: 1.0 does not test it yet, and support is planned for a later 1.x release.

Even on these, the rules above about rollbacks, flushes and clocks still apply: they come from the store's reset detection, not from the server.