Building microservices, does size matter?
Tags: dotnet, webdev, csharp, microservices
Published: 2019-08-06 12:29:04

A few months ago I had a chance to sit down and chat with Glenn Condron and Ryan Nowak from the ASP.NET Team about the new things coming in .NET 3.0 for Microservices.
 
{% youtube cNdPbTB72bw %}

**Topics**
* [00:22](https://www.youtube.com/watch?v=cNdPbTB72bw&t=22s) - What’s the goal with the Microservice templates in .NET Core 3.0?
* [03:05](https://www.youtube.com/watch?v=cNdPbTB72bw&t=185s) - What are the new Worker templates?
* [06:57](https://www.youtube.com/watch?v=cNdPbTB72bw&t=417s) - What’s happening with gRPC in .NET Core 3?
* [11:47](https://www.youtube.com/watch?v=cNdPbTB72bw&t=707s) - How we developers choose between gRPC and Web APIs?

**Resources**
* [Introduction to gRPC on ASP.NET Core](https://docs.microsoft.com/aspnet/core/grpc/?view=aspnetcore-3.0&WT.mc_id=devto-blog-shboyer)
* [Download .NET Core 3.0](https://dotnet.microsoft.com/download/dotnet-core/3.0?WT.mc_id=devto-blog-shboyer)
* [.NET Microservices Architecture Guidance](https://dotnet.microsoft.com/learn/web/microservices-architecture?WT.mc_id=devto-blog-shboyer)
* [Comparing gRPC series with HTTP APIs](https://docs.microsoft.com/aspnet/core/grpc/comparison?view=aspnetcore-3.0&WT.mc_id=devto-blog-shboyer)

# Does Size Matter for Microservices?

It got me thinking more about microservices in general and how to think of them. Is "micro" the right prefix here? Are they actually small? Probably not. 

The term is more around the responsibility, deployment (not the size of), manageability, service boundary, and development team.

# Hello World

Not to retract, but what is the minimal realistic service in .NET Core we can write to get simply produce a "Hello World"?

```csharp

public class Service
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Service>();
            });

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGet("/", async context =>
            {
                await context.Response.WriteAsync("Hello, world!");
            });
        });
    }
}
``` 

Here, a host is created with EndpointRouting set to handle the "/" route and return "Hello, world!". 

Using `dotnet run` you could browse to http://localhost:5000 and see the **Hello, world!** output to the screen. Not very practical, however, a single responsible service doing the job.

> Use the .NET CLI to create a new API template using `dotnet new webapi` for a more complete template to build on. See [tutorial/docs here](https://docs.microsoft.com/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio-code&WT.mc_id=devto-blog-shboyer).


# Focus on the job

Building the service should be more about the desire to create a "small focused" services instead of just a small service.

There is a risk vs reward conversation to be had here. The desire to create as many small services as possible in order to adhere to a mantra of microservices approach will lead to a degree of overhead not worth it. 

![](https://thepracticaldev.s3.amazonaws.com/i/yno5ri8kn8miifby0405.jpg)

Not sure about you, but every time I step in that pile it hurts...

# Bounded contexts

Depending on where you look, listen, read, etc., Domain-Driven Design may come into the picture on how to put together complexity in large systems and it has many great tools.

One term used in DDD is [aggregate](http://martinfowler.com/bliki/DDD_Aggregate.html) to describe a cluster or collection of objects treated as a single unit. So in an application where we are handling customer orders, it would make sense for all of the "CustomerOrder" operations and data to be handled by a single service.

However, it is easy to have other "Customer" or "Order" operations bleed into the service. Finding yourself grouping classes or entities instead of the actual capabilities of the services.

Having the ability to maintain and deploy each service/schema independently is a large benefit, and scaling each service on a need basis lends itself in this architecture as well. However, being network chatty is a side effect. Are you ok with a bunch of calls as long as they are super fast in exchange for ease of deployments, scalability and, maintainability?

# How small is small?

Is the team small? 6-8 engineers or a handful seems to be an ideal number depending on the application, but large systems have been built by smaller teams and vice versa.

My code use to be measured by the number of lines? 

Size on disk? Disk space is cheap some would say.

Containers are getting smaller every day, until you put an app in it.

What are your thoughts?