[{"data":1,"prerenderedAt":787},["ShallowReactive",2],{"$fJ2LkTSRR9ZASXbGO1rwTbitSy73Q2epd06pc4qWRNvw":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":772,"editUrl":781,"prev":782,"next":785},"techniques\u002Fbackground-jobs","Background jobs","Dispatch and process jobs with ferrite-queue, backed by memory or Redis.","---\ntitle: Background jobs\ndescription: Dispatch and process jobs with ferrite-queue, backed by memory or Redis.\n---\n\n# Background jobs\n\n`ferrite-queue` gives you a `Queue` abstraction with an in-memory and a Redis implementation, a typed `DispatcherService` to enqueue, and a `WorkerService` to process.\n\n## Enable it\n\n```cargo\nfr add queue\n```\n\n```rust\n#[module(imports = [QueueModuleImpl])]\npub struct AppModule;\n```\n\n## Job types\n\nA job is any serializable, cloneable type:\n\n```rust\npub trait Job: Serialize + for\u003C'de> Deserialize\u003C'de> + Clone + Send + Sync + 'static {}\n```\n\n```rust\n#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]\npub struct EmailJob {\n    pub to: String,\n    pub subject: String,\n    pub body: String,\n}\n```\n\n## Dispatch\n\n```rust\nuse ferrite_queue::{DispatcherService, JobEnvelope};\n\n#[injectable]\npub struct OrdersService {\n    dispatcher: DispatcherService\u003CMemoryQueue>,\n}\n\nimpl OrdersService {\n    #[inject]\n    pub fn new(dispatcher: DispatcherService\u003CMemoryQueue>) -> Self {\n        Self { dispatcher }\n    }\n\n    pub async fn send_receipt(&self, email: EmailJob) -> Result\u003C(), JobError> {\n        self.dispatcher.dispatch(email).await\n        \u002F\u002F or: dispatcher.dispatch_to(\"critical\", email)\n    }\n}\n```\n\n`JobEnvelope::new::\u003CJ>(&job, queue)` wraps jobs with their target queue when you need explicit routing.\n\n## Workers\n\n```rust\nuse ferrite_queue::{JobHandler, submit_job_handler};\n\npub struct EmailWorker;\n\n#[ferrite_framework::async_trait]\nimpl JobHandler\u003CEmailJob> for EmailWorker {\n    async fn handle(&self, job: EmailJob) -> Result\u003C(), JobError> {\n        send_email(&job).await?;\n        Ok(())\n    }\n}\n\nsubmit_job_handler!(EmailWorker);\n```\n\nThe worker registry maps `TypeId` of the job to its handler. `WorkerService` runs `run()`\u002F`tick()` to drain queues — `QueueModuleImpl` wires this on bootstrap.\n\n## Queues\n\n| Queue | Backing | Use case |\n| --- | --- | --- |\n| `MemoryQueue` | In-process VecDeque | Tests, single instance, dev |\n| `RedisQueue` | Redis list | Multi-instance, durable-ish |\n\n`DispatcherService::new::\u003CQ>(queue, default_queue)` binds a dispatcher to a specific queue implementation.\n\n## Full flow\n\n```text\nHTTP handler ──dispatch──▶ Queue ──► WorkerService ──► JobHandler\u003CEmailJob>\n```\n\nThe dispatcher serializes the job, enqueues it; the worker pops it, deserializes, and calls the matching handler.\n\n## Next steps\n\n- [Scheduling](\u002Fdocs\u002Ftechniques\u002Fscheduling).\n- [CQRS](\u002Fdocs\u002Ftechniques\u002Fcqrs).",{"frontmatter":9,"meta":10,"nodes":37},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28,31,34],{"id":17,"depth":13,"text":18},"enable-it","Enable it",{"id":20,"depth":13,"text":21},"job-types","Job types",{"id":23,"depth":13,"text":24},"dispatch","Dispatch",{"id":26,"depth":13,"text":27},"workers","Workers",{"id":29,"depth":13,"text":30},"queues","Queues",{"id":32,"depth":13,"text":33},"full-flow","Full flow",{"id":35,"depth":13,"text":36},"next-steps","Next steps",[38,42,62,65,74,119,121,124,195,290,292,504,510,512,661,682,684,731,737,739,750,753,755],[39,40,5],"h1",{"id":41},"background-jobs",[43,44,45,49,50,53,54,57,58,61],"p",{},[46,47,48],"code",{},"ferrite-queue"," gives you a ",[46,51,52],{},"Queue"," abstraction with an in-memory and a Redis implementation, a typed ",[46,55,56],{},"DispatcherService"," to enqueue, and a ",[46,59,60],{},"WorkerService"," to process.",[63,64,18],"h2",{"id":17},[66,67,70],"pre",{"language":68,"class":69},"cargo","shiki dark:github-dark",[46,71,73],{"class":72},"language-cargo","fr add queue",[66,75,78],{"language":76,"class":77},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[46,79,81,104,105],{"class":80},"language-rust",[82,83,86,90,94,97,101],"span",{"class":84,"style":85},"line","display: inline",[82,87,89],{"style":88},"color:#24292E;--shiki-dark:#E1E4E8","#[module(imports ",[82,91,93],{"style":92},"color:#D73A49;--shiki-dark:#F97583","=",[82,95,96],{"style":88}," [",[82,98,100],{"style":99},"color:#6F42C1;--shiki-dark:#B392F0","QueueModuleImpl",[82,102,103],{"style":88},"])]","\n",[82,106,107,110,113,116],{"class":84,"style":85},[82,108,109],{"style":92},"pub",[82,111,112],{"style":92}," struct",[82,114,115],{"style":99}," AppModule",[82,117,118],{"style":88},";",[63,120,21],{"id":20},[43,122,123],{},"A job is any serializable, cloneable type:",[66,125,126],{"language":76,"class":77},[46,127,128],{"class":80},[82,129,130,132,135,138,141,144,147,150,153,156,159,162,164,166,168,171,174,176,179,181,184,186,189,192],{"class":84,"style":85},[82,131,109],{"style":92},[82,133,134],{"style":92}," trait",[82,136,137],{"style":99}," Job",[82,139,140],{"style":92},":",[82,142,143],{"style":99}," Serialize",[82,145,146],{"style":92}," +",[82,148,149],{"style":92}," for",[82,151,152],{"style":88},"\u003C'",[82,154,155],{"style":99},"de",[82,157,158],{"style":88},"> ",[82,160,161],{"style":99},"Deserialize",[82,163,152],{"style":88},[82,165,155],{"style":99},[82,167,158],{"style":88},[82,169,170],{"style":92},"+",[82,172,173],{"style":99}," Clone",[82,175,146],{"style":92},[82,177,178],{"style":99}," Send",[82,180,146],{"style":92},[82,182,183],{"style":99}," Sync",[82,185,146],{"style":92},[82,187,188],{"style":88}," '",[82,190,191],{"style":99},"static",[82,193,194],{"style":88}," {}",[66,196,197],{"language":76,"class":77},[46,198,199,104,231,104,243,104,259,104,272,104,285],{"class":80},[82,200,201,204,207,210,213,216,219,222,224,226,228],{"class":84,"style":85},[82,202,203],{"style":88},"#[derive(",[82,205,206],{"style":99},"Debug",[82,208,209],{"style":88},", ",[82,211,212],{"style":99},"Clone",[82,214,215],{"style":88},", serde",[82,217,218],{"style":92},"::",[82,220,221],{"style":99},"Serialize",[82,223,215],{"style":88},[82,225,218],{"style":92},[82,227,161],{"style":99},[82,229,230],{"style":88},")]",[82,232,233,235,237,240],{"class":84,"style":85},[82,234,109],{"style":92},[82,236,112],{"style":92},[82,238,239],{"style":99}," EmailJob",[82,241,242],{"style":88}," {",[82,244,245,248,251,253,256],{"class":84,"style":85},[82,246,247],{"style":92},"    pub",[82,249,250],{"style":88}," to",[82,252,140],{"style":92},[82,254,255],{"style":99}," String",[82,257,258],{"style":88},",",[82,260,261,263,266,268,270],{"class":84,"style":85},[82,262,247],{"style":92},[82,264,265],{"style":88}," subject",[82,267,140],{"style":92},[82,269,255],{"style":99},[82,271,258],{"style":88},[82,273,274,276,279,281,283],{"class":84,"style":85},[82,275,247],{"style":92},[82,277,278],{"style":88}," body",[82,280,140],{"style":92},[82,282,255],{"style":99},[82,284,258],{"style":88},[82,286,287],{"class":84,"style":85},[82,288,289],{"style":88},"}",[63,291,24],{"id":23},[66,293,294],{"language":76,"class":77},[46,295,296,104,319,104,321,104,326,104,337,104,356,104,360,104,362,104,371,104,376,104,409,104,417,104,422,104,424,104,469,104,490,104,496,104,500],{"class":80},[82,297,298,301,304,306,309,311,313,316],{"class":84,"style":85},[82,299,300],{"style":92},"use",[82,302,303],{"style":99}," ferrite_queue",[82,305,218],{"style":92},[82,307,308],{"style":88},"{",[82,310,56],{"style":99},[82,312,209],{"style":88},[82,314,315],{"style":99},"JobEnvelope",[82,317,318],{"style":88},"};",[82,320],{"class":84,"style":85},[82,322,323],{"class":84,"style":85},[82,324,325],{"style":88},"#[injectable]",[82,327,328,330,332,335],{"class":84,"style":85},[82,329,109],{"style":92},[82,331,112],{"style":92},[82,333,334],{"style":99}," OrdersService",[82,336,242],{"style":88},[82,338,339,342,344,347,350,353],{"class":84,"style":85},[82,340,341],{"style":88},"    dispatcher",[82,343,140],{"style":92},[82,345,346],{"style":99}," DispatcherService",[82,348,349],{"style":88},"\u003C",[82,351,352],{"style":99},"MemoryQueue",[82,354,355],{"style":88},">,",[82,357,358],{"class":84,"style":85},[82,359,289],{"style":88},[82,361],{"class":84,"style":85},[82,363,364,367,369],{"class":84,"style":85},[82,365,366],{"style":92},"impl",[82,368,334],{"style":99},[82,370,242],{"style":88},[82,372,373],{"class":84,"style":85},[82,374,375],{"style":88},"    #[inject]",[82,377,378,380,383,386,389,391,393,395,397,400,403,407],{"class":84,"style":85},[82,379,247],{"style":92},[82,381,382],{"style":92}," fn",[82,384,385],{"style":99}," new",[82,387,388],{"style":88},"(dispatcher",[82,390,140],{"style":92},[82,392,346],{"style":99},[82,394,349],{"style":88},[82,396,352],{"style":99},[82,398,399],{"style":88},">) ",[82,401,402],{"style":92},"->",[82,404,406],{"style":405},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[82,408,242],{"style":88},[82,410,411,414],{"class":84,"style":85},[82,412,413],{"style":405},"        Self",[82,415,416],{"style":88}," { dispatcher }",[82,418,419],{"class":84,"style":85},[82,420,421],{"style":88},"    }",[82,423],{"class":84,"style":85},[82,425,426,428,431,433,436,439,442,445,448,450,452,455,457,460,463,466],{"class":84,"style":85},[82,427,247],{"style":92},[82,429,430],{"style":92}," async",[82,432,382],{"style":92},[82,434,435],{"style":99}," send_receipt",[82,437,438],{"style":88},"(",[82,440,441],{"style":92},"&",[82,443,444],{"style":405},"self",[82,446,447],{"style":88},", email",[82,449,140],{"style":92},[82,451,239],{"style":99},[82,453,454],{"style":88},") ",[82,456,402],{"style":92},[82,458,459],{"style":99}," Result",[82,461,462],{"style":88},"\u003C(), ",[82,464,465],{"style":99},"JobError",[82,467,468],{"style":88},"> {",[82,470,471,474,477,480,482,484,487],{"class":84,"style":85},[82,472,473],{"style":405},"        self",[82,475,476],{"style":92},".",[82,478,479],{"style":88},"dispatcher",[82,481,476],{"style":92},[82,483,23],{"style":99},[82,485,486],{"style":88},"(email)",[82,488,489],{"style":92},".await",[82,491,492],{"class":84,"style":85},[82,493,495],{"style":494},"color:#6A737D;--shiki-dark:#6A737D","        \u002F\u002F or: dispatcher.dispatch_to(\"critical\", email)",[82,497,498],{"class":84,"style":85},[82,499,421],{"style":88},[82,501,502],{"class":84,"style":85},[82,503,289],{"style":88},[43,505,506,509],{},[46,507,508],{},"JobEnvelope::new::\u003CJ>(&job, queue)"," wraps jobs with their target queue when you need explicit routing.",[63,511,27],{"id":26},[66,513,514],{"language":76,"class":77},[46,515,516,104,532,104,534,104,545,104,547,104,557,104,578,104,613,104,630,104,638,104,642,104,646,104,648],{"class":80},[82,517,518,520,522,524,526,529],{"class":84,"style":85},[82,519,300],{"style":92},[82,521,303],{"style":99},[82,523,218],{"style":92},[82,525,308],{"style":88},[82,527,528],{"style":99},"JobHandler",[82,530,531],{"style":88},", submit_job_handler};",[82,533],{"class":84,"style":85},[82,535,536,538,540,543],{"class":84,"style":85},[82,537,109],{"style":92},[82,539,112],{"style":92},[82,541,542],{"style":99}," EmailWorker",[82,544,118],{"style":88},[82,546],{"class":84,"style":85},[82,548,549,552,554],{"class":84,"style":85},[82,550,551],{"style":88},"#[ferrite_framework",[82,553,218],{"style":92},[82,555,556],{"style":88},"async_trait]",[82,558,559,561,564,566,569,571,574,576],{"class":84,"style":85},[82,560,366],{"style":92},[82,562,563],{"style":99}," JobHandler",[82,565,349],{"style":88},[82,567,568],{"style":99},"EmailJob",[82,570,158],{"style":88},[82,572,573],{"style":92},"for",[82,575,542],{"style":99},[82,577,242],{"style":88},[82,579,580,583,585,588,590,592,594,597,599,601,603,605,607,609,611],{"class":84,"style":85},[82,581,582],{"style":92},"    async",[82,584,382],{"style":92},[82,586,587],{"style":99}," handle",[82,589,438],{"style":88},[82,591,441],{"style":92},[82,593,444],{"style":405},[82,595,596],{"style":88},", job",[82,598,140],{"style":92},[82,600,239],{"style":99},[82,602,454],{"style":88},[82,604,402],{"style":92},[82,606,459],{"style":99},[82,608,462],{"style":88},[82,610,465],{"style":99},[82,612,468],{"style":88},[82,614,615,618,620,622,625,628],{"class":84,"style":85},[82,616,617],{"style":99},"        send_email",[82,619,438],{"style":88},[82,621,441],{"style":92},[82,623,624],{"style":88},"job)",[82,626,627],{"style":92},".await?",[82,629,118],{"style":88},[82,631,632,635],{"class":84,"style":85},[82,633,634],{"style":99},"        Ok",[82,636,637],{"style":88},"(())",[82,639,640],{"class":84,"style":85},[82,641,421],{"style":88},[82,643,644],{"class":84,"style":85},[82,645,289],{"style":88},[82,647],{"class":84,"style":85},[82,649,650,653,655,658],{"class":84,"style":85},[82,651,652],{"style":99},"submit_job_handler!",[82,654,438],{"style":88},[82,656,657],{"style":99},"EmailWorker",[82,659,660],{"style":88},");",[43,662,663,664,667,668,670,671,674,675,678,679,681],{},"The worker registry maps ",[46,665,666],{},"TypeId"," of the job to its handler. ",[46,669,60],{}," runs ",[46,672,673],{},"run()","\u002F",[46,676,677],{},"tick()"," to drain queues — ",[46,680,100],{}," wires this on bootstrap.",[63,683,30],{"id":29},[685,686,687,702],"table",{},[688,689,690],"thead",{},[691,692,693,696,699],"tr",{},[694,695,52],"th",{},[694,697,698],{},"Backing",[694,700,701],{},"Use case",[703,704,705,718],"tbody",{},[691,706,707,712,715],{},[708,709,710],"td",{},[46,711,352],{},[708,713,714],{},"In-process VecDeque",[708,716,717],{},"Tests, single instance, dev",[691,719,720,725,728],{},[708,721,722],{},[46,723,724],{},"RedisQueue",[708,726,727],{},"Redis list",[708,729,730],{},"Multi-instance, durable-ish",[43,732,733,736],{},[46,734,735],{},"DispatcherService::new::\u003CQ>(queue, default_queue)"," binds a dispatcher to a specific queue implementation.",[63,738,33],{"id":32},[66,740,742],{"language":741,"class":77},"text",[46,743,745],{"class":744},"language-text",[82,746,747],{"class":84,"style":85},[82,748,749],{},"HTTP handler ──dispatch──▶ Queue ──► WorkerService ──► JobHandler\u003CEmailJob>",[43,751,752],{},"The dispatcher serializes the job, enqueues it; the worker pops it, deserializes, and calls the matching handler.",[63,754,36],{"id":35},[756,757,758,766],"ul",{},[759,760,761,476],"li",{},[762,763,765],"a",{"href":764},"\u002Fdocs\u002Ftechniques\u002Fscheduling","Scheduling",[759,767,768,476],{},[762,769,771],{"href":770},"\u002Fdocs\u002Ftechniques\u002Fcqrs","CQRS",{"title":12,"searchDepth":13,"depth":14,"links":773},[774,775,776,777,778,779,780],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},{"id":32,"depth":13,"text":33},{"id":35,"depth":13,"text":36},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ftechniques\u002Fbackground-jobs.md",{"title":783,"slug":784},"Messaging (NATS & Kafka)","techniques\u002Fmessaging",{"title":765,"slug":786},"techniques\u002Fscheduling",1789565440150]