Top Banner
CQRS : Introduction Shah Ali Newaj Topu, CTO, Selise
43

CQRS : Introduction

Jun 14, 2015

Download

Technology

Selise Tech talk presentation 2014
SELISE rockin’ software is going to organize the first of its kind TechTalk in Dhaka. The event features activities for both professional software developers and fresh graduates.

The morning session holds a recruitment program only for fresh CSE graduates which includes a coding competition.

In the evening, software professionals from around the country are welcome to participate in three Keynotes, held by leading Tech experts on state of the art technologies.
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: CQRS : Introduction

CQRS : IntroductionShah Ali Newaj Topu, CTO, Selise

Page 2: CQRS : Introduction

How do we architect enterprise application

Application Layer (Client)

Service Layer

Business Logic Layer

Data Access Layer

Security

Logging

Relational Database

Page 3: CQRS : Introduction

A typical request LifeCycle

newContactID=ContactService.AddContact(Contact)

return ContactManager.AddContact(Contact);

If(Validate(Contact))return ContactRepository.Add(Contact)

Insert into… return newContactID

Relational Database

Page 4: CQRS : Introduction

What happens on UI

Page 5: CQRS : Introduction

What happens on UI

Page 6: CQRS : Introduction

and we wait

Page 7: CQRS : Introduction

and taken back to the grid

Page 8: CQRS : Introduction

Why we wait?

Because it takes some time to add the information to the Database and return us the newly created ID.

Page 9: CQRS : Introduction

Why database takes time?

● More write operation might be in progress.● Lot of read operation might be in progress.● Some request itself might take longer than

others because it has to update multiple tables.

● And so on.

Page 10: CQRS : Introduction

What happens when we read data

contactList=ContactService.GetContacts()

return ContactManager.GetContact();

return ContactRepository.Get()

Select from ...join…join return contactList

Relational Database

Page 11: CQRS : Introduction

and we wait

Page 12: CQRS : Introduction

and ta da..

Page 13: CQRS : Introduction

We have 2 wait screens

Can we get rid of them?

Page 14: CQRS : Introduction

Why we wait?

● Lots of Joins in Select query● There was some write request from other

users which locked the table we were trying to read.

Page 15: CQRS : Introduction

Why we have Joins

Because we have Normalized Database

Page 16: CQRS : Introduction

And we need Normalized database because

Page 17: CQRS : Introduction

Normalization slows down read operation

The best READ performance can be achieved from a Denormalized Database where data is duplicated and found in columns rather than rows.

Page 18: CQRS : Introduction

Can we have best of both

Imaging you have one service that does all the WRITE operation and updates your Normalized Database and ensures data integrity.Then you have one service that reads the data from a Denormalized Database.And somehow these two Databases are synchronized.

Page 19: CQRS : Introduction

Separation of Read and WritecontactList=ContactREADService.

GetContacts()

return ContactManager.GetContact();

return ContactRepository.Get()

Select * from Contact return contactList

Denormalized Database

newContactID=ContactWRITEService.AddContact(Contact)

return ContactManager.AddContact(Contact);

If(Validate(Contact))return ContactRepository.Add(Contact)

Insert into… return newContactID

Normalized DatabaseSync

Page 20: CQRS : Introduction

We can deploy them separately

More Read Service Less Write Service

Page 21: CQRS : Introduction

But this just a small improvement

We still need the loading screen because Sync will take time and if we make a call to the read service before the sync is done we will not get the data we are looking for.

Page 22: CQRS : Introduction

But lets look back

Page 23: CQRS : Introduction

We have all the data on the ClientWe refresh our grid to show the newly added row by doing a query to the read service.Why?We have all the data in the client because user just now entered it, remember? In the add contact screen. Why we need to query to the read service for data which we already have?

Page 24: CQRS : Introduction

So..

Can we add the row to the grid without making a call to the Read Service?

Page 25: CQRS : Introduction

But we still have to wait for Write

Remember our Write Operation returns an ID. We need this ID to uniquely identify the Data we are writing.What if the Write takes long? We will need a wait for that anyway!

Page 26: CQRS : Introduction

Redefining ID

What if we stop asking the Database to create this id and send it back?What if we generate the ID at the client?What if the ID is a GUID?

Page 27: CQRS : Introduction

Redefining IDContactWRITEService.AddContact

(Contact)

ContactManager.AddContact(Contact);

If(Validate(Contact))ContactRepository.Add(Contact)

Insert into…

Normalized Database

Page 28: CQRS : Introduction

Client becomes independent

For each data change client can now make a call to the write service and without waiting for return value it go back to do the next operation.

Write service can take its time to perform the operation and update the read database accordingly.

Page 29: CQRS : Introduction

Read and Write are independent

Read services only reads dataWrite services only changes the data.

Page 30: CQRS : Introduction

Command Query Responsibility Segregation.

● A Pattern○ Not an Architecture○ Not an Architectural style○ Not a principal

● Based on Command Query Separation Principle

Udi Dahan Greg Young

Page 31: CQRS : Introduction

CQS PrincipalCommand–query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.

It states that a method should either change the state of an object or return a result but not both.

Page 32: CQRS : Introduction

The Reactive Manifesto

Systems built as Reactive Systems are more flexible, loosely-coupled and scalable. This makes them easier to develop and amenable to change. They are significantly more tolerant of failure and when failure does occur they meet it with elegance rather than disaster. Reactive Systems are highly responsive, giving users effective interactive feedback.

Page 33: CQRS : Introduction

Reactive Systems are

● Responsive● Resilient● Elastic● Message Driven

Page 34: CQRS : Introduction

Necessary patterns

CQRSEvent SourcingDomain Driven DesignEvent Driven Architecture

Page 35: CQRS : Introduction

How does it look

Page 36: CQRS : Introduction

Who is doing it?MicrosoftIn July 2012 Ms started a project called CQRS Journey. The project is focused on building highly scalable, highly available and maintainable applications with the Command & Query Responsibility Segregation and the Event Sourcing patterns. Now they published a book and a fully documented source for implementing CQRS.

Page 37: CQRS : Introduction

Who is doing it?

Java:● AxonFramework● Jdon FrameworkScala:● eventsourced

Page 38: CQRS : Introduction

Who is doing it?

Selise:We are building Publiweb, a massive multi module, multi tenant , SaaS based highly scalable application using CQRS, Event Sourcing and Domain driven design for Swiss E-Gov.

Page 39: CQRS : Introduction

What no one told me

When you are asked to develop a scalable web application you are indeed asked to build a distributed system.

Page 40: CQRS : Introduction

What is a Distributed System

Write Read

Page 41: CQRS : Introduction

What that has to do with Web App

Page 42: CQRS : Introduction

Fallacies of Distributed Computing

● The network is reliable.● Latency is zero.● Bandwidth is infinite.● The network is secure.● Topology doesn't change.● There is one administrator.● Transport cost is zero.● The network is homogeneous.

Page 43: CQRS : Introduction