Extracting a Microservice from a Monolith: Start with the Monolith
Start with the Monolith
That’s right, forget about actually implementing the new Microservice. We’ll get to that later.
Refactoring legacy code is an inescapable reality
It’s tempting to jump straight into implementing the new Microservice, but this is a mistake.
I understand the temptation to start with the new Microservice. This is the fun part after all. You get to write new code from scratch! It’s a developer’s dream. You’re enamored with the idea of writing the cleanest code you’ve written thus far, with excellent automated test coverage, completely decoupled from the shackles of legacy code, able to be deployed continuously and independently. But you forgot one thing:
It won’t provide any value if you can’t plug it into the legacy system
How much value do you get out of a flawlessly executed Microservice that you can’t actually use? None.
You might have thought that creating the new Microservice first will save you from having to do much refactoring of the legacy code. You thought wrong.
Your new Microservice will almost certainly have an API that isn’t compatible with the existing structure of the legacy code which needs to call it. Something’s gotta give.
You have two options:
- Change the new Microservice so that its API is compatible with the legacy system
- Refactor the legacy system so that it’s compatible with the API of the new Microservice
If option 1 sounds insane, that’s because it is.
If you do this, you’ve failed to achieve any meaningful decoupling. Changes to the behavior of the Microservice will necessarily force changes to the legacy system — an exercise which you sought so very much to avoid by starting with the Microservice — and worse, force the deployments of both to be carefully orchestrated. Essentially all you’ve done is taken an existing composition of function calls in your legacy system, and made this workflow slower, more expensive, and more difficult to trace as your legacy system behaves exactly how it did before, only now must transfer state across a network boundary via a stateless protocol as you enter and exit functions which used to be entirely in-process.
Don’t corrupt your new design with the existing design
It’s easy to see that option 2 is the correct answer, but if you implement the new Microservice first because you’re intentionally avoiding the pain of refactoring the legacy system, there’s yet another more subtle risk: plugging into the legacy system is going to be on your mind while doing so, and that’s going to necessarily steer the design of your new Microservice into a shape that supports the legacy system at the API level. You’ve just picked the insane option 1 without even meaning to.
Bring the pain to the front
This is a guiding principle of software development: if it hurts, do it more often.
Refactoring legacy code is one of the most painful tasks for any developer, but that’s precisely why it’s so valuable for achieving your goals that you set out to accomplish with this Microservice initiative in the first place.
You can’t deploy services independently that are structurally coupled together by having APIs designed in tandem with knowledge of each other’s implementation details.
Your service calls are going to be necessarily inefficient if you don’t properly wrangle the state inside the Monolith before calling the Microservice. Remember, HTTP (and in fact all external communications methods in software I’ve seen used for service communication thus far) are stateless protocols. That is, your Microservice won’t have the session state from the Monolith, it won’t have the memory, the stack, the heap, or even the database access from the Monolith either. Whatever state from the Monolith that the Microservice must have, must be transmitted over the wire, or your Microservice will necessarily be querying for this information, adding even more inefficiencies to your service communication.
Wrapping up, for now
The simple heuristic to be gleaned from this advice is as follows: do not create an external Microservice until such a time as your Monolith has been refactored to appear as if it’s already calling that Microservice.
I’ll cover the ins-and-outs of achieving this heuristic in more depth in future posts, but for now, here are things to do which strongly signal passage:
- Create a module within your Monolith the encapsulates all the behavior of the as-of-yet non-existent Microservice
- This new module should not be able to read application state from the Monolith (session, stack, heap, DB) unless this state is passed in via the API
- Add a referential boundary between this new module and the rest of the Monolith so that the new module can be deployed independently via assembly deployment (even if you won’t actually deploy assemblies related to your Monolith independently, the ability to do that is what we’re after)
- Invert the dependency between this new module and the Monolith by having the Monolith define its own API for communicating with the new module (the “anti-corruption layer” pattern)
Pingback: pudbiascan.strikingly.com
Pingback: https://www.clubsandwiched.com/community/account/sagasdg/
Pingback: https://pastelink.net/ii18z6qf
Pingback: https://joshbond.co.uk/community/profile/shippingtousa/
Pingback: shippingtousa.mystrikingly.com
Pingback: https://pharmaciesshipping.wordpress.com/2023/05/15/canadian-pharmaceuticals-online-with-no-prescription/
Pingback: http://trommelforum.ch/forum/profile/franbervage/
Pingback: https://www.horreur.club/community/profile/canadianpharmacy/
Pingback: https://essidi.cm/community/profile/canadianpharmacy/
Pingback: canadian viagra
Pingback: https://abusetalk.co.uk/forum/profile/canadianpharmacy/
Pingback: canadian pharmacys
Pingback: warriorfarm.co.ukcommunityprofile153413
Pingback: ascenddeals.combeaverageprofilecanadianpharmacy
Pingback: baldstyled.comcommunityprofilecanadianpharmacy
Pingback: www.careerstek.comforumprofilecanadianpharmacy
Pingback: chanchuoi.comcommunityprofilecanadianpharmacy
Pingback: buyviagraonlinet.com
Pingback: viagra 50 mg online
Pingback: college essay help
Pingback: essay writing service legit
Pingback: best custom essays
Pingback: compare and contrast essay help
Pingback: easy essay writer
Pingback: automatic essay writer
Pingback: essay writing help online
Pingback: the help essays
Pingback: where can i buy essays online
Pingback: help writing essays for scholarships
Pingback: college essay editing service
Pingback: services essay
Pingback: cheapest essay writers
Pingback: essay help writing
Pingback: pharmacy escrow viagra
Pingback: pharmacy methotrexate error
Pingback: florida pharmacy law codeine
Pingback: ativan no prescription online pharmacy
Pingback: Prilosec
Pingback: what is tadalafil tablet
Pingback: viagra price without insurance
Pingback: tadalafil side effects long term
Pingback: buy generic tadalafil
Pingback: buy cialis black au
Pingback: sildenafil otc nz
Pingback: cheap sildenafil tablets
Pingback: generic cialis no prescription
Pingback: online pharmacy tramadol no prescription
Pingback: generic cialis online pharmacy
Pingback: viagra online safe
Pingback: where can i buy cheap viagra
Pingback: buy viagra online paypal
Pingback: centurion laboratories tadalafil
Pingback: is sulfamethoxazole the same as amoxicillin
Pingback: valtrex worse
Pingback: lisinopril dmd
Pingback: rybelsus 28 mg
Pingback: semaglutide label
Pingback: equivalencia rybelsus ozempic
Pingback: zoloft taper
Pingback: cephalexin 500 side effects
Pingback: buy cymbalta
Pingback: how long does it take for lexapro to work for anxiety
Pingback: bactrim antibiotic class
Pingback: bactrim for kidney infection
Pingback: neurontin bipolar depression
Pingback: diltiazem side effects
Pingback: can you take flexeril with ibuprofen
Pingback: simvastatin with or without ezetimibe in familial hypercholesterolemia
Pingback: ddavp iv compatibility
Pingback: cozaar vs diovan
Pingback: augmentin price
Pingback: what happens if i stop taking allopurinol
Pingback: ibuprofen and aspirin
Pingback: can you get high off celebrex 200 mg
Pingback: kamagra lactose
Pingback: amitriptyline alternatives
Pingback: repaglinide and glyburide
Pingback: semaglutide 30 units
Pingback: remeron 15 mg for sleep
Pingback: protonix vs omeprazole
Pingback: abilify for depression
Pingback: online pharmacy valtrex no prescription
Pingback: buy viagra cialis levitra online
Pingback: price of ivermectin liquid
Pingback: snovitra 20 mg vardenafil
Pingback: ivermectin 5ml
Pingback: stromectol tab 3mg
Pingback: best prices for tadalafil 20mg
Pingback: amoxil polymox trimox
Pingback: glucophage 850g
Pingback: modafinil provigil cephalon
Pingback: doxycycline dosage for std