Building Microservices with Dapr and .NET | Part-I

Arkaprava Sinha
FAUN — Developer Community 🐾
6 min readJul 16, 2023

--

What is Dapr?

Dapr as the name suggests, it means distributed application runtime, this a runtime, that makes building microservices superb easy in C#/Java or any language of your choice. Dapr provides a mechanism to loosely couple your code with underlying infrastructure so that developer can focus on writing the code. For example, today we might be using Redis for our state management but tomorrow we might need to use Memcached. If we want to integrate Memcached then again we have to modify our code to support that, but with Dapr, application code can remain same, Dapr will take care of switching the infrastructure using it’s runtime. It helps to keep the application code platform agnostic. For our purpose in this tutorial series, we are going to use C# and Dapr .Net SDK.

How Dapr Works?

Dapr uses a side car architecture to expose it self as http or gRPC service from a container or a process, which normally our application code call to leverage the platform agnostic behavior. For example, if my API which is written in .Net normally performs a state operations in SQL Server, now my application code can be decoupled using Dapr runtime, my application code will call Dapr API over http or gRPC and Dapr will persist or query my SQL Server and return the results. Dapr will be my middle man to do my bidding.

From: https://docs.dapr.io/concepts/overview/

What are the things that are provided by Dapr out of the box?

In normal microservices world below are the important things that are required to make microservices work, Dapr provides them most of the out of the box, we can choose to use some of them or most of them depending upon requirement.

  1. Service to Service Invocation: Dapr provides service to service communications with resiliency, retries, method invocations and also on the remote service. But remember, if you are already using a Service Mesh solution such as Linkered, Istio, Consul etc. Dapr also complement them, but Dapr does not provide a full fledged features that is expected from a Service Mesh. Don’t go ahead with Dapr as a Service Mesh replacement. Dapr is not capable of traffic routing and splitting etc.
From: https://docs.dapr.io/concepts/service-mesh/

2. State Management: Dapr provides an easy way to save, read and query key/value pairs using it’s state management API onto any state store of our choices, as of now, it supports, Redis, Memcached, Azure Cosmos DB, SQL Server, Postgres SQL etc.

From: https://docs.dapr.io/developing-applications/building-blocks/state-management/state-management-overview/

3. Pub/Sub : Dapr provides a Pub/Sub mechanism to allow microservices to communicate with each other in a event driven world. To do this Dapr provides a Pub/Sub endpoint that will take care of communicating with underlying infrastructure such as Kafka, Rabbit MQ, Azure Storage Queue etc. If you ae thinking of the errors, Dapr also provides support for dead lettering and at least once guarantees out of the box.

From: https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-overview/

4. Bindings: Dapr provides binding API’s which will help to listen to changes such as queues message, file-system changes etc. and take appropriate actions on them or provide. It provides both input and output bindings. Input bindings will help to take proper actions on input and output bindings help in returning the result after a action being taken.

From: https://docs.dapr.io/developing-applications/building-blocks/bindings/howto-triggers/

5. Actors: Dapr also provides Actor API’s which help to implement Actor pattern. Actor pattern by definition is a basic building block for concurrent computation. For example, if you are subscribing to messages on a Kafka Topic, you may need perform some actions on the message (either store the data on DB or send the processed message forward to other topics, so that other actors may work on them). Dapr manages the concurrency on Actors level by providing a turn based access model. Turn based access model fully specifies that a new actor will be only taken in consideration when older actor is fully executed and nothing is pending on it.

6. Observability: Dapr provides observability for it’s own components by following Open Telemetry standards. It provides distributed tracing, logging, metrics etc. Dapr control plane and side car also emits health metrics and logging which is helpful for diagnosing any issue in the curent system.

From: https://docs.dapr.io/concepts/observability-concept/

7. Secrets Management: Dapr also provides a unified way to fetch secrets from any Secret Management tool such as Vault, Azure KeyValut in a unified way and decouple the dependency on the underlying secret stores.

From: https://docs.dapr.io/developing-applications/building-blocks/secrets/secrets-overview/

8. Configuration Management: Dapr also provides API’s to manage application configuration such as Batch Size, DB Connection Pool Size etc. We can also watch for changes in the configuration and do reload on application configurations.

From: https://docs.dapr.io/developing-applications/building-blocks/configuration/configuration-api-overview/

9. Distributed Locks: Dapr also provides API to manage locks in a distributed system efficiently. Let’s say we have resource which only one instance of my service needs to access at a single time for modification purposes, then we can leverage Dapr distributed locks to helps to implement the same.

From: https://docs.dapr.io/developing-applications/building-blocks/distributed-lock/distributed-lock-api-overview/

10. Workflows: In today’s distributed microservices world, we have to handle multiple complex business logic spread across multiple microservices. to handle this scenario, we need to have an orchestrator to perform my business logics. It is quite similar with Azure Durable Functions. Dapr provides workflow API’s to handle this kind of scenarios.

From: https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-overview/

There are other building blocks also provided such as Cryptography block as well.

These building blocks we will discuss in each part of these series to gain more understanding.

Dapr Performance Concerns

As we know that Dapr introduced an extra layer in our services to manage them efficiently, it also means that there is an extra hop in all other communications. Internally when Dapr sidecar communicates with each other, they use gRPC by default, so there is performance is better, but still these extra hops can add to our network latency overhead (we have to keep them in mind). But as Dapr claims, still this latency will be in few milli seconds, still it is blessing for microservice developers.

Dapr Hosting Mechanism:

Dapr provides multiple hosting mechanisms such as Self Hosted, where we need to host and manage everything in Virtual Machines, then most popular one, Kubernetes hosting and then comes local docker hosting which helps developer to build things locally.

In our series we are going to use Dapr hosting mechanism in Kubernetes, will cover them in the next blog.

Reference:

  1. Dapr Official Doc: https://docs.dapr.io/

👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN Developer Community & Get Similar Stories in your Inbox Each Week

--

--