Top Banner
Optimizing the query stack of CQRS-layered applications Dino Esposito JetBrains dino.esposito@jetbrains.com @despos facebook.com/naa4e
22

Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Jun 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Optimizing the query stack of CQRS-layered applications

Dino EspositoJetBrains

[email protected]@despos

facebook.com/naa4e

Page 2: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Drink Eat

Page 3: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

QueryCommand

Page 4: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

CQRS ≈ Separating Stacks

Page 5: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Domain layer

Presentation layer

Application layer

Infrastructure layer

Canonical

Layered Architecture

Domain layer

Presentation layer

Application layer

Infrastructure layer

Commands Queries

Data

access

+

DTODomain layer Domain layer

CQRS

Layered Architecture

Page 6: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Conceptual model split into separate

models for update and display

Focus on tasks

Build the data model right for the task

Stop god-mode development

Stop wondering if your model is perfect

Page 7: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

CQRS

Architectural spinoffs

Page 8: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Presentation layer

VIEWSFORMS

Infrastructure layer

Page 9: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Domain layer

Presentation layer

Application layer

Infrastructure layer

Commands Queries

Data

access

+

DTO

CQRS

Layered Architecture

Infrastructure layer

Page 10: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Synchronous

Every command triggers sync

updates

Asynchronous

Every command triggers async

updates

Scheduled

A job runs periodically and

updates the read storage

On-demand

Updates triggered by

requests (if older enough)

Automatically

up-to-date

Eventually

up-to-date

Controlled

stalenessControlled

up-to-date

Page 11: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

CQRS for Plain CRUD Applications

Presentation

&

Application

Presentation

&

Application

YourApp.CommandStack YourApp.ReadStack

Page 12: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Use the code that does the job

Existing code

Existing products

Existing skills

COMMAND stack

Known patterns

Known techniques

Known practices

Page 13: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

O/RM of choice

LINQ

Database in use

READ stackUse the code that does the job

SQL queries

Data binding/grids

JSON / OData

Page 14: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

CQRS for Rich CRUD Applications

Presentation

&

Application

Presentation

&

Application

YourApp.CommandStack YourApp.ReadStack

Page 15: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

COMMAND STACK

Request for action

Log of actions

READ

DATA

STORE

Aggregate ID Data

Aggregate ID Data

Request for data Formatted data

DETAILED VIEW

Page 16: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Logging the list of commands makes any

CRUD an historical CRUD

Page 17: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Logging the list of

commands lets you

keep track of the

latest without

forgetting anything

that happened in

the past.

PS: if you really want to forget … go with plain CRUD.

Page 18: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

SELECT relevant business events

FROM System

WHERE TimeStamp BETWEEN Time1 AND Time2

SELECT relevant business events

FROM System

WHERE TimeStamp NEWER THAN Time1

SELECT relevant business events

FROM System

WHERE TimeStamp OLDER THAN Time1

REPLAY EVENTS

REBUILD THE STATECQRS leads to

Business Intelligence

Page 19: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Ubiquitous Language

When the language of the business is

fully replicated in the codebase

Page 20: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

Ubiquitous

Language Query stack

public class Database : IDisposable{

private readonly QueryDbContext _db = new QueryDbContext();

public IQueryable<Customer> Customers{

get { return _db.Customers; } }public void Dispose() {

_db.Dispose(); }

}

Page 21: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

DEMOWeb API service using OData and domain-friendly

extension methods for LINQ queries

Page 22: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications

FOLLOW

That’s All Folks!

facebook.com/naa4e

software2cents.wordpress.com

[email protected]

@despos