The ordering part covers many technical topics, including the CQRS pattern, MediatR for local command dispatch, local event handling, abstraction and implementation of distributed events, the outbox pattern, distributed locks, and more.

Ordering Service Architecture Diagram

Basic system architecture

Microservice Communication

Microservice communication

Client Side

The client side mainly covers order creation, payment, cancellation, and querying. The client calls the ordering microservice through the API Gateway; the ordering microservice processes commands via MediatR, then publishes events through the event bus; the event bus dispatches the events to event handlers, which process them and then persist the events to the database.

Ordering WebApi

WebApi serves as the entry point of the ordering microservice. It receives client requests, processes commands via MediatR, and then publishes events through the event bus.

Queries

Queries are handled by a read replica, which is an independent database dedicated to queries. Data in the read replica is synchronized from the primary database via streaming replication, is read-only, and is eventually consistent.

Commands

The command pattern is used to handle order creation, payment, cancellation, and other commands. The command pattern is a behavioral design pattern, and command data is ultimately persisted to the primary database.

Local Events

Local events are events internal to a microservice. They are published via MediatR and can only be used within the current microservice.

Distributed Events

Distributed events are events exchanged between microservices. They are published through the event bus and can be used across multiple microservices; publishing and handling are asynchronous. Dapr’s Pub/Sub component implements event publishing and subscription, and many kinds of message queues can be used underneath.

Using Distributed Locks

Distributed locks handle concurrency problems. They are implemented through a Dapr component. A distributed lock is a synchronization tool in a distributed system for solving concurrency issues; currently Dapr’s distributed lock is built on Redis.