
Scheduler, retry, timeout, tránh chạy trùngSchedulers, retries, timeouts, no double runs
300.000 việc chạy ngầm300,000 jobs behind the scenes.Mỗi ngày Shop Việt gửi hóa đơn điện tử, email xác nhận, xuất báo cáo và dọn dữ liệu bằng khoảng 300.000 job. Lúc cao điểm có 50 job mỗi giây, chạy trên 6 worker.Every day Shop Viet sends e-invoices, confirmation emails, report exports and cleanups as about 300,000 jobs. At peak there are 50 jobs per second across 6 workers.
Theo chân job gửi hóa đơn inv-7781 của một đơn vừa đặt: bảng job trong PostgreSQL vừa là hàng đợi vừa là nguồn sự thật, nên job được INSERT cùng transaction với đơn hàng. Worker nhận job bằng SKIP LOCKED, giữ lease 60 giây có heartbeat, còn Lease Reaper trả lại job khi lease hết hạn. Chạy luồng bình thường trước, rồi thử worker chết, job quá timeout và cron đêm trên 6 instance để xem vì sao không job nào bị mất hay chạy trùng.Follow invoice job inv-7781 for a freshly placed order: the PostgreSQL job table is both the queue and the source of truth, so the job is INSERTed in the same transaction as the order. Workers claim jobs with SKIP LOCKED and hold a 60-second lease with heartbeats, while the Lease Reaper returns jobs whose lease expired. Play the normal run first, then try a crashing worker, a job hitting its timeout and the nightly cron on 6 instances to see why no job is lost or run twice.
1Order Service INSERT job gửi hóa đơn trong cùng transaction tạo đơn. Đơn rollback thì job cũng biến mất, đơn commit thì job chắc chắn tồn tại.The Order Service INSERTs the send-invoice job in the same transaction that creates the order. If the order rolls back the job disappears; if it commits the job is guaranteed to exist.
SELECT … FOR UPDATE SKIP LOCKED trong cùng câu UPDATE đổi trạng thái sang RUNNING: worker này bỏ qua dòng worker kia đang khoá, không chờ, không trùng.SELECT … FOR UPDATE SKIP LOCKED inside the UPDATE that sets RUNNING: each worker skips rows another one holds, with no waiting and no duplicates.
Mỗi job có lease locked_until. Worker gia hạn bằng heartbeat; reaper trả job có lease hết hạn về QUEUED. Lúc ghi kết quả phải kiểm locked_by để worker cũ không ghi đè.Each job has a lease, locked_until. The worker extends it with heartbeats; a reaper returns expired leases to QUEUED. Check locked_by when writing the result so a stale worker cannot overwrite it.
Mỗi lần chạy cron ghi một dòng với unique(job_name, scheduled_for); instance nào INSERT được thì chạy, instance khác nhận lỗi trùng và bỏ qua. Hoặc dùng leader election (advisory lock, ShedLock).Each cron firing inserts a row with unique(job_name, scheduled_for); the instance whose INSERT succeeds runs it, the others hit the unique violation and skip. Or use leader election (an advisory lock, ShedLock).
Mỗi job chạy trong context có deadline; mọi I/O nhận context đó nên bị huỷ đúng lúc. Ở Java, Future.get(timeout) rồi cancel(true) và handler phải tôn trọng interrupt.Every job runs under a context with a deadline; all I/O takes that context and is cancelled on time. In Java, Future.get(timeout) then cancel(true), and the handler must honour interrupts.
Lỗi tạm thời được hẹn lại với backoff có jitter; lỗi dữ liệu hoặc hết số lần thử chuyển sang DEAD để người xem, không thử mãi.Transient errors are rescheduled with jittered backoff; bad data or exhausted attempts go to DEAD for a person to review, never retried forever.
Job có thể chạy hơn một lần sau crash hoặc timeout. Handler dùng idempotency key với bên ngoài và kiểm tra trạng thái trước khi ghi.A job can run more than once after a crash or a timeout. Handlers pass an idempotency key to external systems and check state before writing.
| BảngTable | Cột chínhKey columns | Ghi chúNotes |
|---|---|---|
job | id, type, key, payload, status, priority, run_at, attempts, max_attempts, timeout_s, locked_by, locked_until, last_error, created_at | QUEUED → RUNNING → DONE · QUEUED (retry) · DEAD; index (status, priority, run_at); unique(key)QUEUED → RUNNING → DONE · QUEUED (retry) · DEAD; index (status, priority, run_at); unique(key) |
cron_run | job_name, scheduled_for, instance_id, job_id, created_at | unique(job_name, scheduled_for): mỗi lần hẹn chỉ một instance tạo jobunique(job_name, scheduled_for): one instance creates the job per firing |
job_attempt | job_id, attempt, worker_id, started_at, finished_at, outcome, error | Lịch sử từng lần chạy, hiển thị trên dashboardEvery run, shown on the dashboard |
JobWorker (Java) và Worker (Go) là các hộp Worker A, Worker B ở sơ đồ: nhận job từ hộp Job Table theo số luồng còn trống bằng SKIP LOCKED, chạy handler gọi hộp Billing / Email API trong giới hạn thời gian, rồi ghi DONE, hẹn lại hoặc chuyển sang hộp DEAD jobs. Worker.heartbeat gia hạn lease; khi worker chết, hộp Lease Reaper trả job về QUEUED.JobWorker (Java) and Worker (Go) are the Worker A and Worker B boxes in the diagram: they claim jobs from the Job Table box with SKIP LOCKED according to free slots, run the handler against the Billing / Email API box within a time limit, then record DONE, a retry or a move to the DEAD jobs box. Worker.heartbeat extends the lease; when a worker dies, the Lease Reaper box puts the job back to QUEUED.
package com.example.jobs;
import static com.example.jobs.Job.JOB_MAPPER;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class JobWorker {
private static final String CLAIM = """
UPDATE job SET status = 'RUNNING', locked_by = ?, attempts = attempts + 1,
locked_until = now() + interval '60 seconds'
WHERE id IN (SELECT id FROM job
WHERE status = 'QUEUED' AND run_at <= now()
ORDER BY priority DESC, run_at
LIMIT ? FOR UPDATE SKIP LOCKED)
RETURNING *""";
private final JdbcTemplate jdbc;
private final JobRepository jobs;
private final Map<String, JobHandler> handlers;
private final ThreadPoolTaskExecutor slots; // core = max = 8
private final ExecutorService handlerPool;
private final String workerId;
@Scheduled(fixedDelay = 1000)
public void poll() {
int free = slots.getMaxPoolSize() - slots.getActiveCount();
if (free <= 0) return;
List<Job> batch = jdbc.query(CLAIM, JOB_MAPPER, workerId, free);
batch.forEach(job -> slots.execute(() -> run(job)));
}
void run(Job job) {
Future<?> f = handlerPool.submit(() -> handlers.get(job.type()).handle(job));
try {
f.get(job.timeoutSeconds(), TimeUnit.SECONDS);
jobs.markDone(job.id(), workerId); // WHERE locked_by = ?
} catch (TimeoutException e) {
f.cancel(true); // interrupt the handler
jobs.retryOrDead(job, workerId, e);
} catch (Exception e) {
jobs.retryOrDead(job, workerId, e); // run_at = now() + backoff
}
}
}package main
import (
"context"
"errors"
"sync"
"time"
)
func (w *Worker) Run(ctx context.Context, slots int) {
var wg sync.WaitGroup
for i := 0; i < slots; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for ctx.Err() == nil {
if err := w.runOne(ctx); errors.Is(err, ErrNoJob) {
time.Sleep(time.Second)
}
}
}()
}
wg.Wait() // graceful shutdown: running jobs finish first
}
func (w *Worker) runOne(ctx context.Context) error {
job, err := w.store.Claim(ctx, w.id, 60*time.Second) // FOR UPDATE SKIP LOCKED
if err != nil {
return err
}
jobCtx, cancel := context.WithTimeout(ctx, job.Timeout)
defer cancel()
go w.heartbeat(jobCtx, cancel, job.ID)
err = w.handlers[job.Type].Handle(jobCtx, job)
switch {
case err == nil:
return w.store.MarkDone(ctx, job.ID, w.id)
case job.Attempts >= job.MaxAttempts:
return w.store.MarkDead(ctx, job.ID, w.id, err)
default:
delay := backoff(job.Attempts) // 10s·2^n + jitter, capped at 1h
return w.store.Reschedule(ctx, job.ID, w.id, time.Now().Add(delay), err)
}
}
func (w *Worker) heartbeat(ctx context.Context, cancel context.CancelFunc, id int64) {
t := time.NewTicker(20 * time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
if err := w.store.ExtendLease(ctx, id, w.id, 60*time.Second); err != nil {
cancel() // lease lost: stop the handler too
return
}
}
}
}| Bảng job (Postgres)Job table (Postgres) | Broker (RabbitMQ, SQS)Broker (RabbitMQ, SQS) | |
|---|---|---|
| Hợp vớiGood for | Dưới vài trăm job/giây, cần truy vấn và sửa jobUp to a few hundred jobs/s, when you need to query and edit jobs | Lưu lượng lớn, nhiều consumerHigh volume, many consumers |
| Giao dịchTransactions | Enqueue cùng transaction với dữ liệu nghiệp vụEnqueue in the same transaction as business data | Cần outbox để không mất jobNeeds an outbox to avoid losing jobs |
| Hẹn giờ, hiển thịScheduling, visibility | run_at và dashboard chỉ là câu SQLrun_at and the dashboard are plain SQL | Delay giới hạn, khó xem từng jobLimited delays, individual jobs are hard to inspect |
| Chọn khiPick it when | Đã có Postgres, team nhỏPostgres is already there and the team is small | Bảng job làm nghẽn DB chínhThe job table starts to load the main database |
Job xuất báo cáo chạy 90 giây với lease 60 giây và không heartbeat: reaper trả job về QUEUED khi nó vẫn đang chạy, và worker thứ hai làm lại từ đầu. Gửi heartbeat mỗi lease/3 (20 giây) để gia hạn locked_until, và đặt timeout của job luôn nhỏ hơn lease. Đếm số lần reaper thu hồi job từ worker vẫn còn sống: con số khác 0 là dấu hiệu cấu hình sai.A report export runs 90 seconds with a 60-second lease and no heartbeat: the reaper requeues it while it is still running, and a second worker starts over. Heartbeat every lease/3 (20 seconds) to extend locked_until, and always keep the job timeout below the lease. Count how often the reaper reclaims jobs from workers that are still alive: anything above 0 means a misconfiguration.
Giữ hết 300.000 job DONE mỗi ngày trong cùng bảng thì sau vài tháng câu SKIP LOCKED quét hàng chục triệu dòng chết và autovacuum chạy không kịp. Dùng partial index trên (priority, run_at) WHERE status='QUEUED', chuyển job DONE sang bảng lịch sử hoặc partition theo ngày rồi drop. Theo dõi thời gian của câu claim, cảnh báo khi p99 vượt 20 ms.Keeping all 300,000 daily DONE jobs in the same table means after a few months the SKIP LOCKED query scans tens of millions of dead rows and autovacuum falls behind. Use a partial index on (priority, run_at) WHERE status='QUEUED', and move DONE jobs to a history table or a daily partition you drop. Track the claim query's latency and alert when p99 passes 20 ms.
API hóa đơn điện tử bị bảo trì 10 phút, 20.000 job retry ngay lập tức và đối tác chặn IP vì vượt hạn mức. Hẹn lại theo exponential backoff có jitter (10 giây × 2^attempt, tối đa 1 giờ) và phân biệt lỗi tạm thời với lỗi dữ liệu như 400. Giới hạn số job đồng thời theo loại để một đối tác chậm không chiếm hết 48 luồng.The e-invoice API goes down for 10 minutes of maintenance, 20,000 jobs retry immediately, and the partner blocks the IP for exceeding its quota. Reschedule with jittered exponential backoff (10 seconds × 2^attempt, capped at an hour) and separate transient errors from data errors such as 400. Cap concurrent jobs per type so one slow partner cannot take all 48 slots.
Tạo đơn rồi mới enqueue job gửi hóa đơn ở lời gọi riêng: app crash giữa hai bước thì có đơn mà không bao giờ có hóa đơn, còn enqueue trước rồi rollback thì gửi hóa đơn cho đơn không tồn tại. Vì hàng đợi nằm trong PostgreSQL, INSERT job trong cùng transaction với đơn hàng. Nếu dùng broker ngoài, ghi qua outbox thay vì gửi thẳng.Creating the order and then enqueuing the invoice job in a separate call means a crash between them leaves an order that never gets an invoice, while enqueuing first and rolling back invoices an order that does not exist. Since the queue lives in PostgreSQL, INSERT the job in the same transaction as the order. With an external broker, go through an outbox instead of publishing directly.
Mỗi pattern mở bài học tương ứng trong menu Pattern.Each one opens its lesson in the Patterns menu.
Không có heartbeat thì reaper trả job về QUEUED sau 60 giây và worker khác chạy song song. Phải gia hạn lease định kỳ, và khi gia hạn thất bại thì huỷ handler.Without heartbeats the reaper requeues it after 60 seconds and another worker runs it in parallel. Extend the lease periodically and cancel the handler when an extension fails.
Worker cũ có thể bị dừng lâu (GC, mạng) rồi tỉnh lại sau khi job đã được trao cho worker khác. Điều kiện locked_by đóng vai fencing token, chặn ghi đè kết quả.A stale worker can pause (GC, network) and wake up after the job was handed to someone else. The locked_by condition acts as a fencing token and blocks the overwrite.
Mỗi instance INSERT vào cron_run(job_name, scheduled_for='02:00'); unique constraint để một instance thắng. Hoặc giữ pg_try_advisory_lock làm leader.Each instance inserts into cron_run(job_name, scheduled_for='02:00'); the unique constraint lets one instance win. Or hold pg_try_advisory_lock as the leader.
Job chủ yếu chờ I/O thì nhiều luồng hơn số CPU, nhưng tổng luồng trên mọi instance không được vượt connection pool của DB và giới hạn của API phía sau.I/O-bound jobs allow more slots than CPUs, but the total across instances must stay under the DB connection pool and the downstream API's limits.
Phím 1–4 để chọn đáp án, Enter để kiểm tra.Press 1–4 to choose an answer, Enter to check.
Ôn tập ngắt quãng (spaced repetition): thẻ được lặp lại sau 1 → 3 → 7 → 14 → 30 ngày để củng cố trí nhớ dài hạn.Spaced repetition: cards are repeated after 1 → 3 → 7 → 14 → 30 days to build long-term memory.