InMemory Scheduler

The InMemory Scheduler is a lightweight, timer-based scheduling implementation provided by Brighter for testing, development, and demonstration purposes. It requires no external dependencies and stores scheduled jobs in memory using timers.

Important Warning

The InMemory Scheduler is NOT durable and is NOT recommended for production use.

  • No Persistence: All scheduled jobs are lost if the application crashes or restarts

  • No Distribution: Cannot be shared across multiple application instances

  • No Recovery: Failed jobs are not automatically retried after restart

  • Memory Bound: All scheduled jobs are held in memory

Use this scheduler for:

  • Unit and integration tests

  • Local development

  • Demos and proof-of-concepts

  • Production scenarios where losing scheduled work is acceptable

What is the InMemory Scheduler?

The InMemory Scheduler uses .NET's ITimerProvider internally to schedule delayed execution of messages. When you schedule a message:

  1. Brighter creates an in-memory timer for the specified delay

  2. The timer fires at the scheduled time

  3. Brighter dispatches your message to the appropriate handler

  4. The timer is removed from memory

This simple approach makes it perfect for testing but unsuitable for production systems that require durability.

Architecture

When to Use InMemory Scheduler

Unit Testing:

Limited Production Scenarios

The InMemory Scheduler might be acceptable in production only if:

  • Loss of scheduled work is acceptable (non-critical notifications, analytics, etc.)

  • Your application rarely restarts

  • Scheduled work has short delays (minutes, not hours/days)

  • You have alternative mechanisms to recover lost work

Example - Acceptable Production Use:

Configuration

Basic Configuration

Configure the InMemory Scheduler with InMemorySchedulerFactory:

Environment-Specific Configuration

Use InMemory for development, production schedulers elsewhere:

Configuration with Custom Timer Provider

The InMemory Scheduler uses ITimerProvider internally. You can provide a custom implementation for testing:

NuGet Package

To use the InMemory Scheduler, install the NuGet package:

Package: Paramore.Brighter.InMemoryScheduler

Code Examples

Basic Scheduling

Schedule a command with a delay:

Scheduling with Absolute Time

Schedule for a specific time:

Cancelling a Scheduled Job

Cancel a previously scheduled job:

Testing with InMemory Scheduler

Example unit test using InMemory Scheduler:

Comparison with Production Schedulers

Feature
InMemory
Quartz.NET
Hangfire
AWS Scheduler
Azure Service Bus

Persistence

None

Database

Database

AWS

Azure

Distribution

No

Yes

Yes

Yes

Yes

Cancellation

Yes

Yes

Yes

Limited

No

Dashboard

No

Limited

Yes

AWS Console

Azure Portal

Setup Complexity

Minimal

Moderate

Easy

️Moderate

Easy

Production Ready

No

Yes

Yes

Yes

Yes

Testing

Ideal

Overkill

Overkill

No

No

Best Practices

1. Use for Testing Only

2. Document Production Limitations

If you use InMemory in production, document why:

3. Keep Delays Short

If using in production, keep delays under a few minutes:

4. Test Scheduler Behavior

Write tests that verify scheduled behavior:

5. Don't Rely on It for Critical Work

Migration to Production Scheduler

When moving to production, replace InMemory with a durable scheduler:

Before (Development):

After (Production with Hangfire):

After (Production with Quartz):

No code changes required - just swap the scheduler factory!

Troubleshooting

Scheduled Jobs Not Executing

Symptom: Jobs scheduled but never execute

Possible Causes:

  1. Application stopped before timer fires

  2. Delay too short (already passed)

  3. Exception in handler preventing execution

Solution:

Scheduled Jobs Lost After Restart

Symptom: Application restart loses all scheduled jobs

Cause: This is expected behavior - InMemory scheduler has no persistence

Solution: Use a production scheduler (Quartz, Hangfire) if you need durability

Memory Usage Growing

Symptom: Memory consumption increases over time

Cause: Too many scheduled jobs held in memory

Solution:

  • Reduce number of concurrent scheduled jobs

  • Use shorter delays

  • Consider a production scheduler with external storage

Summary

The InMemory Scheduler is a lightweight, zero-dependency scheduling solution perfect for:

  • Unit and integration tests - No external dependencies

  • Local development - Fast and simple

  • Demos and POCs - Quick to set up

NOT recommended for production due to:

  • No persistence - Jobs lost on restart

  • No distribution - Single-process only

  • No recovery - No durability guarantees

Use production schedulers (Quartz.NET, Hangfire, AWS Scheduler, Azure Service Bus Scheduler) for any system requiring durability and reliability.

Last updated

Was this helpful?