1 Aug 2022 · Software Engineering

    When Microservices Are a Bad Idea

    9 min read
    Contents

    On paper, microservices sound wonderful. They are modular, scalable, and fault tolerant. A lot of companies have had great success using this model, so microservices might naturally seem to be the superior architecture and the best way to start new applications.

    However, most firms that have succeeded with microservices did not begin with them. Consider the examples of Airbnb and Twitter, which went the microservice route after outgrowing their monoliths and are now battling its complexities. Even successful companies that use microservices appear to still be figuring out the best way to make them work. It is evident that microservices come with their share of tradeoffs.

    Migrating from a monolith to microservices is also not a simple task, and creating an untested product as a new microservice is even more complicated. Microservices should only be seriously considered after evaluating the alternative paths.

    Microservices are only viable for mature products

    On the topic of starting from a microservice design, Martin Fowler observed that:

    1. Almost all the successful microservice stories started with a monolith that got too big and was broken up.

    2. Almost all the cases where a system that was built as a microservice system from scratch, ended up in serious trouble.

    This pattern has led many to argue that you shouldn’t start a new project with microservices, even if you’re sure your application will be big enough to make it worthwhile.

    The first design is rarely fully optimized. The first few iterations of any new product are spent finding what users really need. Therefore, success hinges on staying agile and being able to quickly improve, redesign, and refactor. In this regard, microservices are manifestly worse than a monolith. If you don’t nail the initial design, you’re in for a rough start, as it’s much harder to refactor a microservice than a monolith.

    Are you a startup or working on a greenfield project?

    As a startup (not likely in this economy), you already are running against the clock, looking for a breakthrough before the next bad thing happens. You don’t need the scalability at this point (and probably not for a few years yet), so why ignore your customers by using a complicated architecture model?

    A similar argument can be made when working on greenfield projects, which are unconstrained by earlier work and hence have nothing upon which to base decisions. Sam Newman, author of Building Microservices: Designing Fine-Grained Systems, stated that it is very difficult to build a greenfield project with microservices:

    I remain convinced that it is much easier to partition an existing “brownfield” system than to do so upfront with a new, greenfield system. You have more to work with. You have code you can examine, you can speak to people who use and maintain the system. You also know what ‘good’ looks like – you have a working system to change, making it easier for you to know when you may have got something wrong or been too aggressive in your decision-making process.

    Microservices aren’t the best for on-premise

    Microservice deployments need robust automation because of all the moving parts. Under normal circumstances, we can rely on continuous deployment pipelines for the job–Developers deploy the microservices and customers just use the application online.

    This won’t fly for on-premise applications, where developers publish a package and it’s up to the customer to manually deploy and configure everything on their own on their private systems. Microservices make all these tasks especially challenging, so this is a release model that does not fit nicely with microservice architecture.

    To be clear, developing an on-premise microservice application is entirely viable. Semaphore is accomplishing just that with Semaphore On-Premise. However, as we realized along the way, there are several challenges to overcome. Consider the following before deciding to adopt microservices on-premise:

    • Versioning rules for on-premise microservices are more stringent. You must track each individual microservice that participates in a release.
    • You must carry out thorough integration and end-to-end testing, as you can’t test in production.
    • Troubleshooting a microservice application is substantially more difficult without direct access to the production environment.

    Your monolith might have life left

    Every piece of software has a lifecycle. You might be tempted to scrap a monolith because it’s old and has its share of complications. But tossing a working product is wasteful. With a little effort, you might be able to squeeze a few more good years out of your current system.

    There are two moments when it might seem that microservices are the only way forward:

    • Tangled codebase: it’s hard to make changes and add features without breaking other functionality.

    Performance: you’re having trouble scaling the monolith.

    Modularizing the monolith

    A common reason developers want to avoid monoliths is their proclivity to deteriorate into a tangle of code. It’s challenging to add new features when we get to this point since everything is interconnected.

    But a monolith does not have to be a mess. Take the example of Shopify: with over 3 million lines of code, theirs is one of the largest Rails monoliths in the world. At one point, the system grew so large it caused much grief to developers:

    The application was extremely fragile with new code having unexpected repercussions. Making a seemingly innocuous change could trigger a cascade of unrelated test failures. For example, if the code that calculates our shipping rate is called into the code that calculates tax rates, then making changes to how we calculate tax rates could affect the outcome of shipping rate calculations, but it might not be obvious why. This was a result of high coupling and a lack of boundaries, which also resulted in tests that were difficult to write, and very slow to run on CI.

    Instead of rewriting their entire monolith as microservices, Shopify chose modularization as the solution.

    Diagram showing two axes. The vertical axis reads the level of modularization. The horizontal axis reads the number of deployable units. Four quadrants are shown. Near the zero on both axes sits a layered monolith. Upwards on the vertical axis, there is a modularized monolith. On the far right and top sit microservices. On the bottom right, there is a distributed monolith.
    Modularization helps design better monoliths and microservices. Without carefully defined modules, we either fall into the traditional layered monolith (the big ball of mud) or, even worse, as a distributed monolith, which combines the worst features of monoliths and microservices.

    Modularization is a lot of work, that’s true. But it also adds a ton of value because it makes development more straightforward. New developers do not have to know the whole application before they can start making changes. They only need to be familiar with one module at a time. Modularity makes a large monolith feel small.

    Modularization is a required step before transitioning to microservices, and it may be a better solution than microservices. The modular monolith, like in microservices, solves the tangled codebase problem by splitting the code into independent modules. Unlike with microservices, where communication happens over a network, the modules in the monolith communicate over internal API calls.

    Layered vs modularized monolith. The left picture shows a classic MVC layered monolith. On the right side, it has been modularized. Instead of layers, we have vertical blocks representing the Accounts, Orders, and Products modules.
    Layered vs modular monoliths. Modularized monoliths share many of the characteristics of microservice architecture sans the most difficult challenges.

    Monoliths can scale

    Another misconception about monoliths is that they can’t scale. If you’re experiencing performance issues and think that microservices are the only way out, think again. Shopify has shown us that sound engineering can make a monolith on work on a mind-boggling scale:

    The architecture and technology stack will determine how the monolith can be optimized; a process that almost invariably will start with modularization and can leverage cloud technologies for scaling:

    • Deploying multiple instances of the monolith and using load balancing to distribute the traffic.
    • Distributing static assets and frontend code using CDNs.
    • Using caching to reduce the load on the database.
    • Implementing high-demand features with edge computing or serverless functions.

    If it’s working, don’t fix it

    If we measure productivity as the number of value-adding features implemented over time, then it follows that switching architecture makes little sense while productivity is strong.

    A two-axis diagram showing two lines, one for monolith and the other for microservices. The X axis is time, and the Y axis is productivity. Initially, the monolith is more productive than microservices. Over time, monolith productivity decreases faster than a monolith, making the lines cross at some middle point. On the right side, microservices are more productive.
    Microservices are initially the less productive architecture due to maintenance overhead. As the monolith grows, it gets more complex, and it’s harder to add new features. Microservice only pays off after the lines cross.

    True, something will have to change eventually. But that could be years from now, and by then, requirements may have changed — and who knows what new architecture models may emerge in the meantime?

    Brooke’s Law and developer productivity

    In The Mythical Man Month (1975), Fred Brook Jr stated that “adding manpower to a late software project makes it later”. This happens because new developers must be mentored before they can work on a complex codebase. Also, as the team grows, the communication overhead increases. It’s harder to get organized and make decisions.

    A two-axis diagram is shown. The vertical axis is time, and the horizontal axis is the number of developers. The curve starts high on time, and as the number of developers increases, the curve goes down, making the project take less time to complete. At some point, the tendency changes, and the curve increases exponentially. The more people that are added, the longer the project takes. The inflection point is the optimal team size.
    Brook’s law applied to complex software development states that adding more developers to a late software project only makes it take longer.

    Microservices are one method of reducing the impact of Brooke’s Law. However, this effect is only visible in complicated and huge codebases where everything is interconnected, because we cannot divide development into discrete tasks in such a scenario.

    Before deciding on using microservices, you must determine if Brooke’s Law is affecting your monolith. Switching to microservices too soon would not add much value.

    Are you ready to transition?

    Some conditions must be met before you can begin working with microservices. Along with preparing your monolith, you’ll need to:

    Changing the culture of an organization can take years. Learning all that there is to know will take months. Without preparation, transitioning to microservices is unlikely to succeed.

    Conclusion

    We can summarize this whole discussion about transitioning to microservices in one sentence: don’t do it unless you have a good reason. Companies that embark on the journey to microservices unprepared and without a solid design will have a very tough time of it. You need to achieve a critical mass of engineering culture and scaling know-how before microservices should be considered as an option.

    In the meantime, why change if your system is performing well and you’re still developing features at a decent pace?

    Thanks for reading, and happy coding!

    Leave a Reply

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

    Avatar
    Writen by:
    I picked up most of my skills during the years I worked at IBM. Was a DBA, developer, and cloud engineer for a time. After that, I went into freelancing, where I found the passion for writing. Now, I'm a full-time writer at Semaphore.
    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.