DEV Community

James Konik for Fabric

Posted on • Updated on

Design Patterns for Modern Day Commerce Using Microservices

E-commerce businesses are using microservices to build a set of reusable components for their stores. These services make it easier to deliver your content to multiple channels at scale by operating independently from the frontend.

In this post, I’ll talk about several design patterns you can implement and explain what they offer. I’ll also mention common use cases.

Understanding Software Design Patterns

Software design patterns are defined ways to solve common problems. They help developers understand how the components of a system relate to each other and interact. There’s no “perfect” design pattern—each has pros and cons and is helpful in specific situations.

Most developers spend years of practice getting these patterns right. However, by applying them properly, you can achieve results. There are five design patterns for modern e-commerce architectures:

  • The strangler pattern: A useful way to migrate from legacy software to a more modern approach.
  • The ambassador pattern: This gives you an encapsulated approach to handling network issues.
  • The sidecar pattern: This helps you add functionality without getting too tightly coupled to the rest of your software.
  • API interfaces: These help software services and components communicate.
  • Function chains: These help code to handle sequential tasks.

While implementation is the most difficult part, knowing the names and intentions of each pattern is an essential first step. At the end of this guide, you will have a starting point for deciding when each is right for your e-commerce platform.

The strangler pattern

Named after the strangler fig tree, the strangler pattern is moving from one platform to another gradually. You do this by replacing parts of your software one by one until eventually the old system is fully strangled. In practice, you can break it down into three steps:

  • Transformation: you create new versions of your services, replacing them one at a time
  • Co-existence: where you run the new services and older services together
  • Elimination: you replace everything you need and can retire the old system

Using the strangler pattern allows continuous delivery of new features and high code coverage. It also facilitates a modular, test-driven approach enabling you to isolate issues and ensure each service you deliver works well.

It’s a great way to shift to a new software setup, such as from a monolith to microservices. It lets you break the work down into manageable chunks, driving results quickly. Furthermore, you can distribute tasks to different teams to increase buy-in and accountability across your engineering organization.

On the other hand, it can take time. However, you can mitigate that by having teams work in parallel. Setting up your team right is as important as nailing the technical side of things.

The ambassador pattern

In the ambassador pattern, the ‘ambassador’ service is dedicated to communication. You create a proxy process or service that handles network requests for the rest of your application.

With an ambassador service in place, you can add features like monitoring, logging, and call rerouting. It’s useful to translate requests from one format to another—for example, multi-channel e-commerce, where you distribute products to many different frontend consumers.

If you’re using a mix of legacy and modern software, it can help bridge the gap, ensuring your network meets modern security and accountability standards. From an organizational viewpoint, it lets you assign a team to the proxy service itself, allowing you to divide responsibility.

While the ambassador pattern can quickly tie disparate systems together, it isn’t ideal if network latency is a concern. It can increase inter-service communication and higher memory and CPU usage.

If you have issues moving away from your monolith or legacy software you’re stuck maintaining, the ambassador can be a great way to circumvent those weaknesses. It allows you to add features to old software without having to rewrite everything.

The sidecar pattern

In the sidecar pattern, you move a specified set of functionality into a separate component. This component exists alongside the primary application, generally sharing the same lifecycle.

diagram showing sidecar pattern. The main application and sidecar both exchange information and have access to data on the shared host. The sidecar writes to the shared data when logging.)

The sidecar is hosted together with its parent and can even run in the same process. That means there is little to no latency when the sidecar communicates with the parent, and it has access to the same resources.

It can, however, use a different programming language or framework than the primary service, and multiple sidecars can use different languages. That means you can use the right tool for the job when adding extra functionality or cater to the strengths and preferences of various team members.

Often the sidecar process can handle peripheral functions such as logging or network connectivity, while the main application handles core functions. Thus, if you need to move or reconfigure the application, teams can focus on the sidecar without having to change the main application.

It’s a simple pattern with many potential applications. For example, in an e-commerce setting, you could use it to log financial transactions. As detailed records are vital in e-commerce, you can have an independent one to add to and build on over time.

You can also use the sidecar pattern for handling network operations like adding modern encryption to a legacy service. This can let you partially modernize old e-commerce systems without resorting to a complete rewrite.

API interfaces

An application programming interface, or API, is a way for software components to communicate with each other using a defined set of calls. Web services or microservices commonly use APIs.

In addition to their use over networked communication, you can also use them for communication between microservices on the same host. There are several patterns commonly seen in API interfaces.

REST is the most recognizable. It’s a staple of computer science courses and standard for a vast number of websites and services. It includes a set of verbs, which implement the CRUD pattern. RESTful services are stateless and cacheable, making them ideal for the web.

In headless commerce, APIs allow multiple frontend applications to communicate with your backend services. Websites, apps, and software deployed on any other platform can send API requests to the same location. This lets you work on each component separately, making improvements and additions without worrying about the effect on the whole ecosystem.

Function Chain

You can build serverless functions on the cloud that are self-contained and stateless and can execute on demand. Services like Amazon Web Services, Microsoft Azure, and Google Cloud let you create these, so you don’t have to worry about hardware issues.

You can organize serverless functions into a function chain. In this pattern, each function invokes the next when it completes. This pattern is ideal if a user action kicks off a series of tasks that are slow to process. The first function can respond to the user, so they aren’t left waiting.

There are a few issues to consider when applying this pattern. Ideally, functions are independent and replaceable, but here the functions are dependent on each other. This breaks object-oriented design principles, but it’s necessary for certain applications. You can use a queuing system to call the functions in sequence, making them more independently operable and scalable.

Function chains are useful to implement sequential tasks that are well defined. For example, you may want to call a function chain after a user places an order to process data through different microservices and push it into each subsequent data store.

These tasks can each happen independently and in the background. This way, your e-commerce store’s UI stays snappy while the backend functions might take minutes to complete.

Key Takeaway

The more you know about software patterns and how to apply them to e-commerce microservices, the better you can leverage this existing knowledge to solve problems.

Discussion (0)