beyond crud: patterns that never forget

51
Beyond CRUD: patterns that never forget Diego Moreno

Upload: diego-moreno

Post on 14-Apr-2017

201 views

Category:

Software


1 download

TRANSCRIPT

Beyond CRUD:patterns that never forget

Diego Moreno

· R&D Software Manager @ Bizpills Group

· Now making a new platform

easy, fast, collaborative & scalable

@[email protected]

Rise your hand if you have

· heard about CQRS/Event Sourcing

Rise your hand if you have

· heard about CQRS/Event Sourcing

· used it in production

Rise your hand if you have

· heard about CQRS/Event Sourcing

· used it in production

· heard about Redux

Big data trees

Our need: Make fast & scalable

reads, writes and copies

Problem: read fast or write fast

but not both

Challenge

[Recurso gráfico: caja de herramientas semivacía]

Business logic

backend

frontend

TipicalWeb App

backend

frontend

CQRS:Command Query

Responsibility Segregation

Commandlogic

Querylogic

THINKOUTSIDETHEBOX

Read and write from same source?

THINKOUTSIDETHEBOX

backend

frontend

CQRS:Command Query

Responsibility Segregation

Commandlogic

Querylogic

backend

frontend

CQRS:Command Query

Responsibility Segregation

sync orasync

deriveddata

Commandlogic

Querylogic

CQRS: Derived data

◼ Application-level

▼ Race conditions

▼ Cold start

▼ Cache miss ratio

▼ Invalidation

Cache

CQRS: Derived data

◼ Application-level

▼ Race conditions

▼ Cold start

▼ Cache miss ratio

▼ Invalidation

◼ Database-level

▲ Autosync

▼ Additional load on database

▼ Limited to SQL logic

▼ Code testing/maintenance

Cache Materialized Views

backend

frontend

CQRS:Command Query

Responsibility Segregation

sync orasync

deriveddata

Commandlogic

Querylogic

backend

views

frontend

view generator

CQRS

Commandlogic

Querylogic

CQRS: Pros & Cons

▲ Separation of concerns

▲ Optimize reads & writes

▲ No cold cache

▲ No race conditions

▲ Agile development

▲ Scale reads & writesindependently

CQRS: Pros & Cons

▲ Separation of concerns

▲ Optimize reads & writes

▲ No cold cache

▲ No race conditions

▲ Agile development

▲ Scale reads & writesindependently

▼ Complexity

▼ UX impact

Read and write from same source?

THINKOUTSIDETHEBOX

Read and write from same source?

Data must be mutable?

THINKOUTSIDETHEBOX

A

1 32

node parent

1 A

2 A

Event Sourcing

Don’t save current state

Save the events that lead to current state

A

1 32

node parent

1 A

2 A

event node parent

ADD_CHILD 1 A

ADD_CHILD 2 A

ADD_CHILD 3 A

REMOVE_CHILD 3 A

Event Sourcing

CRUDCreate Read Update Delete

CRUDCreate Read Update Delete

ES: Pros & Cons

▲ Fast-write

▲ Deal with immutable data

▲ Easy audit

▲ Recovery from human error

▲ Users can undo actions

▲ Better analytics

ES: Pros & Cons

▲ Fast-write

▲ Deal with immutable data

▲ Easy audit

▲ Recovery from human error

▲ Users can undo actions

▲ Better analytics

▼ Complexity

▼ Event backward compatibility

▼ No current state

▼ Slow-read

backend

views

frontend

view generator

CQRS

Commandlogic

Querylogic

backend

views

frontend

view generator

a

a

ES + CQRS

action log

a

Commandlogic

Querylogic

Redux

REDUCERS STATE

actions subscribe

change State container written in Javascript

UI dispatch actions

Only reducers change state

backend

views

frontend

view generator

a

a

ES + CQRS

action log

a

Commandlogic

Querylogic

backend

views

frontend

view generator

a

Redux + ES + CQRS

action log

a

Commandlogic

Querylogic

Re

Sta

Commandlogic

Querylogic

Re

St

Re St

backend

action log

views

frontend

a

a

a

view generator

Redux + ES + CQRS

Redux: Pros & Cons

▲ Predictable state changes

▲ Optimistic UI

▲ Desktop app performance

▲ “undo” UI

Redux: Pros & Cons

▲ Predictable state changes

▲ Optimistic UI

▲ Desktop app performance

▲ “undo” UI

▼ Learning curve

Commandlogic

Querylogic

Re

St

Re St

backend

action log

views

frontend

a

a

a

view generator

Redux + ES + CQRS

Commandlogic

Querylogic

Re

St

Re St

backend

action log

views

frontend

a

a

a

a

view generator

Redux + ES + CQRSreal time collaboration

web socket

Real life

backend

views

frontend

view generatoraction log

Commandlogic

Querylogic

backend

views

frontend

view generatoraction log

Commandlogic

Querylogic

Alternative: Apache Samza

backend

frontend

view generatoraction log

Commandlogic

Querylogic

views

backend

frontend

view generatoraction log

Commandlogic

Querylogic

views

backend

views

frontend

view generatoraction log

Commandlogic

Querylogic

Alternative: Apache Kafka

CQRS + ES + Redux

▲ Fast

▲ Collaborative

▲ Scalable

▼ Complexity

“a great power comes a great

responsibility”

“a great power comes a great

responsibility”

[Recurso gráfico: caja de herramientas repleta de cosas o

banco de bricolaje lleno de herramientas]

Want to join our team?PHP/NodeJS/Cloud

[email protected]

Thanks

[email protected]@diegmoren

?? || /**/

ReferencesMartin Kleppmann @ Strange Loop 2014: Turning the database inside-out with Apache Samzahttps://www.confluent.io/blog/turning-the-database-inside-out-with-apache-samza/

Kevin Robinson @ React Europe 2015: Simplifying the data layer at react-europe 2015https://www.youtube.com/watch?v=EOz4D_714R8

Martin Fowler: CQRShttp://martinfowler.com/bliki/CQRS.htm