title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
11:9
| |
11:10
|
By the end of this chapter, you will have learned how to implement a single microservice in .NET. Chapter 20, Kubernetes, also explains how to deploy, debug, and manage a whole microservices-based application. Chapter 14, Implementing Microservices with .NET, and Chapter 18, Implementing Frontend Microservices with ASP.NET Core, are step-by-step guides to the practical implementation of microservices with .NET.
|
11:11
|
Technical requirements
|
11:12
|
The code for this chapter is available at https://github.com/PacktPublishing/Software-Architecture-with-C-Sharp-12-and-.NET-8-4E.
|
11:13
|
In this chapter, you will require the following:
|
11:14
| |
11:15
|
Visual Studio 2022 free Community Edition or better with all the database tools installed.
|
11:16
|
A free Azure account. The Creating an Azure account section in Chapter 1, Understanding the Importance of Software Architecture, explains how to create one.
|
11:17
|
Docker Desktop for Windows if you want to debug Docker containerized microservices in Visual Studio (https://www.docker.com/products/docker-desktop).
|
11:18
| |
11:19
|
In turn, Docker Desktop for Windows requires at least Windows 10 with either Windows Subsystem for Linux (WSL) or Windows Containers installed.
|
11:20
|
WSL enables Docker containers to run on a Linux virtual machine and can be installed as follows (see also https://learn.microsoft.com/en-us/windows/wsl/install):
|
11:21
| |
11:22
|
Type powershell in the Windows 10/11 search bar.
|
11:23
|
When Windows PowerShell is proposed as a search result, click on Run as an administrator.
|
11:24
|
In the Windows PowerShell administrative console that appears, run the command wsl --install.
|
11:25
| |
11:26
|
Windows Containers enable Docker containers to run directly on Windows, but they require at least the Windows Professional edition. They can be installed as follows:
|
11:27
| |
11:28
|
Type Windows features in the Windows 10/11 search bar.
|
11:29
|
The search results will propose running the panel to enable/disable Windows features.
|
11:30
|
Click on it, and in the window that opens, select Containers.
|
11:31
| |
11:32
|
What are microservices?
|
11:33
|
Microservices are essentially small, independent units that make up a larger software application, each with its specific role and functionality. Splitting a software application into independent microservices allows each module that makes up a solution to be scaled independently from the others to achieve the maximum throughput with minimal cost. In fact, scaling whole systems instead of their current bottlenecks inevitably results in a remarkable waste of resources, so fine-grained control of subsystem scaling has a considerable impact on the system’s overall cost.
|
11:34
|
However, microservices are more than scalable components – they are software building blocks that can be developed, maintained, and deployed independently of each other. Splitting development and maintenance among modules that can be independently developed, maintained, and deployed improves the overall system’s CI/CD cycle (CI/CD was described in detail in Chapter 8, Understanding DevOps Principles and CI/CD).
|
11:35
|
The CI/CD improvement is due to microservice independence because it enables the following:
|
11:36
| |
11:37
|
Scaling and distributing microservices on different types of hardware.
|
11:38
|
Since each microservice is deployed independently from the others, there can’t be binary compatibility or database structure compatibility constraints. Therefore, there is no need to align the versions of the different microservices that compose the system. This means that each of them can evolve as needed without being constrained by the others.
|
11:39
|
However, attention must be paid to the choice of communication protocols and messages and to their versions, which must be supported by all involved microservices. Protocols that are widely supported and that facilitate backward compatibility with previous versions of messages should be preferred.
|
11:40
| |
11:41
| |
11:42
|
Assigning their development to completely separate smaller teams, thus simplifying job organization and reducing all the inevitable coordination inefficiencies that arise when handling large teams.
|
11:43
|
Implementing each microservice with more adequate technologies and in a more adequate environment since each microservice is an independent deployment unit. This means choosing tools that best fit your requirements and an environment that minimizes development efforts and/or maximizes performance.
|
11:44
|
Since each microservice can be implemented with different technologies, programming languages, tools, and operating systems, enterprises can use all available human resources by matching environments with developers’ competencies. For instance, all available Java and .NET developers can cooperate in the same application, thus exploiting all available resources.
|
11:45
|
Legacy subsystems can be embedded in independent microservices, thus enabling them to cooperate with newer subsystems. This way, companies may reduce the time to market new system versions. Moreover, in this way, legacy systems can evolve slowly toward more modern systems with an acceptable impact on costs and the organization.
|
11:46
| |
11:47
|
The next subsection explains how the concept of microservices was conceived. Then, we will continue this introductory section by exploring basic microservice design principles and analyzing why microservices are often designed as Docker containers.
|
11:48
|
Microservices and the evolution of the concept of modules
|
11:49
|
For a better understanding of the advantages of microservices, as well as their design techniques, we must keep the two-fold nature of software modularity and software modules in mind:
|
11:50
| |
11:51
|
Code modularity refers to code organization that makes it easy for us to modify a chunk of code without affecting the remainder of the application. It is usually enforced with object-oriented design, where modules can be identified with classes.
|
11:52
|
Deployment modularity depends on what your deployment units are and which properties they have. The simplest deployment units are executable files and libraries. Thus, for instance, dynamic link libraries (DLLs) are, for sure, more modular than static libraries since they must not be linked with the main executable before being deployed.
|
11:53
| |
11:54
|
While the fundamental concepts of code modularity have reached stasis, the concept of deployment modularity is still evolving, and microservices are currently state-of-the-art along this evolution path.
|
11:55
|
As a short review of the main milestones on the path that led to microservices, we can say that, first, monolithic executables were broken into static libraries. Later on, DLLs replaced static libraries.
|
11:56
|
A great change took place when .NET (and other analogous frameworks, such as Java) improved the modularity of executables and libraries. In fact, with .NET, they can be deployed on different hardware and different operating systems since they are deployed in an intermediary language that is compiled when the library is executed for the first time. Moreover, they overcome some versioning issues of previous DLLs since any executable can bring with it a DLL with a version that differs from the version of the same DLL that is installed on the operating system.
|
11:57
|
However, .NET can’t accept two referenced DLLs – let’s say, A and B – using two different versions of a common dependency – let’s say, C. For instance, suppose there is a newer version of A with many new features we would like to use that, in turn, relies on a newer version of C that is not supported by B. In this situation, we should renounce the newer version of A because of the incompatibility of C with B. This difficulty has led to two important changes:
|
11:58
| |
11:59
|
Packages: The development world moved from using single DLLs and/or single files as deployment units to using packages composed of both DLLs and metadata as deployment units. Packages are handled by package management systems such as NuGet and npm, which use package metadata to automatically check version compatibility with the help of semantic versioning.
|
11:60
|
Service-Oriented Architecture (SOA): Deployment units started being implemented as SOAP-based web services and later transition to REST web services. This solves the version compatibility problem since each web service runs in a different process and can use the most adequate version of each library with no risk of causing incompatibilities with other web services. Moreover, the interface that is exposed by each web service is platform-agnostic; that is, web services can connect with applications using any framework and run on any operating system since web service protocols are based on universally accepted standards. SOAs and protocols will be discussed in more detail in Chapter 15, Applying Service-Oriented Architectures with .NET.
|
11:61
| |
11:62
|
Microservices are an evolution of SOA and add more features and more constraints that improve the scalability and the modularity of services to improve the overall CI/CD cycle. It’s sometimes said that microservices are SOA done well. Moreover, as we will see in the next section, microservices are strictly tied with the DDD methodology described in Chapter 7, Understanding the Different Domains in Software Solutions.
|
11:63
|
To sum things up, the microservice architecture is an SOA that maximizes independence and fine-grained scaling. Now that we’ve clarified all the advantages of microservice independence and fine-grained scaling, as well as the very nature of independence, we are in a position to look at microservice design principles.
|
11:64
|
Microservice design principles
|
11:65
|
In this section, you will learn about the microservices’ basic design principles. These principles are the basis for designing each microservice’s code and architecture, and for designing the whole application architecture.
|
11:66
|
Let’s start with principles that arise from the independence constraint. We will discuss them each in a separate subsection.
|
11:67
|
The independence of design choices
|
11:68
|
A fundamental design principle is the independence of design choices, which can be stated as follows:
|
11:69
| |
11:70
|
The design of each microservice must not depend on the design choices that were made in the implementation of other microservices.
|
11:71
| |
11:72
|
This principle enables the full independence of each microservice CI/CD cycle and leaves us with more technological choices on how to implement each microservice. This way, we can choose the best available technology to implement each microservice.
|
11:73
|
Another consequence of this principle is that different microservices can’t connect to the same shared storage (database or filesystem) since sharing the same storage also means sharing all the design choices that determine the structure of the storage subsystem (database table design, database engine, and so on). Thus, either a microservice has its own data storage, or it has no storage at all and communicates with other microservices that take care of handling storage.
|
11:74
|
Dedicated data storage can be implemented either by physically including the database service within the boundary of the microservice or with an external database that the microservice has exclusive access to. Both are acceptable design choices. However, external databases are usually adopted because, for performance reasons, database engines are better run on dedicated hardware and with OS and hardware features that are optimized for their storage functionalities.
|
11:75
|
Usually, the independence of design choices is interpreted in a lighter form by distinguishing between logical and physical microservices. More specifically, logical microservices are the result of splitting the application into logical independent modules. If the application is designed with a domain-driven design (DDD) methodology, logical microservices correspond to DDD-bounded contexts, which we discussed in detail in Chapter 7, Understanding the Different Domains in Software Solutions.
|
11:76
|
In turn, each logical microservice may be split into various physical microservices that use the same data storage but that are load-balanced independently to achieve a better load balance.
|
11:77
|
For instance, in the book case study, travel payments are handled by the Payments Bounded Context described in the Understanding the domains of the WWTravelClub application section of Chapter 21, Case Study, which gives rise to a unique logical microservice. However, its practical implementation requires two main submodules:
|
11:78
| |
11:79
|
A customer credit card verification and authorization module, which takes care of all credit card verifications
|
11:80
|
A user credits management module, which handles credits that the user already purchased, card information already loaded in the platform, and new credit card info loading
|
11:81
| |
11:82
|
Since the process of credit card verification and authorization might be very time-consuming, it is convenient to implement the two submodules above as independent physical microservices, so they can be load-balanced separately.
|
11:83
|
Independence from the deployment environment
|
11:84
|
During load-balancing, microservices can be moved from very busy hardware nodes to more idle nodes. However, dependencies of each microservice on other software/files of the destination hardware nodes constrain the possible destination nodes.
|
11:85
|
Therefore, the more we reduce microservice dependencies, the more we have the freedom to move them from busy nodes to idle nodes, achieve a better load balance, and exploit the available hardware nodes.
|
11:86
|
This is the reason microservices are often containerized and use Docker. Containers will be discussed in more detail in the Containers and Docker subsection of this chapter, but basically, containerization is a technique that allows each microservice to bring its dependencies with it so that it can run anywhere. However, this is not a must because, in some applications, one might verify that all dependencies requirements of all microservices can be easily satisfied by all available nodes.
|
11:87
|
As we explore how microservices operate within their containerized environments, another key architectural principle comes into play – the concept of loose coupling.
|
11:88
|
Loose coupling
|
11:89
|
Each microservice must be loosely coupled with all the other microservices. This principle has a two-fold nature. On the one hand, this means that, according to object-oriented programming principles, the interface that’s exposed by each microservice must not be too specific but as general as possible. However, it also means that communications among microservices must be minimized in order to reduce communication costs since microservices don’t share the same address space and run on different hardware nodes.
|
11:90
|
For instance, suppose we are implementing a distributed web video game with a microservice architecture. Each microservice might take care of different functionalities, like collisions, visibility, user input handling, and so on. Some modules, like the collision and visibility modules, must know the whole game state, such as places where the user avatars are, the state of each avatar, and also the state of each reactive object that is in the game (such as obstacles, bullets shot by avatars, and so on). Therefore, either all the modules with a hard dependency on the whole game state are collapsed into a unique microservice or we must find an efficient way to share the overall game state between them with just a few message exchanges.
|
11:91
|
Both options have advantages and disadvantages and are actually adopted by real-world video games. Fewer messages might cause temporary incongruences, but melting too many modules into a unique microservice might impact the overall game performance so that the game might appear too “slow” to the users.
|
11:92
|
This concept of minimal inter-service communication naturally leads us to another consideration: the avoidance of chained requests/responses in a microservice architecture
|
11:93
|
No chained requests/responses
|
11:94
|
When a request reaches a microservice, it must not cause a recursive chain of nested requests/responses to other microservices since a similar chain would result in an unacceptable response time.
|
11:95
|
For instance, suppose that microservice A issues a request to microservice B and then waits for B to answer, and B does the same with C, and C does the same with D, and so on. As a result, A remains blocked waiting for its answer for the whole time the request propagates first to B, then to C, and then to D, and then the answer propagates back from D to C, then from C to B, and finally reaches A. That is, four request propagation times sum to the other four answer propagation times to get the overall A wait time. This way, the time a user waits to get an answer from the application might easily become unacceptable.
|
11:96
|
Chained requests/responses can be avoided if the private data models of all the microservices synchronize with push events each time they change. In other words, as soon as the data that is handled by a microservice changes, those changes are sent to all the microservices that may need them to serve their requests. This way, each microservice has all the data it needs to serve all its incoming requests in its private data storage, with no need to ask other microservices for the data that it lacks.
|
11:97
|
Figure 11.1 shows how updates are sent to all interested microservices as soon as they are produced and how each microservice combines all received updates in a local database. This way, each query microservice has all the data it needs to answer queries in its local database.
|
11:98
| |
11:99
|
Figure 11.1: Push events
|
11:100
|
In conclusion, every microservice must contain all the data it needs to serve incoming requests and ensure fast responses. To keep their data models up to date and ready for incoming requests, microservices must communicate their data changes as soon as they take place. These data changes should be communicated through asynchronous messages since synchronous nested messages cause unacceptable performance because they block all the threads involved in the call tree until a result is returned.
|
11:101
|
It is worth pointing out that the independence of design choices principle is substantially the bounded context principle of DDD, which we discussed in detail in Chapter 7, Understanding the Different Domains in Software Solutions. In this chapter, we have seen that, often, a full DDD approach is useful for the update subsystem of each microservice.
|
11:102
|
It’s not trivial that, in general, all systems that have been developed according to the bounded context principle are better implemented with a microservice architecture. In fact, once a system has been decomposed into several completely independent and loosely coupled parts, it is very likely that these different parts will need to be scaled independently because of different traffic and different resource requirements.
|
11:103
|
At the preceding constraints, we must also add some best practices for building a reusable SOA. More details on these best practices will be given in Chapter 15, Applying Service-Oriented Architectures with .NET, but nowadays, most SOA best practices are automatically enforced by tools and frameworks that are used to implement web services.
|
11:104
|
Fine-grained scaling is a key aspect of microservices architecture, involving several critical software and infrastructure requirements:
|
11:105
| |
11:106
|
First of all, microservices must be small enough to isolate well-defined functionalities.
|
11:107
|
We also need a complex infrastructure that takes care of automatically instantiating microservices and allocating instances on various hardware computational resources, commonly called nodes.
|
11:108
|
The same infrastructure must take care of scaling microservices and load-balancing them on the available nodes.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.