Discovering Microservices: A Case Study in API Integration and Asynchronous Solutions

Back in 2014, I faced a significant challenge that ultimately led me to discover the power of microservices. In this article, I will share my experience of how I stumbled upon microservices to solve a pressing real-world problem and how it transformed our approach to system architecture.

I was deeply involved in developing a travel system that primarily interacted with a single hotel API using PHP. The initial project proposal was straightforward: the owner only needed one hotel API integration. Given the tight project deadline and our abundant PHP resources, we decided to implement the system in PHP. At that time, Node.js was relatively new in the market, and we opted for the technology we were most comfortable with.

This setup worked well for over a year, providing the necessary functionality and performance for our needs. However, the landscape changed dramatically when the product owner approached us with a new requirement: integrate another hotel API without increasing the overall response time.

The challenge was significant. For example, the first API took 20 seconds to respond, and the second one took 25 seconds. The product owner was adamant that the total response time should not exceed 25 seconds, rejecting the idea of adding the two response times together, which would result in a cumulative 45 seconds. This requirement posed a substantial problem for several reasons:

  1. Performance Constraints: The owner’s requirement meant that the total response time had to be within the longest single API response time. We couldn’t simply sum the times of both APIs, which would have been 45 seconds in total.
  2. Existing PHP Codebase: We had over a year of PHP-based code that was deeply integrated and could not be easily replaced or refactored in a short timeframe.
  3. Monolithic Application: The application was a monolithic system, making it more challenging to isolate and optimize specific components without affecting the entire system.
  4. Lack of Asynchronous Support in PHP: PHP does not natively support asynchronous operations or threading, which are essential for handling multiple API calls concurrently. This limitation made it challenging to meet the required performance standards.

At that time, I had never heard about microservice application design. The concept was completely new to me, and I needed to find a solution that would meet our immediate needs without requiring a complete overhaul of our existing system.

Brainstorming

Given these constraints, I needed to think outside the box. I realized that we needed a way to perform asynchronous operations to meet the performance requirements. That’s when I considered using Node.js, a JavaScript runtime known for its non-blocking, event-driven architecture, which supports asynchronous operations natively.

Here’s the step-by-step approach we took to resolve the issue:

  1. Isolate the Problem: I identified that only the hotel API integration needed to be asynchronous. This meant we didn’t have to rewrite the entire system but could focus on a specific part.
  2. Choose the Right Technology: I decided to develop the new hotel API integration using Node.js due to its excellent support for asynchronous operations.
  3. Bridge the Technologies: Instead of changing the entire infrastructure, we kept the client-side requests in PHP. PHP would then communicate with the Node.js service responsible for handling the hotel APIs.
  4. Optimize API Response Time: By offloading the API integration to Node.js, we could ensure that the maximum response time remained within 25 seconds. Node.js would handle both API calls asynchronously, reducing the total waiting time.

Implementing

The implementation involved setting up a Node.js service that could handle the hotel API requests asynchronously. This service acted as a middle layer between our PHP application and the external hotel APIs. The PHP application sent requests to the Node.js service, which then managed the API calls concurrently, ensuring that the total response time did not exceed the time taken by the slower API.

We carefully tested this setup to ensure that it met the required performance standards. The results were impressive. By leveraging Node.js’s asynchronous capabilities, we managed to keep the total response time within 25 seconds, as required by the product owner.

Results and Learnings

The implementation of this hybrid solution yielded impressive results. By strategically using Node.js for the hotel API integration while maintaining the existing PHP infrastructure, we met the performance requirements without a complete system overhaul. This approach demonstrated several key principles of microservices:

  • Decoupling Services: By isolating the hotel API integration, we could develop and deploy it independently from the rest of the system.
  • Using the Right Tools for the Job: Node.js was chosen for its asynchronous capabilities, which were crucial for meeting the performance requirements.

This solution wasn’t a fully-fledged microservice architecture but rather an introduction to the concept. It showcased how separating concerns and using the right tools for specific tasks could lead to more efficient and scalable systems.

Future Directions

This experience laid the foundation for my journey into microservices. In future articles, I will explore how to build pure microservice-based systems with real-world examples. These articles will cover:

  • Designing Microservices: How to design services that are loosely coupled and highly cohesive.
  • Deployment Strategies: Techniques for deploying microservices independently and scaling them as needed.
  • Real-World Examples: Detailed case studies of implementing microservices to solve complex business problems.
  • Microservice Protocols: Understanding the various communication protocols used in microservices, such as REST, gRPC, and messaging queues.
  • Efficiency Considerations: Analyzing the efficiency and performance benefits of using microservices.
  • And Much More: Covering a wide range of topics related to microservices to provide a comprehensive understanding.

By sharing these insights, I hope to provide a comprehensive guide to understanding and implementing microservices, helping others navigate similar challenges in their development journey.

About the Author:
I’m ASM, a software engineer with over ten years of experience in web development. I enjoy tackling challenging problems and sharing my solutions with the community. When I’m not coding, you can find me exploring nature, reading science fiction, or experimenting with new recipes in the kitchen.

Stay tuned for more articles on microservices and other software development topics.


Leave a Reply