Serverless CQRS in Azure! Toon Vanhoutte
Nice to meet you Toon Vanhoutte Lead Architect @ Codit Belgium Microsoft Azure MVP @toonvanhoutte toonvanhoutte.wordpress.com www.linkedin.com/in/toon-vanhoutte
159 Who we are worldwide 2000 Belgium 2004 France 2013 Portugal 2016 Switzerland 2016 UK 2016 The Netherlands 2017 Malta 159 worldwide Customers Entities
The thing we do
Agenda An Introduction to CQRS Serverless Technologies in Azure Use Case: Optimize a Webshop Conclusion
An introduction to CQRS
Proven and simple design CRUD Architecture Proven and simple design
Command Query Responsibility Segregation Dealing with complex domain models From a DDD perspective, where the business complexity requires two separate models for read and write models. Eg one database with write against database tables and reads done based on views that are optimized for the UI
Command Query Responsibility Segregation Dealing with high performing applications Improve performance, scalability and flexibility
Commands vs Queries Commands Queries Write Read Sync / Async Sync Reliable Volatile Persistent In Memory Transactional Non Transactional Improve performance, scalability and flexibility Fast Fast
Different aspects of CQRS Multiple points of view Event Sourcing (store ordered historic events) DDD (different models) Scalability & Performance (different stores) Improve performance, scalability and flexibility
Highly performing and scalable solutions Scalability & Performance Focus of this session Highly performing and scalable solutions Event Sourcing (store ordered historic events) DDD (different models) Scalability & Performance (different stores) Improve performance, scalability and flexibility
Serverless in Azure
Pizza as a Service
Serverless Technology Options Azure Functions “Code triggered by events” Azure Logic Apps “Workflows triggered by events” Azure Service Bus “Reliable messaging platform” Azure Event Grid “Serverless event routing service”
Azure Functions Azure Logic Apps Developer experience Connectivity Exception Handling State Networking Deployment Runtime Monitoring Pricing Model Security
Azure Functions Azure Logic Apps Preferred option for complex synchronous request/response calls. Preferred option for asynchronous fire-and-forget messaging. Azure Functions perfectly extends Azure Logic Apps.
Azure Service Bus Azure Event Grid Report Command Notification Transfer Query Measurement Assignment Job Request Trace Update Handover © Clemens Vasters
Messaging Eventing Azure Service Bus Azure Event Grid Passing Intents Reporting Facts Command Discrete Transfer Report Query Notification Messaging Eventing Handover Job Measurement Assignment Update Trace Request Series © Clemens Vasters
Messaging Eventing Azure Service Bus Azure Event Grid Passing Intents Reporting Facts Discrete “PriceUpdated” “OpportunityWon” “InvoiceBooked” “CreateOrder” “BookHotel” “UpdateStock” Messaging Eventing Temperature stream Telemetry data Audit trail Series © Clemens Vasters
Use Case
Find out how CQRS will save Antonio’s business! Pizza Antonio Find out how CQRS will save Antonio’s business!
Pizza Antonio Website € 10 € 12 € 11 Submit Query Command
Pizza Antonio Query SQL Database Command Very good pizza’s GetProducts Command CreateOrder Very good pizza’s Customers get errors when ordering pizza during peak hours
Pizza Antonio Query SQL Database Command GetProducts Command CreateOrder Customers don’t get any errors anymore The orders don’t make it to the database, so no pizza
Pizza Antonio Query SQL Database Command GetProducts Command CreateOrder All orders make it to the database Polling during quiet hours is rather expensive, so more expensive pizza’s
Pizza Antonio Query SQL Database Command GetProducts Command CreateOrder Cheaper polling mechanism, so again sharper prices No flexible way to send order confirmations through email
Pizza Antonio Query SQL Database Command GetProducts Command CreateOrder Flexible extension points for e.g. emails Two different URL’s and security keys for the website
Pizza Antonio Query SQL Database Command Uniform API for website Azure Function Proxy SQL Database GetProducts Command CreateOrder Uniform API for website Loading products on the website is extremely slow during peak hours
Pizza Antonio Query SQL Database Command Azure Function Proxy SQL Database GetProducts Command CreateOrder Fast responsive times on the website No flexible way to update products on external food delivery services
Pizza Antonio Query SQL Database Command Azure Function Proxy GetProducts Command CreateOrder
Pizza Antonio Query SQL Database Command Antonio happy Customers happy Azure Function Proxy SQL Database GetProducts Command CreateOrder Antonio happy Customers happy
Conclusion
CQRS is a proven design pattern! It exists in many different variations Azure Function Proxy SQL Database Query Command Sentinet SQL Database Query Command Azure API Mgmt SQL Database Query Command Cache
Lessons learned Azure Event Grid Azure Functions Consumption plan results in cold starts, also for function proxies! Solution: Keep it warm (5 min) App Service Plan Pre-compiled functions Event Grid puts responsibility for sequencing at receive side! Solution: Use it wisely Consider Azure Service Bus if sequencing is required
Architectural Advise on CQRS Do not overengineer! Advantages Challenges Disadvantages Performance Idempotence Complexity Scalability Eventual Consistency Monitoring Flexibility Resiliency Maintenance Apply CQRS carefully on specific parts of the solution!
Thank you! Keep in touch. @toonvanhoutte toonvanhoutte.wordpress.com www.linkedin.com/in/toon-vanhoutte