DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Lifecycle Microservices With GenAI Tools
  • FastAPI Got Me an OpenAPI Spec Really... Fast
  • AI-Driven API and Microservice Architecture Design for Cloud
  • Implementation Best Practices: Microservice API With Spring Boot

Trending

  • Java Virtual Threads and Scaling
  • Unlocking AI Coding Assistants Part 2: Generating Code
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • AI, ML, and Data Science: Shaping the Future of Automation
  1. DZone
  2. Data Engineering
  3. Databases
  4. Microservice Pattern: API Gateway

Microservice Pattern: API Gateway

The API Gateway helps us fetch and aggregate data for our microservices. Learn how to implement this pattern in this tutorial.

By 
Héctor Valls user avatar
Héctor Valls
·
Jun. 01, 18 · Tutorial
Likes (24)
Comment
Save
Tweet
Share
28.3K Views

Join the DZone community and get the full member experience.

Join For Free

Microservice architectures are here to stay and, when implementing them, we need to face some troubles we don’t suffer in monolithic systems. How to fetch and aggregate data from several services to serve a single request is one of them, and the API Gateway pattern is what we need to solve it.

First, we have an Order Microservice, where, via HTTP call, we can get information from an order:

GET http://order_service/orders/1

{
  id: 1,  
  description: "This is the order’s description",  
  customerId: 56,  
  lines: [    
    { description: "item 1", price: 3.5 },    
    { description: "item 2", price: 2 }    
  ],  
  total: 5.5
}

The Order Microservice only works with orders, not with customers; that’s why the order doesn’t know anything about the customer but their ID.

Then, we have a Customer Microservice, where we can get complete information about that customer:

GET http://customer_service/customers/56

{ 
  id: 56, 
  name: "John Doe", 
  email: "john@mail.com"
}

Now, we want to expose our services as a public API so our applications, or external ones, can consume it.

When one client requests information about an order, we want to embed customer information in the response, instead of forcing him to make another extra request. This data aggregation logic must be made in the API Gateway layer and the response would be something like this:

GET http://api/orders/1

{   
  id: 1,  
  description: "This is the order’s description",  
  customer: {    
    id: 56,    
    name: "John Doe",   
    email: "john@mail.com"  },  
  lines: [    
    { description: "item 1", price: 3.5 },   
    { description: "item 2", price: 2 }    
  ],  
  total: 5.5
}

As you see, the response contains the full data from the customer. The API consumer doesn’t know that internally two requests were needed to build the response; it’s transparent, and that’s the core idea of API Gateway.

About API Gateway:

  • It acts as a router. It is the only entry point to our collection of microservices. This way, microservices are not needed to be public, anymore, but are behind an internal network and API Gateway is responsible for making requests against a service or another one (Service Discovery).
  • It acts as a data aggregator: API Gateway fetches data from several services and aggregates it to return a single rich response. Depending on the API consumer, data representation may change according to the needs, and here is where Backend For Frontend (BFF) comes into play.
  • It is a protocol abstraction layer: API Gateway can be exposed as a REST API or GraphQL or whatever, no matter what protocol or technology is being used internally to communicate with the microservices.
  • Error management is centralized: When a service is not available, is getting too slow or something like that, API Gateway can provide data from cache, default responses or make smart decisions to avoid bottlenecks or fatal errors propagation. This keeps the circuit closed (Circuit Breaker) and makes the system more resilient and reliable.

I have built a naive implementation of an API Gateway, based on the example above, using NodeJS, Express, and RxJS. You can find it in this repo.

I hope you enjoyed the post!

API microservice

Published at DZone with permission of Héctor Valls. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Lifecycle Microservices With GenAI Tools
  • FastAPI Got Me an OpenAPI Spec Really... Fast
  • AI-Driven API and Microservice Architecture Design for Cloud
  • Implementation Best Practices: Microservice API With Spring Boot

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!