[{"data":1,"prerenderedAt":936},["ShallowReactive",2],{"$fqPFjUnd_a6b7S01JMU9ZzVCot3LIG_32_YDOXK82PDU":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":920,"editUrl":929,"prev":930,"next":933},"techniques\u002Fcqrs","CQRS","Split commands and queries with CommandBus, Inbox\u002FOutbox, and QueryDB helpers.","---\ntitle: CQRS\ndescription: Split commands and queries with CommandBus, Inbox\u002FOutbox, and QueryDB helpers.\n---\n\n# CQRS\n\n`ferrite-cqrs` provides a command\u002Fevent split for the write side plus an outbox mechanism for durably emitting domain events.\n\n## Enable it\n\n```cargo\nfr add cqrs\n```\n\n```rust\n#[module(imports = [CqrsModule])]\npub struct AppModule;\n```\n\n## Commands\n\nCommands are plain serializable types:\n\n```rust\n#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]\npub struct CreateNote {\n    pub author_id: i64,\n    pub body: String,\n}\n\n#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]\npub struct NoteCreated {\n    pub note_id: i64,\n    pub author_id: i64,\n}\n```\n\n## CommandBus\n\n`CommandBus` dispatches a command and returns the aggregate result type. Handlers are registered per command type:\n\n```rust\nuse ferrite_cqrs::{CommandBus, CommandHandler, register_command_handler};\n\npub struct CreateNoteHandler(Arc\u003CRepository\u003CNote>>);\n#[async_trait]\nimpl CommandHandler\u003CCreateNote, CreateNote> for CreateNoteHandler {\n    async fn handle(&self, cmd: CreateNote) -> Result\u003CCreateNote, CqrsError> {\n        \u002F\u002F returns a command-specific result\n        Ok(CreateNote { \u002F* ... *\u002F })\n    }\n}\nregister_command_handler!(CreateNoteHandler, CreateNote, CreateNote);\n```\n\nDispatch from any provider:\n\n```rust\n#[injectable]\npub struct NotesService {\n    bus: CommandBus,\n}\n\nimpl NotesService {\n    #[inject]\n    pub fn new(bus: CommandBus) -> Self {\n        Self { bus }\n    }\n\n    pub async fn create(&self, author_id: i64, body: String) -> Result\u003CCreateNote, CqrsError> {\n        self.bus.execute(CreateNote { author_id, body }).await\n    }\n}\n```\n\n## Inbox \u002F Outbox\n\nThe outbox pattern persists outgoing events so they can be delivered at-least-once:\n\n```rust\nuse ferrite_cqrs::Outbox;\n```\n\n| Method | Purpose |\n| --- | --- |\n| `publish(event)` | Append a serialized event to the outbox |\n| `dispatch()` | Drain published events through the message handlers |\n| `fetch_active()` | Read pending events |\n\nThe inbox side tracks processed events with an `Inbox` keyed by `(message_id, payload)` so replays are idempotent.\n\n## QueryDB\n\nFor the query side, `QueryDB` is a thin wrapper over a relaxed-access repository layer (in-memory by default) built from plain structs:\n\n```rust\nuse ferrite_cqrs::QueryDB;\n\n#[derive(Clone, serde::Serialize, serde::Deserialize)]\npub struct NoteView { pub id: i64, pub body: String }\n\ndb.upsert(NoteView { id, body });\nlet view = db.get::\u003CNoteView>(id);\nlet all = db.all::\u003CNoteView>();\n```\n\nSwap the storage engine by replacing the store in the container for read models.\n\n## How they fit\n\n```text\nController ──CommandBus──▶ Handler(lookup\u002Fstore) ──► Outbox.publish(NoteCreated)\n                                                        │\nOver night scans\u002Fother loops ──Outbox.dispatch──▶ event handlers\n```\n\nThe write path is authoritative; the outbox makes events durable; QueryDB keeps reads cheap.\n\n## Next steps\n\n- [Messaging](\u002Fdocs\u002Ftechniques\u002Fmessaging) once events cross service boundaries.\n- [Database (ORM)](\u002Fdocs\u002Ftechniques\u002Fdatabase).",{"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},"commands","Commands",{"id":23,"depth":13,"text":24},"commandbus","CommandBus",{"id":26,"depth":13,"text":27},"inbox-outbox","Inbox \u002F Outbox",{"id":29,"depth":13,"text":30},"querydb","QueryDB",{"id":32,"depth":13,"text":33},"how-they-fit","How they fit",{"id":35,"depth":13,"text":36},"next-steps","Next steps",[38,42,50,53,62,107,109,112,263,265,270,462,465,637,639,642,659,709,720,722,728,871,874,876,897,900,902],[39,40,5],"h1",{"id":41},"cqrs",[43,44,45,49],"p",{},[46,47,48],"code",{},"ferrite-cqrs"," provides a command\u002Fevent split for the write side plus an outbox mechanism for durably emitting domain events.",[51,52,18],"h2",{"id":17},[54,55,58],"pre",{"language":56,"class":57},"cargo","shiki dark:github-dark",[46,59,61],{"class":60},"language-cargo","fr add cqrs",[54,63,66],{"language":64,"class":65},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[46,67,69,92,93],{"class":68},"language-rust",[70,71,74,78,82,85,89],"span",{"class":72,"style":73},"line","display: inline",[70,75,77],{"style":76},"color:#24292E;--shiki-dark:#E1E4E8","#[module(imports ",[70,79,81],{"style":80},"color:#D73A49;--shiki-dark:#F97583","=",[70,83,84],{"style":76}," [",[70,86,88],{"style":87},"color:#6F42C1;--shiki-dark:#B392F0","CqrsModule",[70,90,91],{"style":76},"])]","\n",[70,94,95,98,101,104],{"class":72,"style":73},[70,96,97],{"style":80},"pub",[70,99,100],{"style":80}," struct",[70,102,103],{"style":87}," AppModule",[70,105,106],{"style":76},";",[51,108,21],{"id":20},[43,110,111],{},"Commands are plain serializable types:",[54,113,114],{"language":64,"class":65},[46,115,116,92,149,92,161,92,178,92,192,92,197,92,199,92,223,92,234,92,247,92,259],{"class":68},[70,117,118,121,124,127,130,133,136,139,141,143,146],{"class":72,"style":73},[70,119,120],{"style":76},"#[derive(",[70,122,123],{"style":87},"Debug",[70,125,126],{"style":76},", ",[70,128,129],{"style":87},"Clone",[70,131,132],{"style":76},", serde",[70,134,135],{"style":80},"::",[70,137,138],{"style":87},"Serialize",[70,140,132],{"style":76},[70,142,135],{"style":80},[70,144,145],{"style":87},"Deserialize",[70,147,148],{"style":76},")]",[70,150,151,153,155,158],{"class":72,"style":73},[70,152,97],{"style":80},[70,154,100],{"style":80},[70,156,157],{"style":87}," CreateNote",[70,159,160],{"style":76}," {",[70,162,163,166,169,172,175],{"class":72,"style":73},[70,164,165],{"style":80},"    pub",[70,167,168],{"style":76}," author_id",[70,170,171],{"style":80},":",[70,173,174],{"style":87}," i64",[70,176,177],{"style":76},",",[70,179,180,182,185,187,190],{"class":72,"style":73},[70,181,165],{"style":80},[70,183,184],{"style":76}," body",[70,186,171],{"style":80},[70,188,189],{"style":87}," String",[70,191,177],{"style":76},[70,193,194],{"class":72,"style":73},[70,195,196],{"style":76},"}",[70,198],{"class":72,"style":73},[70,200,201,203,205,207,209,211,213,215,217,219,221],{"class":72,"style":73},[70,202,120],{"style":76},[70,204,123],{"style":87},[70,206,126],{"style":76},[70,208,129],{"style":87},[70,210,132],{"style":76},[70,212,135],{"style":80},[70,214,138],{"style":87},[70,216,132],{"style":76},[70,218,135],{"style":80},[70,220,145],{"style":87},[70,222,148],{"style":76},[70,224,225,227,229,232],{"class":72,"style":73},[70,226,97],{"style":80},[70,228,100],{"style":80},[70,230,231],{"style":87}," NoteCreated",[70,233,160],{"style":76},[70,235,236,238,241,243,245],{"class":72,"style":73},[70,237,165],{"style":80},[70,239,240],{"style":76}," note_id",[70,242,171],{"style":80},[70,244,174],{"style":87},[70,246,177],{"style":76},[70,248,249,251,253,255,257],{"class":72,"style":73},[70,250,165],{"style":80},[70,252,168],{"style":76},[70,254,171],{"style":80},[70,256,174],{"style":87},[70,258,177],{"style":76},[70,260,261],{"class":72,"style":73},[70,262,196],{"style":76},[51,264,24],{"id":23},[43,266,267,269],{},[46,268,24],{}," dispatches a command and returns the aggregate result type. Handlers are registered per command type:",[54,271,272],{"language":64,"class":65},[46,273,274,92,297,92,299,92,328,92,333,92,360,92,408,92,414,92,432,92,437,92,441],{"class":68},[70,275,276,279,282,284,287,289,291,294],{"class":72,"style":73},[70,277,278],{"style":80},"use",[70,280,281],{"style":87}," ferrite_cqrs",[70,283,135],{"style":80},[70,285,286],{"style":76},"{",[70,288,24],{"style":87},[70,290,126],{"style":76},[70,292,293],{"style":87},"CommandHandler",[70,295,296],{"style":76},", register_command_handler};",[70,298],{"class":72,"style":73},[70,300,301,303,305,308,311,314,317,320,322,325],{"class":72,"style":73},[70,302,97],{"style":80},[70,304,100],{"style":80},[70,306,307],{"style":87}," CreateNoteHandler",[70,309,310],{"style":76},"(",[70,312,313],{"style":87},"Arc",[70,315,316],{"style":76},"\u003C",[70,318,319],{"style":87},"Repository",[70,321,316],{"style":76},[70,323,324],{"style":87},"Note",[70,326,327],{"style":76},">>);",[70,329,330],{"class":72,"style":73},[70,331,332],{"style":76},"#[async_trait]",[70,334,335,338,341,343,346,348,350,353,356,358],{"class":72,"style":73},[70,336,337],{"style":80},"impl",[70,339,340],{"style":87}," CommandHandler",[70,342,316],{"style":76},[70,344,345],{"style":87},"CreateNote",[70,347,126],{"style":76},[70,349,345],{"style":87},[70,351,352],{"style":76},"> ",[70,354,355],{"style":80},"for",[70,357,307],{"style":87},[70,359,160],{"style":76},[70,361,362,365,368,371,373,376,380,383,385,387,390,393,396,398,400,402,405],{"class":72,"style":73},[70,363,364],{"style":80},"    async",[70,366,367],{"style":80}," fn",[70,369,370],{"style":87}," handle",[70,372,310],{"style":76},[70,374,375],{"style":80},"&",[70,377,379],{"style":378},"color:#005CC5;--shiki-dark:#79B8FF","self",[70,381,382],{"style":76},", cmd",[70,384,171],{"style":80},[70,386,157],{"style":87},[70,388,389],{"style":76},") ",[70,391,392],{"style":80},"->",[70,394,395],{"style":87}," Result",[70,397,316],{"style":76},[70,399,345],{"style":87},[70,401,126],{"style":76},[70,403,404],{"style":87},"CqrsError",[70,406,407],{"style":76},"> {",[70,409,410],{"class":72,"style":73},[70,411,413],{"style":412},"color:#6A737D;--shiki-dark:#6A737D","        \u002F\u002F returns a command-specific result",[70,415,416,419,421,423,426,429],{"class":72,"style":73},[70,417,418],{"style":87},"        Ok",[70,420,310],{"style":76},[70,422,345],{"style":87},[70,424,425],{"style":76}," { ",[70,427,428],{"style":412},"\u002F* ... *\u002F",[70,430,431],{"style":76}," })",[70,433,434],{"class":72,"style":73},[70,435,436],{"style":76},"    }",[70,438,439],{"class":72,"style":73},[70,440,196],{"style":76},[70,442,443,446,448,451,453,455,457,459],{"class":72,"style":73},[70,444,445],{"style":87},"register_command_handler!",[70,447,310],{"style":76},[70,449,450],{"style":87},"CreateNoteHandler",[70,452,126],{"style":76},[70,454,345],{"style":87},[70,456,126],{"style":76},[70,458,345],{"style":87},[70,460,461],{"style":76},");",[43,463,464],{},"Dispatch from any provider:",[54,466,467],{"language":64,"class":65},[46,468,469,92,474,92,485,92,497,92,501,92,503,92,511,92,516,92,541,92,549,92,553,92,555,92,603,92,629,92,633],{"class":68},[70,470,471],{"class":72,"style":73},[70,472,473],{"style":76},"#[injectable]",[70,475,476,478,480,483],{"class":72,"style":73},[70,477,97],{"style":80},[70,479,100],{"style":80},[70,481,482],{"style":87}," NotesService",[70,484,160],{"style":76},[70,486,487,490,492,495],{"class":72,"style":73},[70,488,489],{"style":76},"    bus",[70,491,171],{"style":80},[70,493,494],{"style":87}," CommandBus",[70,496,177],{"style":76},[70,498,499],{"class":72,"style":73},[70,500,196],{"style":76},[70,502],{"class":72,"style":73},[70,504,505,507,509],{"class":72,"style":73},[70,506,337],{"style":80},[70,508,482],{"style":87},[70,510,160],{"style":76},[70,512,513],{"class":72,"style":73},[70,514,515],{"style":76},"    #[inject]",[70,517,518,520,522,525,528,530,532,534,536,539],{"class":72,"style":73},[70,519,165],{"style":80},[70,521,367],{"style":80},[70,523,524],{"style":87}," new",[70,526,527],{"style":76},"(bus",[70,529,171],{"style":80},[70,531,494],{"style":87},[70,533,389],{"style":76},[70,535,392],{"style":80},[70,537,538],{"style":378}," Self",[70,540,160],{"style":76},[70,542,543,546],{"class":72,"style":73},[70,544,545],{"style":378},"        Self",[70,547,548],{"style":76}," { bus }",[70,550,551],{"class":72,"style":73},[70,552,436],{"style":76},[70,554],{"class":72,"style":73},[70,556,557,559,562,564,567,569,571,573,576,578,580,583,585,587,589,591,593,595,597,599,601],{"class":72,"style":73},[70,558,165],{"style":80},[70,560,561],{"style":80}," async",[70,563,367],{"style":80},[70,565,566],{"style":87}," create",[70,568,310],{"style":76},[70,570,375],{"style":80},[70,572,379],{"style":378},[70,574,575],{"style":76},", author_id",[70,577,171],{"style":80},[70,579,174],{"style":87},[70,581,582],{"style":76},", body",[70,584,171],{"style":80},[70,586,189],{"style":87},[70,588,389],{"style":76},[70,590,392],{"style":80},[70,592,395],{"style":87},[70,594,316],{"style":76},[70,596,345],{"style":87},[70,598,126],{"style":76},[70,600,404],{"style":87},[70,602,407],{"style":76},[70,604,605,608,611,614,616,619,621,623,626],{"class":72,"style":73},[70,606,607],{"style":378},"        self",[70,609,610],{"style":80},".",[70,612,613],{"style":76},"bus",[70,615,610],{"style":80},[70,617,618],{"style":87},"execute",[70,620,310],{"style":76},[70,622,345],{"style":87},[70,624,625],{"style":76}," { author_id, body })",[70,627,628],{"style":80},".await",[70,630,631],{"class":72,"style":73},[70,632,436],{"style":76},[70,634,635],{"class":72,"style":73},[70,636,196],{"style":76},[51,638,27],{"id":26},[43,640,641],{},"The outbox pattern persists outgoing events so they can be delivered at-least-once:",[54,643,644],{"language":64,"class":65},[46,645,646],{"class":68},[70,647,648,650,652,654,657],{"class":72,"style":73},[70,649,278],{"style":80},[70,651,281],{"style":87},[70,653,135],{"style":80},[70,655,656],{"style":87},"Outbox",[70,658,106],{"style":76},[660,661,662,675],"table",{},[663,664,665],"thead",{},[666,667,668,672],"tr",{},[669,670,671],"th",{},"Method",[669,673,674],{},"Purpose",[676,677,678,689,699],"tbody",{},[666,679,680,686],{},[681,682,683],"td",{},[46,684,685],{},"publish(event)",[681,687,688],{},"Append a serialized event to the outbox",[666,690,691,696],{},[681,692,693],{},[46,694,695],{},"dispatch()",[681,697,698],{},"Drain published events through the message handlers",[666,700,701,706],{},[681,702,703],{},[46,704,705],{},"fetch_active()",[681,707,708],{},"Read pending events",[43,710,711,712,715,716,719],{},"The inbox side tracks processed events with an ",[46,713,714],{},"Inbox"," keyed by ",[46,717,718],{},"(message_id, payload)"," so replays are idempotent.",[51,721,30],{"id":29},[43,723,724,725,727],{},"For the query side, ",[46,726,30],{}," is a thin wrapper over a relaxed-access repository layer (in-memory by default) built from plain structs:",[54,729,730],{"language":64,"class":65},[46,731,732,92,744,92,746,92,766,92,799,92,801,92,819,92,846],{"class":68},[70,733,734,736,738,740,742],{"class":72,"style":73},[70,735,278],{"style":80},[70,737,281],{"style":87},[70,739,135],{"style":80},[70,741,30],{"style":87},[70,743,106],{"style":76},[70,745],{"class":72,"style":73},[70,747,748,750,752,754,756,758,760,762,764],{"class":72,"style":73},[70,749,120],{"style":76},[70,751,129],{"style":87},[70,753,132],{"style":76},[70,755,135],{"style":80},[70,757,138],{"style":87},[70,759,132],{"style":76},[70,761,135],{"style":80},[70,763,145],{"style":87},[70,765,148],{"style":76},[70,767,768,770,772,775,777,779,782,784,786,788,790,792,794,796],{"class":72,"style":73},[70,769,97],{"style":80},[70,771,100],{"style":80},[70,773,774],{"style":87}," NoteView",[70,776,425],{"style":76},[70,778,97],{"style":80},[70,780,781],{"style":76}," id",[70,783,171],{"style":80},[70,785,174],{"style":87},[70,787,126],{"style":76},[70,789,97],{"style":80},[70,791,184],{"style":76},[70,793,171],{"style":80},[70,795,189],{"style":87},[70,797,798],{"style":76}," }",[70,800],{"class":72,"style":73},[70,802,803,806,808,811,813,816],{"class":72,"style":73},[70,804,805],{"style":76},"db",[70,807,610],{"style":80},[70,809,810],{"style":87},"upsert",[70,812,310],{"style":76},[70,814,815],{"style":87},"NoteView",[70,817,818],{"style":76}," { id, body });",[70,820,821,824,827,829,832,834,837,839,841,843],{"class":72,"style":73},[70,822,823],{"style":80},"let",[70,825,826],{"style":76}," view ",[70,828,81],{"style":80},[70,830,831],{"style":76}," db",[70,833,610],{"style":80},[70,835,836],{"style":87},"get",[70,838,135],{"style":80},[70,840,316],{"style":76},[70,842,815],{"style":87},[70,844,845],{"style":76},">(id);",[70,847,848,850,853,855,857,859,862,864,866,868],{"class":72,"style":73},[70,849,823],{"style":80},[70,851,852],{"style":76}," all ",[70,854,81],{"style":80},[70,856,831],{"style":76},[70,858,610],{"style":80},[70,860,861],{"style":87},"all",[70,863,135],{"style":80},[70,865,316],{"style":76},[70,867,815],{"style":87},[70,869,870],{"style":76},">();",[43,872,873],{},"Swap the storage engine by replacing the store in the container for read models.",[51,875,33],{"id":32},[54,877,879],{"language":878,"class":65},"text",[46,880,882,92,887,92,892],{"class":881},"language-text",[70,883,884],{"class":72,"style":73},[70,885,886],{},"Controller ──CommandBus──▶ Handler(lookup\u002Fstore) ──► Outbox.publish(NoteCreated)",[70,888,889],{"class":72,"style":73},[70,890,891],{},"                                                        │",[70,893,894],{"class":72,"style":73},[70,895,896],{},"Over night scans\u002Fother loops ──Outbox.dispatch──▶ event handlers",[43,898,899],{},"The write path is authoritative; the outbox makes events durable; QueryDB keeps reads cheap.",[51,901,36],{"id":35},[903,904,905,914],"ul",{},[906,907,908,913],"li",{},[909,910,912],"a",{"href":911},"\u002Fdocs\u002Ftechniques\u002Fmessaging","Messaging"," once events cross service boundaries.",[906,915,916,610],{},[909,917,919],{"href":918},"\u002Fdocs\u002Ftechniques\u002Fdatabase","Database (ORM)",{"title":12,"searchDepth":13,"depth":14,"links":921},[922,923,924,925,926,927,928],{"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\u002Fcqrs.md",{"title":931,"slug":932},"Scheduling","techniques\u002Fscheduling",{"title":934,"slug":935},"i18n","techniques\u002Fi18n",1789565440153]