@schneidenbach
Microservices in .NET
Spencer Schneidenbach
Slide 2
Slide 2 text
@schneidenbach
Microservices in .NET
Spencer Schneidenbach
Slide 3
Slide 3 text
@schneidenbach
My Background
Slide 4
Slide 4 text
@schneidenbach
Slide 5
Slide 5 text
@schneidenbach
and
…and React
Slide 6
Slide 6 text
@schneidenbach
Microservices.
Why do I care?
Slide 7
Slide 7 text
@schneidenbach
I DON’T LIKE TAKING
CALLS AFTER 5 PM.
Slide 8
Slide 8 text
@schneidenbach
I have lots of tools to help in
this regard
• Code reviews
• Expectation setting with clients
• CypressJS (for automated testing)
• And, to some degree, microservices
@schneidenbach
We got things to talk about
• What are microservices?
• What’s the good/bad/ugly?
• What does “the .NET” have? (Azure? AWS?)
Slide 12
Slide 12 text
@schneidenbach
What are
microservices?
Slide 13
Slide 13 text
@schneidenbach
Not a tech stack- a
mentality
Slide 14
Slide 14 text
@schneidenbach
Microservices have
some common elements
Slide 15
Slide 15 text
@schneidenbach
Loosely coupled
Slide 16
Slide 16 text
@schneidenbach
Loosely coupled
Two systems are loosely coupled if changes to the design,
implementation, or behavior in one won't cause changes in
another.
https://www.capitalone.com/tech/software-engineering/how-to-avoid-loose-coupled-
microservices
Slide 17
Slide 17 text
@schneidenbach
Data ownership
Slide 18
Slide 18 text
@schneidenbach
Separately
deployable
Slide 19
Slide 19 text
@schneidenbach
Monitored
Slide 20
Slide 20 text
@schneidenbach
Highly testable and
tested
Slide 21
Slide 21 text
@schneidenbach
Slide 22
Slide 22 text
@schneidenbach
Case Study #1
Slide 23
Slide 23 text
@schneidenbach
Slide 24
Slide 24 text
@schneidenbach
Slide 25
Slide 25 text
@schneidenbach
Slide 26
Slide 26 text
@schneidenbach
Slide 27
Slide 27 text
@schneidenbach
Slide 28
Slide 28 text
@schneidenbach
Slide 29
Slide 29 text
@schneidenbach
Requirements
• Decoupled from main app for rapid, iterative release
• If main app went down (it did sometimes) make sure it
would get message later
• Push text noti
fi
cations to employees to ensure timely
response
Slide 30
Slide 30 text
@schneidenbach
Slide 31
Slide 31 text
@schneidenbach
Message Queue
Slide 32
Slide 32 text
@schneidenbach
Slide 33
Slide 33 text
@schneidenbach
Slide 34
Slide 34 text
@schneidenbach
Microservice
communication
Slide 35
Slide 35 text
@schneidenbach
Microservice
communication
• HTTP
• Message bus
Slide 36
Slide 36 text
@schneidenbach
Slide 37
Slide 37 text
@schneidenbach
Microservice
communication
• Message bus to carry events/commands
• Use pointers and then provide HTTP endpoints to get
data later
Slide 38
Slide 38 text
@schneidenbach
Requirements
• Decoupled from main app for rapid, iterative release
• If main app went down (it did sometimes) make sure it
would get message later
• Push text noti
fi
cations to employees to ensure timely
response
Slide 39
Slide 39 text
@schneidenbach
My First Microservice
Awwwwwwww how cute
Slide 40
Slide 40 text
@schneidenbach
This is a
microservices talk, so
I’ll answer this
question: are
monoliths inherently
bad?
Slide 41
Slide 41 text
@schneidenbach
No.
Slide 42
Slide 42 text
@schneidenbach
No.
Slide 43
Slide 43 text
@schneidenbach
How many of you work
on existing monoliths?
Slide 44
Slide 44 text
@schneidenbach
Monoliths are
fi
ne…
until they’re not
Slide 45
Slide 45 text
@schneidenbach
Oftentimes, microservices is a good
pattern to break off a piece that is
particularly tricky for some reason or
another (reliability, performance, etc.)
Slide 46
Slide 46 text
@schneidenbach
How Bout That Dot Net?
Slide 47
Slide 47 text
@schneidenbach
ASP.NET Core is a
great start
Slide 48
Slide 48 text
@schneidenbach
ASP.NET Core is a
great start
(Examples forthcoming)
Slide 49
Slide 49 text
@schneidenbach
“Serverless”
• Azure Functions
• AWS Lambda
• …whatever it is that Google has - ask Jon Skeet
@schneidenbach
Three major choices
• Write your own queue readers and processors
• Use a free library, MassTransit
• Use a not-so-free library, NServiceBus
Slide 54
Slide 54 text
@schneidenbach
I recommend MassTransit
or NServiceBus
• That said, if you’re new to service-based architectures, be
prepared to read their docs a lot and copy/paste sample
code.
Slide 55
Slide 55 text
@schneidenbach
NServiceBus pros
• Enterprise-focused
• Amazing support team
• Lots of other tooling that comes with the NServiceBus
system (for monitoring, etc)
Slide 56
Slide 56 text
@schneidenbach
MassTransit pros
• Free and open source
• Decent support, but no centralized support (basically,
Google, StackOver
fl
ow, samples, and their Discord
channel)
Slide 57
Slide 57 text
@schneidenbach
Let’s look at some code
Slide 58
Slide 58 text
@schneidenbach
Some useful microservice
patterns/tools you should
be aware of
@schneidenbach
Backends for
Frontends
Basically: service and data aggregation
Slide 63
Slide 63 text
@schneidenbach
Versioning
Slide 64
Slide 64 text
@schneidenbach
Versioning
Let’s assume SendEmail supports one attachment
Slide 65
Slide 65 text
@schneidenbach
public class SendEmail
{
public string From { get; set; }
public string To { get; set; }
//other stuff
public Attachment Attachment { get; set; }
}
Slide 66
Slide 66 text
@schneidenbach
public Attachment[] Attachments { get; set; }
public Attachment Attachment { get; set; }
to
Slide 67
Slide 67 text
@schneidenbach
Versioning
Slide 68
Slide 68 text
@schneidenbach
public class SendEmailV2
{
public string From { get; set; }
public string To { get; set; }
//other stuff
public Attachment[] Attachments { get; set; }
}
Slide 69
Slide 69 text
@schneidenbach
Versioning
Slide 70
Slide 70 text
@schneidenbach
Monitoring
• Azure Application Insights
• Other products like Datadog
Slide 71
Slide 71 text
@schneidenbach
Using correlation IDs
for monitoring
Link
Slide 72
Slide 72 text
@schneidenbach
API Gateway
• Management tool between client and published APIs
• Often handles authentication, documentation, rate
limiting, etc.
Slide 73
Slide 73 text
@schneidenbach
Containers/Kubernetes
Slide 74
Slide 74 text
@schneidenbach
Containers/K8S
Slide 75
Slide 75 text
@schneidenbach
Kubernetes
Slide 76
Slide 76 text
@schneidenbach
Case Study #2
Slide 77
Slide 77 text
@schneidenbach
Client of mine
• Startup in a speci
fi
c vertical
• They process a lot of
fi
les that get sent to them
Slide 78
Slide 78 text
@schneidenbach
Slide 79
Slide 79 text
@schneidenbach
Advantages
• Used all of the Azure bits
• Storage queues for messages and
fi
le storage
• Event Grid to listen for incoming
fi
les
• Azure SQL for, well, stu
ff
you’d put in a database
• Easy-ish to monitor using Application Insights
Slide 80
Slide 80 text
@schneidenbach
Problems
• Reached limits on the database
• Reached execution limits on Azure Functions - oh and it
got really ‘spensive
Slide 81
Slide 81 text
@schneidenbach
Slide 82
Slide 82 text
@schneidenbach
Microservice anti-
patterns
Slide 83
Slide 83 text
@schneidenbach
Slide 84
Slide 84 text
@schneidenbach
Slide 85
Slide 85 text
@schneidenbach
Dependency disorder
Slide 86
Slide 86 text
@schneidenbach
Distributed monolith
Slide 87
Slide 87 text
@schneidenbach
Slide 88
Slide 88 text
@schneidenbach
Shared database
Slide 89
Slide 89 text
@schneidenbach
Slide 90
Slide 90 text
@schneidenbach
Slide 91
Slide 91 text
@schneidenbach
Layered Services
Slide 92
Slide 92 text
@schneidenbach
Slide 93
Slide 93 text
@schneidenbach
Slide 94
Slide 94 text
@schneidenbach
Shared codebases
Slide 95
Slide 95 text
@schneidenbach
Some more links
• eShop on Containers - https://github.com/dotnet-
architecture/eShopOnContainers