Skip to content

MicroService Development Process

Wim van Gool edited this page Jul 16, 2019 · 1 revision

Designing, implementing, deploying and maintaining high quality software systems is a complex endeavor. Programming, the art of writing code that is not only correct but is also reasonably efficient, readable and maintainable, is just one of the challenges every developer faces when implementing software systems. Other challenges include, but are not limited to...

  • ensuring that the needs and requirements of the business are understood,
  • creating a design that works for the short-term but is also flexible enough for the long-term,
  • foreseeing all kinds of risks regarding security, performance, use of frameworks, infrastructure, etc,
  • estimating the complexity and required effort of specific work-items,
  • monitoring the behavior of the software and infrastructure once it is deployed,
  • troubleshooting and fixing all kinds of bugs, performance problems and what have you.

Because of all these challenges, it is absolutely essential to thoroughly test your software before you release it to production. Note that testing is not just about checking whether or not you have written bug-free code, but also about whether or not you build the right product (does it do what was asked, or, even more important, does it do what the business needs - which can be different things).

For this reason, implementing and executing tests during the different phases of the software development process have become the standard of today: Unit Testing, Test-Driven Development (TDD), Behavior-Driven Development (BDD), Integration Testing, UI Testing, etc. However, many developers have different views or opinions about what a 'unit test' actually is or how to apply Behavior Driven Development in practice, because these terms have no globally acknowledged definition.

For example, most developers I've met know the importance of unit testing and integration testing and systematically implement such tests while developing new features. Unfortunately, though, each developer has its own view or preference on how to write these tests, how to name them, what to mock and what not to mock, etc. Furthermore, some developers make a clear distinction between what a 'unit test' and an 'integration test' or 'regression test' is, while others will call every automated test a unit test.

This inevitably leads to a world where these concepts are applied differently in each project and the way software is being developed and tested is, at best, being reinvented from one project to another. That's a shame, because productivity and quality can be increased when developers agree on these concepts and how to apply them. What we are missing is a concrete definitions of terms and a common way of building and testing MicroServices.

Hence, in this series, I would like to rather strictly define the different types of tests you can execute and propose a structured, common approach to implementing a MicroService, all based on the behavioral and structural aspects of a MicroService that we discussed previously. The first goal of this series is to understand the way we, as the authors of the Kingo Framework, think about MicroService development, so that you can better understand the design and purpose of this framework. The second goal is to standardize your way of working while adding features to your MicroServices, enabling you to be more productive and maintain a high standard on the quality of your code.

We will kick-off this series by taking a step back and shortly discuss the software development process in a nutshell, reviewing the general practice of designing, implementing and testing software systems. On the next page, we will take the first step of the proposed development process by creating a simple, technical design on the architectural level for one specific feature of a web shop. After creating this design, we will explain how you can systematically implement this design and test this feature in Step 2, Step 3, Step 4 and Step 5. Finally, in Step 6, we will discuss the things you can do to deploy your service to various environments all the way up until production, and how to test your service once it is released.

Please note that during this entire series, we will attempt to keep the discussion on a conceptual level and avoid using any code samples, so that this process can be applied regardless of platform or programming language. Needless to say, we will eventually apply this entire process using the Kingo Framework to implement a real application (a game) in the upcoming tutorial.

Software Development Process in a Nutshell

When you manufacture complex products, tests are typically done at different phases of production. First, the smallest parts are designed and tested in isolation. Then, some parts are assembled into half-products and tested together. Finally, all half-products are assembled together into the final product and different types of tests are performed on the product as a whole. Some of these tests, especially of the individual parts and half-products, may be automated by machines that, for example, test the size, flexibility or robustness of a component. Other tests may involve people manually assessing the quality of a product. This is often required to assess properties that are hard to measure, like emotional traits - How does it look? How does it feel? Is the product easy to understand and use? And most importantly: does it serve the purpose it was designed to fulfill?

Software testing works in a similar way. When designing a software system or application, the whole system is always broken into smaller components. In case the global architecture is a Service Oriented Architecture, these smaller components are the MicroServices, where each MicroService is assigned a very specific role in the system with a specific set of features.

Subsequently, MicroServices can broken down into different internal layers and components, where each layer and/or component has its specific responsibility within the service. On the lowest scale, these layers and components are implemented by individual classes and functions - provided you use a high-level programming language such as C# or Java. As such, you can break down Service Oriented software systems into these levels of integration or scales:

