HHangfire Handbook

İLERİ

Performance & Scaling

Hangfire, birden fazla sunucuda dağıtık çalışabilir — otomatik senkronizasyon storage üzerinden yapılır.

Karar Rehberi

Durum Öneri Örnek veya gerekçe
>1K job/gün, tek sunucu yetmiyor Uygun: Multi-server E-ticaret Black Friday
Queue priority gerekli Uygun: Queue isolation Ödeme > email > analytics
Latency düşürmek Uygun: Polling interval azalt Sipariş onay < 5s
Basit cron job (günde 1) Uygun değil: Scaling gereksiz Tek worker yeterli
Resource sınırlı ortam Uygun değil: Worker artırma riskli 512MB container
Shared Storage SQL Server / Redis Server 1 Workers: 20 Server 2 Workers: 20 Server 3 Workers: 20 API Server Enqueue jobs Automatic coordination — no config needed

Worker Count Tuning

builder.Services.AddHangfireServer(options =>
{
    // CPU-bound: ProcessorCount
    // IO-bound (API calls, DB): ProcessorCount * 2-5
    // Mixed: ProcessorCount * 2 (default)
    options.WorkerCount = Environment.ProcessorCount * 3;

    // Queue priority
    options.Queues = new[] { "critical", "default", "low" };

    // Graceful shutdown timeout
    options.ShutdownTimeout = TimeSpan.FromSeconds(30);
    options.StopTimeout = TimeSpan.FromSeconds(15);

    // Server check interval
    options.HeartbeatInterval = TimeSpan.FromSeconds(30);
    options.ServerCheckInterval = TimeSpan.FromMinutes(5);
    options.ServerTimeout = TimeSpan.FromMinutes(5);

    // Schedule polling interval
    options.SchedulePollingInterval = TimeSpan.FromSeconds(15);
});

Multi-Server Deployment

// Ayrı server'lar farklı queue'lara odaklanabilir

// Server A — Critical jobs only (düşük latency)
builder.Services.AddHangfireServer(options =>
{
    options.ServerName = "critical-worker";
    options.Queues = new[] { "critical" };
    options.WorkerCount = 10;
});

// Server B — Default + Low priority (throughput)
builder.Services.AddHangfireServer(options =>
{
    options.ServerName = "bulk-worker";
    options.Queues = new[] { "default", "low" };
    options.WorkerCount = 40;
});

SQL Server Polling Optimization

// ÖNERİLEN: SlidingInvisibilityTimeout ayarlıysa TimeSpan.Zero kullanın
// Bu durumda Hangfire long-polling yapar (DB WAITFOR), polling gereksiz
new SqlServerStorageOptions
{
    QueuePollInterval = TimeSpan.Zero,              // Long-polling (en iyi latency, en az DB load)
    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
    TryAutoDetectSchemaDependentOptions = true,     // Schema 7'de otomatik aktif

    // Connection pool büyütün
    // ConnectionString'e: "Max Pool Size=100"
}
Parametre Varsayılan IO-Ağırlıklı CPU-Ağırlıklı
WorkerCount CPU * 5 CPU * 4 CPU
QueuePollInterval Zero (long-poll) Zero Zero
SlidingInvisibility 5m 5m 10m
ShutdownTimeout 15s 30s 60s

Örnek: Bir e-ticaret platformunda Black Friday için: 1 critical-worker (ödemeler, 10 worker), 3 bulk-worker (e-postalar, bildirimler, 40 worker each). Toplam 130 worker, tek SQL Server instance üzerinden koordineli çalışıyor — zero configuration distributed processing.