Book Image

Practical Microservices with Dapr and .NET

By : Davide Bedin
Book Image

Practical Microservices with Dapr and .NET

By: Davide Bedin

Overview of this book

Over the last decade, there has been a huge shift from heavily coded monolithic applications to finer, self-contained microservices. Dapr is a new, open source project by Microsoft that provides proven techniques and best practices for developing modern applications. It offers platform-agnostic features for running your applications on public cloud, on-premises, and even on edge devices. This book will help you get to grips with microservice architectures and how to manage application complexities with Dapr in no time. You'll understand how Dapr offers ease of implementation while allowing you to work with multiple languages and platforms. You'll also understand how Dapr's runtime, services, building blocks, and software development kits (SDKs) help you to simplify the creation of resilient and portable microservices. Dapr provides an event-driven runtime that supports the essential features you need to build microservices, including service invocation, state management, and publish/subscribe messaging. You'll explore all of those in addition to various other advanced features with this practical guide to learning Dapr. By the end of this book, you'll be able to write microservices easily using your choice of language or framework by implementing industry best practices to solve problems related to distributed systems.
Table of Contents (16 chapters)
1
Section 1: Introduction to Dapr
4
Section 2: Building Microservices with Dapr
10
Section 3: Deploying and Scaling Dapr Solutions

An overview of Dapr

Dapr is an event-driven, portable runtime created by Microsoft with an open source approach and, at the time of writing this book, is still in active development.

Being event-driven, which is emphasized in the definition of Dapr, plays an important role in microservices as an application can be designed to efficiently react to events from external systems or other parts of the solution, and to produce events as well, in order to inform other services of new facts or to continue processing elsewhere or at a later stage.

Dapr is portable, as it can run locally on your development machine in self-hosted mode; it can also be deployed to the edge, or it can run on Kubernetes.

The following diagram shows the many building blocks of a Dapr architecture:

Figure 1.1 – Dapr architecture

Figure 1.1 – Dapr architecture

Portability does also extend beyond the hosting environment—while Dapr is an initiative that was started by Microsoft, it can also run on Kubernetes on-premises or in the cloud, with Microsoft Azure, Amazon Web Services (AWS), Google Cloud Platform (GCP), or any other cloud vendor.

Dapr has been built on the experience gained by Microsoft in developing hyperscale cloud-native applications. It has been inspired by the design of Orleans and Service Fabric, which in turn enables many Microsoft Azure cloud services to operate resiliently and at a large scale.

Dapr offers developers an approach to design the tools to build and the runtime to operate applications, based on a microservice architecture style.

Microservices offer a vast array of benefits balanced by increased complexities in team and product management, usually with a significant burden on the developer and the team in order to get started.

What if you could leverage a runtime such as Dapr to help you get through the common patterns you will likely need to adopt, and thus ease your operations?

This diagram shows the two Dapr hosting modes:

Figure 1.2 – Dapr sidecar

Figure 1.2 – Dapr sidecar

As depicted in Figure 1.2, the Dapr runtime operates in sidecar processes, lifting most of the complexity from the application to a separate environment, greatly simplifying development and operations as well. These sidecar processes are run locally in your development environment or as containers in a Pod on Kubernetes.

From an application perspective, Dapr is an application programming interface (API) that can be directly reached via HyperText Transfer Protocol (HTTP) or gRPC Remote Procedure Call (gRPC) calls or, even more simply, via any of the software development kits (SDKs) available. At the time of writing this book, these SDKs are .NET, Java, Go, Python, C++, JavaScript, and Rust.

As we will experience later, it is not necessary to adopt the Dapr SDK in your application: a call to a Dapr service can be as simple as an HTTP call to an endpoint such as http://localhost:3500/v1.0/invoke/<app-id>/method/<method name>. Nevertheless, using the SDK does provide many benefits if you are writing a Dapr service or leveraging the Dapr actor model.

What Dapr is not

While I hope the overview of Dapr has informed and intrigued you enough to spend time on this book, when I have the chance to talk about Dapr, I often find myself in need of clarifying what Dapr is not. This makes it easier to eliminate any misconceptions we may have about what Dapr does, as follows:

  • Dapr's goal is not to force the developer to embrace a programming model with strict rules and constraints. On the contrary—while the application developer is freed by Dapr of the many complexities of a microservice architecture, the developer is not mandated on how to write the application. As an example, the management of the connection pooling to the database where the state is stored is a responsibility of Dapr and, as we will see in the following chapters, it is transparent to the microservice application code.
  • Dapr is not a service mesh. While many similarities can be found in the general objectives of Dapr and service meshes, Dapr does provide these benefits at the application level while a service mesh operates on the infrastructure. For instance, Dapr applies retry logic in its interaction with state stores and services, but it is the developer's responsibility to decide how to handle errors Dapr might return if there is a conflict or an intermittent issue: whether raising an error back to the client, compensating an operation, or adopting a retry policy (maybe leveraging Polly in .NET), these are explicit choices only the developer can make.
  • Dapr is meant to be integrated with service meshes such as Istio, which is out of the scope of this book.
  • Dapr is not a Microsoft cloud service: it does help the developer build microservice applications in the cloud, and it surely provides many integrations with Azure cloud services, but it also has as many components for AWS, GCP, and other services.

    It is also true that Dapr does not run better on Azure than on any other Kubernetes environment in the cloud. I would hope to convince you that Azure Kubernetes Service (AKS) is the best-managed Kubernetes offering in the cloud space, but this is a different conversation for another time.

    Important note

    While this book is heavily skewed toward .NET, Dapr does provide the same benefits to Python developers (just as an example) as it provides SDKs for Dapr and Dapr Actor, working on macOS, and with Kubernetes as the deployment target—Dapr welcomes all developers in a vendor-neutral and open approach.

The next section will be dedicated to understanding the architectures that Dapr can enable.