Levels of Integration

Just as with manufacturing other types of products, requirements and design starts on the highest, architectural scale. At this point, the global vision and objectives of the system are defined. When these are determined, the system is slowly broken up into smaller parts and more precise specifications, moving down to the detailed design of individual (Micro)Services.

When implementation and testing of the system is started, the direction in inverted. First, the implementation and tests of the smaller components - the internal structure and behavior of each MicroService - are completed. Then, these smaller parts are assembled and tested together as fully operational MicroServices. Finally, when a few MicroServices are deployed and form a system, the system can be tested as a whole.

If you've been around long enough, you probably still remember the V-model that was used back in the day when the Waterfall model of software development was still being applied in software project. This model was often used to visualize this process of design from global to increasingly detailed on the left, as opposed to the process of implementation, testing and delivery on the right:

V-model

Source: softwaretestingstudio.com

The Waterfall model process was basically copied from existing, physical product manufacturing processes, where the entire system would be specified and designed up-front. Only after the complete design down to the most detailed level was completed, implementation was finally started, assuming that programmers and testers only had to follow the specifications to fully implement and deliver a working system.

Of course, we now all know that this method is not effective for software development. The most essential problem of the Waterfall model was that there was no feedback loop to the designers and the business to validate in time that their design described a system of value to the end-users, that the technical specifications were sound and complete, that the system was technically achievable at all and whether or not the benefits of the system would outweigh its costs. In other words: the waterfall model caused the industry more often than not to go overboard with time and costs and deliver a software system not meeting the needs of its end-users - if a working system was delivered at all.

This is why the software industry - and many other industries for that matter - have replaced the Waterfall model with Agile development, where the product (i.e. software system) is only partially designed up-front and then implemented, tested and deployed in small iterations. However, that doesn't mean the V-model has become an old relic. In fact, it's as relevant as ever: it's just that the process it describes - going from global design towards detailed design and then from implementing and testing individual functions and features towards integrating and testing the whole system - is now followed for each feature and/or iteration. For example, if you are using SCRUM, you may recognize the following steps of the process:

  1. Define Epics and Product Backlog Items (Global Requirements & Specifications)
  2. Refine these Epic and PBI's (High Level Design)
  3. Break these PBI's down into Tasks, sketch some diagrams, think about how to implement it (Detailed Design)
  4. Pick up a Task, start coding and testing with Unit Tests (Coding & Unit Testing)
  5. While completing all Tasks and the entire PBI, add or execute some Integration Tests (Integration Testing)
  6. Build and deploy all your changes to a Development or Test Environment, where you perform some System Level Tests (System Level Testing)
  7. Deploy the entire system (or at least all things that were changed) to an Acceptance Environment where a Product Owner and possibly some other Stakeholders will perform User Acceptance Tests to verify if the team delivered the expected product. (User Acceptance Testing)
  8. Release the new version to Production and go back to 1.

The Test Pyramid

When we zoom in on the right side of the V-model, we see that each phase in the implementation process is accompanied by a different set or type of tests. On the smallest scale, we use Unit Tests to assert that the functions and classes we implement are correct and error-free. These Unit Tests are meant to cover every specific execution path (of the functions they test) so that the risk for bugs and defects on this level is as low as possible. Because we aim for high code coverage and many functions typically need to be tested, we often need to write a lot of Unit Tests. However, Unit Tests mainly focus on testing code and code alone, which ought to make them very fast and cheap to run. Should a class or function have an indirect dependency on, for example, a database, file or service-bus, then we often use a mock or stub to undo this dependency during run-time and to focus the test on the code itself. By doing so, we prevent having to setup and use this infrastructure when executing the Unit Tests.

If we go up one level higher in the hierarchy, we get to the level of Integration Tests. Integration Tests are tests that test large chunks of a MicroService (i.e. individual layers) or the entire service as a whole. In contrast to Unit Tests, Integration Tests can and often will involve the database and other infrastructural components of a service to test whether or not everything is working correctly - as long as these components are still conceptually part of the service. On this level, invocations and communication with other services are still mocked or stubbed.

