17 Mar 2023 · Software Engineering

    Microservices Best Practices

    11 min read
    Contents

    The Microservices Architecture is an evolved pattern that has fundamentally changed the way server-side code is developed and managed. This architectural pattern involves the design and development of the application as a collection of loosely-coupled services that interact over well-defined, lightweight APIs to meet business requirements. It aims to help software development firms to accelerate the development process by facilitating continuous delivery and development.

    If we talk about its elemental level, a particular microservice acts as an application in and of itself that forms a larger application with other microservices; this enables:

    • Easier and faster development
    • Maintainability
    • Scalability

    Essentially, this allows you to manage and maintain an application more effectively. There is, however, specific complexity inherent to this pattern, which can be mitigated by using certain best practices.

    We all know that microservice design has a direct impact on the network resilience of modern architectures. When businesses decide to build using microservices, it’s important to develop them efficiently and effectively so that they can operate over the network, without causing excess latency, bandwidth consumption, and packet loss.

    In this article, we’re going to discuss the basic microservices best practices that you should consider if you want to achieve an efficient microservices ecosystem devoid of extreme architectural complexities. So, without any further ado, let’s get started.

    Adopt the Single Responsibility Principle

    The Single Responsibility Principle is the concept that any single object in OOP should be made for one specific function. Basically, it is part of the programming principles put forth by Robert Martin. Just like with code, a class should have only one reason to change, making the software more maintainable, scalable, and easier to understand. To adopt SRP in software development, you should ensure that each class or module has a well-defined responsibility and that it is not trying to do too many things. You should also keep your modules decoupled and use clear and concise interfaces to communicate between them. To summarize this, we have an interesting quote: 

    Gather together those things that change for the same reason, and separate those things that change for different reasons.” – O’reilly

    We can say that this is one of the finest and most essential principles to building a good architectural design as it signifies that a microservice, module, class, subsystem, or function should not have multiple reasons to change.

    Let’s understand this Principle with an example:

    An eCommerce portal might have a microservices architecture like this:

    Here, all the services (e.g. Product Listing Service, Order Service, Customer Service, Payment Service, Cart Service, Wishlist Service, etc.) have single responsibilities. This means that it is important to make sure that you’re not integrating one service with another when it is not absolutely necessary, as it makes the architecture more complicated to maintain and test.

    Build teams with clear responsibilities

    To develop the microservices architecture, we need to build teams that have clear responsibilities. This can be done in multiple ways, like role-based teams, cross-functional teams, etc. In this architecture, each microservice works as an independent application. So, each team should be versatile enough to handle their operations.

    Let’s understand this with an example:

    An organization can have role-based teams like UI/UX developers, front-end developers, back-end developers, database admins, QAs, middleware developers, etc. that work in isolation but interact on a daily basis via meetings–either in person or using various communication tools like JIRA, Slack, and so on.

    When we think about maintenance, sometimes small bugs or sometimes even large bugs can also occur in the system. So, SCRUM can be a possible solution. It helps each team member to shorten the time of unawareness. But as the teams are organized based on the roles, integration of one update within one sprint can become a complicated task. For example, if UI/UX developers don’t get any information from the server guys about changes in an API, the new API won’t be useful at all.

    So what’s the solution?

    Build cross-functional teams with clear responsibilities, to help orchestrate work between teams

    One cross-functional team that is responsible for the whole microservices functionality can be a major benefit to your project. This team should consist of members from all of the role-based teams and is responsible for orchestrating the various parts of the application, i.e. UI, development, database, and even QA. If there are two versions of the application, i.e. web and mobile, then developers from both the teams should be present in this team. The key benefit of this kind of team is that it becomes easy to solve bugs, develop new features and deploy them in the production environment.

    Use the right tools and frameworks

    By this point, you have probably designed your microservices to deploy them independently, now you must realize the optimal value of those microservices. And to do that, you need to automate build and deployment management using a good set of DevOps tools.

    Using the right tools, frameworks, and libraries will go a long way towards implementing a microservice architecture. If you’re planning on doing this in Java, consider the Spring Boot Project. Choosing the right tools and frameworks takes a lot of time and effort so here’s a list of “go-to,” proven tools and technologies for the job:

    1. Jenkins and Bamboo for deployment automation
    2. Docker for containerization
    3. Postman for API testing
    4. Kubernetes for container orchestration and deployment
    5. Logstash for monitoring
    6. DevSecOps to manage the entire process of the software development lifecycle
    7. GitHub for source code management and version control
    8. Amazon’s simple queue service for messaging
    9. SonarQube to check the code quality and security
    10. Ansible for managing your configuration
    11. Jira for issue tracking and project management

    Keep asynchronous communication between microservices

    Two types of communications occur between microservices: Synchronous and Asynchronous. Let’s understand this with an example:

    For an ecommerce platform, Synchronous communication means that the user will be required to “stay on the line” and advance through a series of steps (select items, add delivery address, payment details, order verification) ultimately resulting in the customer notification “Thank you for your order! We’re delivering it next week”.

    There are several async communications that also occur once the customer notification is processed that are part of the “fulfillment” stage of the order like: warehouse notification, inventory update, etc.

    In the case of synchronous communication, one service becomes dependent on another service. Sometimes, it becomes a time consuming process to complete the entire task using synchronous communication between multiple microservices.

    On the other hand, async communications are not dependent on one another. So, every service can take its time to complete its task. Therefore, one should try to maximize async communication between microservices wherever possible. It reduces dependency and increases the overall efficiency of an application.

    You can see an example of this below:

    Adopt the DevSecOps model and secure microservices

    Security is very important in this architecture. As microservices architecture has evolved in developing cloud native applications, DevSecOps practices are increasingly used to ensure continuous integration and continuous delivery with increased security measures. An application build using microservices can be divided into the following code types:

    1. Application code (core logic)
    2. Application service code (network connections, session establishment, etc.)
    3. Infrastructure (data storage resources, networking, platforms, etc.)
    4. Monitoring (continuous observability of the application)

    DevSecOps consists of three concepts: development, security, and operations, and has proven to be a facilitating paradigm for code types with primitives such as continuous integration, continuous delivery, and continuous deployment pipelines. These pipelines are workflows for using developers’ source code for developing, testing, deploying, and many such operations that are supported by automated tools with feedback mechanisms. Also, it makes development teams deliver better, more-secure code faster. DevSecOps practices in microservices architecture provide numerous benefits such as:

    • High security assurance
    • Reduction in code vulnerability
    • Enhanced product quality
    • Increased productivity
    • Increased speed of the operations
    • Deliver better and higher quality software faster

    Use a separate data store for each microservice

    One important practice is to make sure to have a separate database to store data whenever possible, rather than having the same database for multiple microservices such as in a monolithic architecture. However, a more in-depth analysis might indicate that one microservice only works with a subset of database tables whereas, on the other hand, another microservice only works with an entirely new subset of tables. And if both subsets of data are orthogonal, this would be a case for separating the database into separate services. Therefore, make sure to have a separate data store for your microservices to reduce latency and improve security. This has been mentioned many times already, but it is important to emphasize that microservices should depend on each other as little as possible.

    One of the main attributes of microservice architecture is that each service’s data is private, as is the case, for instance, with the Database per Service pattern.

    We can also use a shared database server that can be used by multiple services with a logical separation of their data.

    Deploy each microservice separately

    If you’re deploying each microservice separately, you’re surely going to save a lot of time coordinating with multiple teams while maintaining or upgrading efforts. Also, if one or more microservices have the same resources, we recommend you use a dedicated infrastructure to isolate each microservice from faults and avoid a full-blown outage.

    Some of the most common and popular patterns for deploying microservices are:

    1. Multiple service instances per host
    2. Service instance per container
    3. Single service instance per host
    4. Service instance per VM

    Orchestrating microservices

    Orchestration of your microservices is one of the most influential factors to gain success in both the process and tooling. You can use Docker to run containers on a VM but it won’t provide the same level of resilience that a container orchestration platform offers. Such a decision may very well negatively affect your uptime while trying to adopt a microservices architecture.

    Here are some of the orchestrating platforms that are proven:

    • K8s (Kubernetes)
    • AKS (Azure Kubernetes Services)
    • ECS (Amazon Elastic Container Services)
    • Azure Container Apps

    These platforms can be helpful in managing container provisioning & deployment, load balancing, scaling, network communication concerns, etc.

    Use an effective monitoring system

    The microservices architecture helps you to carry out enormous scaling of thousands of modular services and offers the potential for improved speed and organized methods of monitoring. It is important, however, to make sure to review all your microservices and regularly check if they are functioning as desired and are efficiently using the available resources. Depending on these observations, you can take appropriate actions if expectations are not being met.

    Let’s analyze an example situation and imagine that you have applied a microservices architecture pattern which does not have the ability to handle requests, but they are still running. For example, if it runs out of database connections, the monitoring system should be capable of generating an alert whenever an instance fails and requests should be routed to working service instances.

    Monitoring microservices, and keeping these stats explained precisely will help you to improve decision-making and keep your microservices available when needed.

    Let’s have a look at a few examples of microservices monitoring tools.

    AWS CloudWatch: a monitoring, observability, and management service that collects and visualizes real-time logs and provides actionable insights for AWS, hybrid, and on-premises applications and infrastructure resources.

    Jaeger: software designed to monitor and troubleshoot complex problems in a microservice environment.

    Datagod: an observability, security, and analytics platform for cloud-scale applications that provides a comprehensive solution for databases, services, and tools using an SaaS-based data analytics platform.

    Graphite: as the name suggests, it is open-source software that monitors and graphs numeric time-series data and provides in-depth insights into the underlying system.

    Prometheus: a free and open-source software tool that offers monitoring and modification solutions.

    Conclusion

    So that’s it for this post. I hope that you found this reading useful and that you’ll follow these microservices’ best practices to end up with an independent, loosely-coupled system, in order to reap the benefits of this architecture.

    6 thoughts on “Microservices Best Practices

    1. Good article, looking forward of having more insights on microservices, kubernetes, dockers and other dependent tech stack and frameworks.

    2. Having lived with some large scale micro services architectures, I’m curious what you think about the independent data store issue and deletes. In your example, if I have to delete a customer record and I have independent data stores, I can’t rely on database guarantees for cascading deletes. Sync or async, I suddenly have a lot of (IMHO complex) code and infra for *guaranteed* deletion across the system to clean up delivery addresses, payment info, wishlists, stranded carts. A shared database, albeit without any shared tables, provides many delete guarantees across primary/foreign key relationships. Isn’t this much easier for most teams to manage?

    3. Good article, little bit irritating you right away talk about API testing instead of Contract Testing (Pact.io). API Testing is only necessary for smoke testing a deployment/environment if you have contract testing in place. And even then API Testing can be done by an Readiness and Liveness probe, because you’re already on a k8s/AKS/ACA level with your hosts.

    4. Thank you for sharing the article on microservices best practices! It’s truly insightful and highlights some key strategies for successfully implementing microservices architecture. I appreciate how the article emphasizes the importance of loose coupling, scalability, and fault tolerance while also addressing challenges like inter-service communication and data management. The practical tips and real-world examples provided make it easy to grasp the concepts and apply them in a meaningful way. Overall, this article is a valuable resource for anyone interested in maximizing the benefits of microservices. Kudos to the authors for delivering such a positive and informative piece!
      https://www.heliossolutions.co/

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Avatar
    Writen by:
    Vikash Kumar works at a software development company – TatvaSoft. He has a rich experience in digital content publications and you will find his many by-lines published in many reputed publications. He has a strong expertise in content collaboration and works along side with technical team to align the relevant content for the audience.
    Avatar
    Reviewed by:
    I picked up most of my soft/hardware troubleshooting skills in the US Army. A decade of Java development drove me to operations, scaling infrastructure to cope with the thundering herd. Engineering coach and CTO of Teleclinic.