queueable-vs-batch
Skill1 starsChanged 7 months agoMITView on GitHub
Decide between Queueable Apex, Batch Apex, Schedulable, and `@future` for an async piece of work. Asks 5–6 quick questions, then recommends with rationale.
In this repository53 other files it gives its agents.
- templates/CLAUDE.mdCLAUDE.md
- adrSkill
- agent-deploySkill
- agent-discoverSkill
- agent-eval-trendSkill
- agent-exchange-listSkill
- agent-specSkill
- agent-testSkill
- agent-vs-flow-vs-apexSkill
- before-vs-after-triggerSkill
- code-reviewSkill
- complexitySkill
- coverage-trendSkill
- dead-codeSkill
- dependency-graphSkill
- deploySkill
- destructive-changesSkill
- diff-deploySkill
- erdSkill
- field-impactSkill
- flaky-test-finderSkill
- flow-auditSkill
- flow-vs-apexSkill
- fls-auditSkill
- gateway-configSkill
- generate-docsSkill
- limit-usageSkill
- lwc-vs-reactSkill
- mcp-tool-vs-restSkill
- notifySkill
- onboardSkill
- org-diffSkill
- org-exploreSkill
- package-versionSkill
- pattern-packSkill
- perf-reviewSkill
- permset-auditSkill
- pr-prepareSkill
- quick-deploySkill
- react-initSkill
- release-notesSkill
- scratch-orgSkill
- security-scanSkill
- sequence-diagramSkill
- sf-initSkill
- sharing-reviewSkill
- slack-agentSkill
- soql-analyzerSkill
- test-coverageSkill
- test-dataSkill
- test-planSkill
- trust-evalSkill
- trust-layer-auditSkill
The file
Files
You are picking the right async mechanism. Salesforce offers four+ kinds; each has different governor limits, scheduling semantics, and chaining rules.
Decision questions
- How many records is the unit of work? under 50, 50–10K, 10K–50M, > 50M?
- Is it triggered by a timer / cron, by a record event, or by user action?
- Does it need to chain into another async job afterward?
- Does it call external systems (callouts)?
- Does it need transaction integrity across the whole job, or per-chunk?
- Does it need to run on a schedule (every hour, every night)?
Decision tree
< 50 records, fire-and-forget, no chaining, no state
→ @future — simplest; one method invocation per call; deprecated for new work but still valid
< 50 records, may need to chain, keep state across runs, callout
→ Queueable (with Database.AllowsCallouts if calling out)
50 – 10K records, chunk-able, runs in single transaction per chunk
→ Database.Batchable — one start/execute/finish; chunk size up to 2000
10K – 50M records, very large or needs scheduling
→ Database.Batchable + Schedulable wrapper; consider chunking by date/owner key
External event consumer, throughput-oriented
→ Platform Event subscriber (see pattern-pack add platform-events) — not Batch
User clicks a button → "Do work in background"
→ Queueable enqueued from the LWC's Apex controller; static guard to prevent duplicate enqueues (PRJ-6)
Periodic ETL / nightly recompute
→ Database.Batchable invoked from a Schedulable (System.schedule)Limits matrix (single-record async)
| Mechanism | SOQL | DML | Heap | CPU | Callouts | Concurrency |
@future | 200 | 150 | 12 MB | 60s | 10 | 50/hr |
Queueable | 200 | 150 | 12 MB | 60s | 10 | 50 enqueues/tx; chain unlimited |
Database.Batchable (per execute) | 200 | 150 | 12 MB | 60s | 10 | scope ≤ 2000 records |
Schedulable | depends — usually wraps Batch | — | — | — | — | scheduled jobs limited org-wide |
Output
# Async Choice
## Description
Recompute customer-rank rolls-up across all Accounts nightly (~250K Account rows)
## Answers
- Volume: 250K records
- Trigger: scheduled (nightly)
- Chains: no
- Callouts: no
- Transaction: per-chunk OK
- Schedule: every night at 02:00
## Recommendation: **Database.Batchable + Schedulable wrapper**
### Why
- 250K rows — far above Queueable's 12 MB heap if loaded in one shot. Batch chunks (default 200, configurable up to 2000) keep heap manageable
- Per-execute transaction is fine — failure of one chunk doesn't void earlier chunks
- Native scheduling via `System.schedule(name, '0 0 2 * * ?', new Schedulable_Wrapper())`
- No callouts → no need for `Database.AllowsCallouts`
### Implementation outline
- `AccountRankBatch implements Database.Batchable<sObject>`
- `start(BatchableContext)` returns the iterator (typically `Database.QueryLocator` for SOQL-defined scope)
- `execute(BatchableContext, List<sObject> scope)` does the per-chunk work
- `finish(BatchableContext)` logs summary and optionally chains to next job
- `AccountRankSchedulable implements Schedulable`
- `execute(SchedulableContext)` calls `Database.executeBatch(new AccountRankBatch(), 200);`
- Schedule once via Setup or anonymous Apex:
```apex
System.schedule('Nightly Account Rank', '0 0 2 * * ?', new AccountRankSchedulable());Patterns referenced
- SF-7 (Trigger Handler Framework — not relevant here, but consider if you also have a record-triggered path)
- PRJ-6 (Queueable with Static Guard — not relevant for Batch but mentioned for completeness)
Alternatives considered
- Queueable chain: would work but burns 50 enqueues per cycle and the per-job 12 MB heap forces tiny chunks; Batch is cleaner
@future: doesn't chain, no scope, no progress tracking; not appropriate for 250K rows
## Rules
- **Don't recommend `@future` for new work.** It's still supported but lacks chaining, state, and observability. Queueable is the modern equivalent
- **Batch's `start` query result counts toward the 50 million row limit.** For >50M rows, partition by date/owner/region and use a `Database.QueryLocator` per slice
- **If callouts are needed, enable them.** `Queueable + implements Database.AllowsCallouts` or `Batchable + implements Database.AllowsCallouts`
- **Static guards (PRJ-6) prevent duplicate enqueues from triggers.** Always recommend when the queueable is invoked from a record triggerDoes it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

