You are here

How to use microservices to cut out spaghetti code

How to use microservices to cut out spaghetti code

Ophir Gross, Chief System Architect, HPE

Service providers today face the difficult challenge of reducing costs and improving agility while adding more products or improving existing ones. That means your software applications have to be adaptive and scalable enough to meet growing customer demands at client locations or in new geographical regions.

If you are providing a service that is used by different clients, you need a strategy that frees you up to make changes to your services without forcing your clients to move to the latest version. This helps ensure that the provider doesn’t break the contract with its clients whenever a change needs to be made. You need to avoid the hassles of spaghetti code.

Buyer’s Guide to Software Test Automation Tools

The risk of monolithic implementations

Architectural design patterns can be used to ensure that services support different versions, but applications developed in the past have generally been monolithic in nature, rigid, and componentized. Any small changes made to any component in an application and its code would have a major impact on productivity, deployment, and time required to sort out the dependencies; this created a constant struggle regarding updates and upgrades of libraries, frameworks, and so on.

Because such applications have to be distributed and easily portable across various platforms and technologies, without customer disruption, developers are left with short turnaround times to make changes.

With typical monolithic implementations, updates have to occur across all applications.  For example, upgrading all applications from Java 7 to Java 8 means having to touch each of the individual applications separately. This becomes a major challenge for development teams.

Avoiding spaghetti code

The other challenge in application development today is avoiding spaghetti code. Spaghetti code is full of checks to see what interface version is being used and to make sure that the right code is executed. It’s often disorganized and usually results in higher maintenance efforts as changes in code affect functionality in areas that are challenging to predict during development stages.

[ Get Report: Gartner Magic Quadrant for Software Test Automation ]

The benefits of a microservices architecture

Migrating your code to a microservices architecture addresses these challenges to a large extent. A microservices architecture breaks down application components into small, manageable services, each running within its own process independently. This makes it easier to change functions and qualities of the system at any time.

The Amazon website, for example, was built with separate scripts for each section of the page—clothing, housewares, shopping cart, etc.—each consisting of hundreds of back-end services and with each application implementing the business logic for a separate function. When you go to and type in "Nike shoe," more than 170 individual applications get triggered, each an individual service connected via HTTP and potentially built in different languages. Building a page in this fashion creates a seamless experience for end users, even if part of the page goes down.

Ownership of the software

One of the primary benefits of developing with microservices is that each team in an enterprise can own and manage the software it’s developing. Microservices lends itself to continuous delivery software development, because a change to a small part of the application only requires one or a small number of services to be redeployed. The services can be designed and developed as completely autonomous entities, with individual services for new applications being reused or integrated with third-party services without affecting other applications. Development teams with a microservices approach are therefore free to build their new enhancements and features in a much more independent, flexible, and adaptive environment.

Technology independence

With a microservices-based architecture, engineers have the freedom to choose their implementation technologies and frameworks. Each microservice can be built by any set of languages or tools, since each is independent from the others and each runs a separate process. This technology independence also means that individual services within an application can be gradually replaced with applications based on more modern technologies—without having to replace the entire application.

Microservices embrace agility

Microservices are independent and embrace agility and rapid application evolution. Development organizations can respond more quickly to customer and market feedback, and releases no longer need to be delayed by the schedule of a single release. This is opposed to a monolithic approach, where everything is tied together and developers are dependent on others—leading to increased time and effort for the development team.

This separation is critical for designing software architecture today. New projects can increase agility by reducing dependencies into design from the beginning. Companies can employ separate, smaller development teams that can develop services to support new products and integrations, without interfering with existing development and operation cycles on existing applications.

Separation leads to fault isolation

This type of separation also enables greater fault isolation. When services break, the application’s overall health is largely unaffected. For example, a crash in one service only affects that service. Other services will continue to handle requests normally. With monolithic architectures, on the other hand, just the opposite occurs: One faulty component can bring down the entire system.

Microservices migration and challenges

Migrating to microservices is not without its challenges. It involves moving to a distributed, decentralized services platform serving many applications and requires a significant cultural and organizational adjustment in order to be executed successfully.

Each task requires automation

Microservices architecture doesn’t enable manual changes. The increasing frequency of updates and number of moving parts require a fully automated workflow and stable infrastructure. Because each task requires automation, moving in that direction requires major investment.

Spaghetti code may follow

While you should always aim to design your architecture correctly from the start, microservices are particularly unforgiving of bad design. If you’re not careful, you’ll find that the cumbersome spaghetti code you desperately want to escape from will follow you into the microservices architecture as well. To prevent this, several design approaches should be avoided, including not creating too many versions in the first place, shielding your microservices with an API gateway (for example, the website application calls nearly 150 services to get the data used to build a web page), and designing your API to be tolerant to changes. If a change will break the API format, the change should be released as a separate version.

Creating an organizational mind shift

Microservices architecture requires discipline and a change in team dynamics that encompasses all aspects of an organization. Using a microservices approach typically means increased responsibility for developers as they become service/microservice owners. Microservices development teams will be asked to make decisions that were previously not theirs—such as ship dates and process selection—and developers will have to deal with the additional complications that come along with creating a distributed system. Testing will also be more difficult, and deployment complexity will increase due to the need to manage a system made up of many different service types.

Keep it flexible

Current business demands are such that applications and technologies must be flexible and adaptive, with minimal customer disruption and improved release times. When implemented correctly, microservices can offer these capabilities by being built and organized around separate components that can be easily updated and replaced—ultimately providing a scalable and agile solution for an enterprise’s needs today.

[ Webinar: How to Fit Security Into Your Software Lifecycle With Automation and Integration ]

Article Tags