Because Integration Tests often require developers to (automatically) setup and initialize the infrastructure to test, and using/connecting to this infrastructure (e.g. executing stored procedures) is orders or magnitude slower than invoking local functions, Integration Tests are typically a lot slower and more expensive to run than simple Unit Tests. However, Integration Tests are not required to achieve the same level of code coverage that Unit Tests do. The basic idea is that Integration Tests can rely on the Unit Tests for correctness of the small parts and focus their attention on the connective tissue between them. In addition, there are much less 'large chunks' than 'small chunks' to test, so you'll often implement less Integration Tests than Unit Tests.

When we get to the level of System Tests and User Acceptance Tests, testing becomes even more slow and expensive. System Level tests are the first type of tests that focus on the integration of different services working together, so they require all services to be deployed to a specific environment. In other words, whereas Unit Tests and Integration Tests are executed continuously during the development process, either on a local development machine or as part of the build, System Level and User Acceptance Tests are executed on a fully operational system. This makes them use much more resources and makes them slower and thus much more expensive to run. As a matter of fact, many tests on this level are manual tests, requiring people to click through an application, enter data and report on what they've found (either good or bad), which is obviously more time-consuming and expensive to do than running a bunch of automated tests.

Because both Integration Tests and Unit Tests are implemented to ensure that each service is functionally sound and complete, System Tests typically focus more on verifying that all services are deployed and working together correctly - Do they exchange messages correctly? Does the entire Ordering Process work from A to Z?

User Acceptance Tests, on the other hand, are often used to verify whether or not the system is easy to understand and meets the needs of these users (i.e. asserting the systems meets the goals it was designed for). These tests are, of course, essential to the success and adoption of the system, but are also the slowest and most expensive to perform.

By now you may have noticed that there is a clear pattern here: we start implementation and testing on the lowest scale where we write and execute many but fast and cheap Unit Tests. As we work our way up the levels of integration, tests become slower and more expensive. But, with each step, we also typically see a decline in the amount of tests to execute, because each layer of tests can rely and build upon the (results of the) tests on the lower levels, focusing on different traits of the system:

Test Pyramid

Source: martinfowler.com

This pattern can be visualized by the so-called Test Pyramid as depicted above. The broad base of this pyramid reflects the lowest scale, where the Unit Tests are implemented. The pyramid elegantly visualizes the amount of tests that are typically required and/or implemented on each level and also how each new layer of tests builds upon its lower layers. In addition, this specific Test Pyramid of Fowler also indicates the relative cost and execution speeds of tests when you go up or down the hierarchy, grasping all important elements and aspects of testing on different levels in one diagram.

Note that the Integration Tests are depicted as the Service level in the figure and that the System Level and User Acceptance Tests are represented by the UI tests.

Building the Pyramid: one layer at a time

When discussing the V-model, we pointed out that during the design phase of the software development process, design starts out at the highest level and is then slowly decomposed into smaller and more concrete requirements for the smallest components in the system. When implementing and testing the software, we take the opposite route, starting with individual classes and methods, assembling them into individual layers and components of a service, and working our way back up to the system level where, eventually, the systems gets tested by end-users and is released to production.

This pattern, where you start at the base of the pyramid (i.e. the lowest level) and work your way up as you stack individual layers and components, is at the heart of our proposed development process. It is reflected in the V-model (from Unit Tests to User Acceptance Tests), the Test Pyramid we discussed (from Unit Tests to UI Tests), and in the structure of each MicroService, represented by the Onion Architecture:

Implementation and Test Direction

Recall that the Onion Architecture is a layered model, where the Business Logic Layer remains at the center, wrapped by the Data Access Layer, then the Endpoint Layer and finally the Presentation Layer, with all dependencies pointing inwards. When implementing a MicroService, it is only natural to start at the center (the Business Logic Layer) and work your way towards the outer layers. As you progress and implement each layer, you will also test that layer, but each layer will build upon its lower layers - not just functionally, but also in the way we test.

The advantage of this approach is that the tests we execute for each layer will be focused specifically on the responsibilities of that layer, minimizing the overlap between low-level tests and high-level tests. In addition, we can build our low-level tests in such way that we can actually (re)use their code as a foundation for the high-level tests, upgrading our tests to first-class citizens of our code base.

Now let's get started with the development process as we envisioned it.

Clone this wiki locally