{"id":13370,"date":"2018-05-14T15:26:50","date_gmt":"2018-05-14T13:26:50","guid":{"rendered":"https:\/\/touk.pl\/blog\/?p=13370"},"modified":"2022-08-02T13:07:02","modified_gmt":"2022-08-02T11:07:02","slug":"when-distributed-monolith-may-work-for-you","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2018\/05\/14\/when-distributed-monolith-may-work-for-you\/","title":{"rendered":"When distributed monolith may work for you"},"content":{"rendered":"<p>The Distributed Monolith term surely has a bad press. When you read through blogs and conference talks I\u2019m sure you\u2019d better build a \u201ctraditional\u201d monolith rather that a distributed one. If you go with the latter, it means you tried to build microservices but you failed. And now your life is sad and full of pain ;) In this post I\u2019d like to describe our case, when we built something like distributed monolith intentionally, with quite a pleasant final experience.<\/p>\n<p>In my opinion the key to microservices is in the <strong>Conway\u2019s Law<\/strong>. Looking from that perspective you\u2019d think that primary use-case for microservices is <strong>large organizations<\/strong> with <strong>in-house IT department<\/strong> with <strong>many small teams<\/strong>, working <strong>independently<\/strong>, with <strong>large control<\/strong> over their part ot the system. But this doesn\u2019t cover the full spectrum of software development market ;)<\/p>\n<p>When you are a software house you deal with various type of projects. This includes maintenance, rewrite part of a large system and &#8211; last but not least &#8211; developing new systems from scratch. But the development process is much different than in-house IT: we use small, versatile teams (up to 10 people), not tight stick to roles like webdev, tester (developers, not programmers). There are no two identical projects for us: every customer has its own needs, tools, infrastructure, procedures, etc. (see <a href=\"http:\/\/ericsink.com\/No_Programmers.html\">http:\/\/ericsink.com\/No_Programmers.html<\/a>)<\/p>\n<p>If you plan your system architecture you often try to do <a href=\"https:\/\/www.simplethread.com\/the-distributed-monolith\/\">some checklist that can help you make the best choice<\/a>. I think the most interesting question from it is:<\/p>\n<blockquote><p>Is there a part of the system that a single person, or small team, operates independently inside of?<\/p><\/blockquote>\n<p>The usual response for a software house is <em>\u201cHmm\u2026 No\u201d<\/em>. So are we forced to always build monoliths?<\/p>\n<h1 id=\"the-good-parts-of-monolith\">The good parts of monolith<\/h1>\n<p>Let\u2019s think first what could be the good parts of monolithic architecture, especially for a small team inside a software house:<\/p>\n<h3 id=\"simple-communication-model\">Simple communication model<\/h3>\n<p>When you want to deliver fast, you have to reduce overhead. If your customer ask for a feature, telling him that you first have to investigate how to make your microservices talk to each other could be not acceptable. Also, having less service-to-service communication, makes monitoring and debugging in production a lot easier. It\u2019s different when you build a large microservice stack for a big organization &#8211; you surely convinced business owners that you need some time to invest in tools and infrastructure and they have to wait for it to be done before features arrive ;)<\/p>\n<h3 id=\"single-repository\">Single repository<\/h3>\n<p>At software houses, we usually have a lot of freedom choosing CI\/CD tooling, some team go with full-stack Gitlab, some prefer Gerrit + Jenkins, some work on customer\u2019s infrastructure. We don\u2019t have repeatable, complicated build pipeline for every project &#8211; usually just running tests, make some static code analysis (with Sputnik), sometimes push Docker images somewhere. It\u2019s just easy to have just one repository, not having to setup multiple independently.<\/p>\n<h3 id=\"single-data-source\">Single data source<\/h3>\n<p>This is also not constant across projects, but most of them need just one data source, possibly just RDBMS (Postgres) to store business data. It\u2019s easy to administer, manage, migrate. If you want a bit less coupling between parts of the system, separate database schemas for a start could be enough.<\/p>\n<h3 id=\"simple-deployment-infrastructure\">Simple deployment infrastructure<\/h3>\n<p>Just like above, from deployment perspective, the simplest &#8211; the better. Your customer doesn\u2019t want you to spend 3 weeks setting up a Kubernetes cluster. If you can go with plain Docker without dedicated orchestration &#8211; go with it. For us &#8211; <a href=\"https:\/\/aws.amazon.com\/ecs\">AWS ECS<\/a> with some home-made automation worked really fine.<\/p>\n<h1 id=\"the-good-parts-of-microservices\">The good parts of microservices<\/h1>\n<p>On the other hand, while building monolith may have many good parts, it can also be really painful. I think that there are numerous advantages you\u2019ll get from splitting the whole system into few smaller parts. The key is having multiple deployment units, which really gives you benefits:<\/p>\n<h2 id=\"resource-separation\">Resource separation<\/h2>\n<p>Bugs happen. It is obvious that your system will someday fail on production. The real question is what happens then? How your system deals with errors? If you use shared resources like database or http client connection pools, one, even not critical part of your system, may cause a full system outage. That\u2019s why splitting your system into few smaller applications matters. You (and your customers) can probably live even if one of the minor functionalities fails.<\/p>\n<h2 id=\"independent-scaling\">Independent scaling<\/h2>\n<p>It is also good when your system can scale. It\u2019s better when you can scale parts of your system independently. Some of features are massively used and business critical &#8211; and you want to run them in high-available mode with possibility to add instances on the fly when traffic increases. But also you have some other parts (e.g. backoffice) that is not heavily used with often zero traffic after working hours. With multiple applications instead of monolith it\u2019s easier to achieve independent scaling.<\/p>\n<h2 id=\"network-separation\">Network separation<\/h2>\n<p>Usually parts of a big system are used from different places. You have some public services (e.g. mailing service), othen with both sides communication (with webhooks). On the other hand &#8211; a back-office part shouldn\u2019t be exposed publicly, best hidden in a VPN to a customer\u2019s office. Having these parts separated makes it easier to put them in different networks and then setup a particular policy of each part.<\/p>\n<h2 id=\"separate-configuration\">Separate configuration<\/h2>\n<p>Defining proper authentication configuration for Java (even in Spring) app could be hard. I guess that defining multiple authentication models (e.g. logged users, internal features for call-center people and public service calls) for different parts of a monolithic application could be a hell. Splitting a monolith into several deployment units, each with separate authentication configuration, may save you many hours of debugging magical untestable code provided by your framework.<\/p>\n<h2 id=\"technology-experiments\">Technology experiments<\/h2>\n<p>It\u2019s really bad for a project if you\u2019re stick to a legacy technology. Having thousands of lines written in 90\u2019s Javascript framework could be really demotivating for developers. When you have few smaller deployment units, it\u2019s much easier to play with technology experiments. You can either write the whole small part in some crazy technology (we use Elm for a small frontend piece) or try to upgrade some important library just in one place (we switched to 4.x Redux router in smaller application before upgrading the bigger one).<\/p>\n<h1 id=\"taking-best-from-two-worlds\">Taking best from two worlds<\/h1>\n<p>In our case, we think that we took the best from two approaches. We had small team (in peak 8 people), working on a <strong>single Git repository<\/strong>. We delivered <strong>10 separate applications<\/strong>, all using the <strong>same datasource<\/strong> (Postgres). We used <strong>Kotlin + Spring Boot<\/strong> on backend, and <strong>React<\/strong> for frontend. We used <strong>Gradle modules<\/strong> to split the backend code into reusable parts, and Spring Boot apps just picked the needed pieces.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-13371\" src=\"https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-42-17-1024x386.png\" alt=\"\" width=\"660\" height=\"249\" srcset=\"https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-42-17-1024x386.png 1024w, https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-42-17-300x113.png 300w, https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-42-17-768x290.png 768w, https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-42-17.png 1174w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p>We packed the apps into <strong>Dockers<\/strong>, built with <strong>Gitlab CI pipelines<\/strong> and used <strong>Amazon ECS<\/strong> for container orchestration (with a small API gateway based on <strong>Nginx and Consul<\/strong>).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13372\" src=\"https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-44-23.png\" alt=\"\" width=\"986\" height=\"432\" srcset=\"https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-44-23.png 986w, https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-44-23-300x131.png 300w, https:\/\/touk.pl\/blog\/wp-content\/uploads\/2018\/05\/Zrzut-ekranu-z-2018-05-11-22-44-23-768x336.png 768w\" sizes=\"auto, (max-width: 986px) 100vw, 986px\" \/> There was no inter-process (HTTP etc.) communication between the apps &#8211; we just used <strong>modules API<\/strong> for synchronous calls (mostly queries) and <strong>Quartz tasks<\/strong> for asynchronous commands. We also had <strong>ELK stack<\/strong> for logging and tracing.<\/p>\n<p>To be honest, we were 90% happy. The remaining 10% of things that didn\u2019t work well included:<\/p>\n<ul>\n<li>many small modules &#8211; we had some performance problems with Intellij at the beginning with indexing all the small modules<\/li>\n<li>cyclic dependencies &#8211; sometimes we discovered cycling dependencies between these modules; on the other hand &#8211; it helped us to remove some flaws from our design<\/li>\n<li>tight coupling &#8211; our apps were designed by API (consumer) perspective, rather than clean DDD, bounded context thinking; but separate modules could help with this &#8211; e.g. a new delivery system was developed as separate part, with its own database schema<\/li>\n<li>\u201call-at-once\u201d deployment &#8211; and yes, having just 1 database, we often had to deploy multiple applications at once.<\/li>\n<\/ul>\n<p>Having said that, I think that we managed a good balance between two worlds. We didn\u2019t struggle with massive infrastructure and tooling, working with different repositories and data sources. We were able to go as fast as we built a single-repository monolith application. But we had some parts separated, with their own configuration\/boilerplate, ready to be deployed and scaled independently.<\/p>\n","protected":false},"excerpt":{"rendered":"The Distributed Monolith term surely has a bad press. When you read through blogs and conference talks I\u2019m&hellip;\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[637,549,556,537],"class_list":{"0":"post-13370","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-architecture","8":"tag-docker","9":"tag-kotlin","10":"tag-shell"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=13370"}],"version-history":[{"count":14,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13370\/revisions"}],"predecessor-version":[{"id":14838,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13370\/revisions\/14838"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=13370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=13370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=13370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}