liveBook · Manning

Upsert

As explained in Microservices Patterns, Second Edition

A database operation that inserts a record if it is absent and updates it otherwise.

An upsert inserts a record when it is absent and updates it when present. In a CQRS view repository, create and update methods commonly use upserts because related events may arrive out of order: a create handler can find a record already created by another aggregate, while an update handler may run before the record exists. Upserts address uncertain record existence; methods still need to handle concurrent and duplicate events correctly. section 13.3.3

Out-of-order events

A create event handler may find that a related aggregate’s event already created the record. Using an upsert lets the handler update that existing record instead of assuming it is absent. section 13.3.3

How Microservices Patterns, Second Edition covers it

How deep it goes
Defines insert-if-absent and update-if-present behavior, and motivates it with out-of-order events; leaves concurrency and duplicate handling to further processing.
Examples
Uses create and update handlers encountering records already present or not yet created because related events arrive out of order.
What you can do after
Apply upserts in create and update methods to insert absent records or update existing ones.
How it connects
Connects CQRS view repositories with event handlers and out-of-order event delivery.

Also explained in

Compare how 4 books explain upsert →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.