The labs on this site need JavaScript to run. The same content is written out below.
Learn › Architecture and operations › Splitting into services
Gateway and gradual migration
Breaking up a huge server piece by piece without stopping it
Why it exists — If an old, huge server is replaced with a newly built system all at once, you have to wait a long time until the new system is completely built, and a problem that appears on the day of the switch spreads to every feature at the same time. If an API gateway in front decides where each route's requests go, you can move features to new services one at a time, and send only the route that has a problem back to the existing server.
Where it's used today — It serves as the entrance to a system split into several services: the web and mobile apps call several services through one address, and requests without a login token are stopped before they reach the internal services. In a migration that moves an existing server to new services bit by bit, it is used to change where each route is sent. In Spring, Spring Cloud Gateway is used, and a reverse proxy such as Nginx is sometimes used to split only the paths.
You put a gateway in front of an existing server (a monolith) that holds every feature in one lump, and hand requests over to new services one route at a time. The address that users see does not change.
Checked against real execution · Sent the 10 requests to Spring Cloud Gateway 5.0.5 (Spring Boot 4.1.1, Spring Security 7.1.1 resource server) started from the application.yml on the screen, and compared the requests and status codes that nginx 1.27.5, standing in for the servers behind it, received in all twenty-four combinations. The Weight ratio was checked by sending the same 10 requests 500 times
Concepts in this lab
- API gateway
- The single entry point that receives all client requests first and forwards them to the right internal service. It handles common functions such as authentication, rate limiting, and logging in one place. Clients do not need to know the layout and addresses of the internal services.
- Strangler fig pattern
- Where it's used today — It is used when splitting an old monolithic service into several services, or when replacing a screen built with an old framework with a new technology. New features are built in the new system from the start, and existing features are moved starting with those that are used less or are easy to detach.
- A way to replace an existing system gradually by moving features to a new system one at a time, instead of replacing it all at once. At the front, where requests arrive, only the requests for features that have been moved go to the new system, and the existing system keeps handling the rest. When all features have been moved, the existing system is removed.
- Monolithic architecture and microservice architecture
- Why it exists — An application that has grown to hold many features has to be rebuilt and redeployed as a whole even for a small change, and several teams that share the same code and deployment schedule end up waiting for each other. Microservice architecture is a way to split the system into services so that each team changes and releases its own service separately.
- Where it's used today — Organizations in which several teams share the work on a large service use microservice architecture. Small teams and new services often start as a monolith and split off services when the boundaries to split on become clear and the need arises.
- A monolithic architecture builds all features as one application and deploys them together. A microservice architecture splits the features into small services, and each one is developed, deployed, and scaled independently. With microservices, each service can be deployed and scaled separately and failures are isolated, but the complexity of communication between services, data consistency, and operations grows a lot.
- Reverse proxy
- Why it exists — If slow clients connect straight to an application server, the request processing threads stay tied up with those connections while the clients slowly upload a large request body or slowly take a response. A reverse proxy such as Nginx, in its default configuration, buffers requests and responses and exchanges data quickly with the backend server, so the backend server's threads are not tied up for long by slow connections.
- Where it's used today — It is used to split static file requests and /api requests by path and send them to different servers, or to reject too-large uploads at the front with Nginx's client_max_body_size. In Kubernetes, an Ingress controller takes this role.
- A server that receives client requests on behalf of internal servers, forwards them to those servers, and returns their responses to the client. The client does not know the actual servers directly. Its roles include distributing requests across several servers, handling TLS, caching, and protecting the internal servers.
- Rolling, blue-green, and canary deployments
- Ways to deploy a new version without interrupting the service. A rolling deployment replaces servers with the new version a few at a time, in turn. A blue-green deployment prepares a complete, separate environment for the new version and then switches the traffic all at once. A canary deployment applies the new version to only some users first, checks that there are no problems, and then expands it gradually.
Try these
- Default settings — Only the order route is handed over to the new service.
- Only 10% of orders first — Verifies the new service with a small share of real requests.
- Before migration — Every request goes to the existing server.
- Migration complete — No requests go to the existing server, so it can be shut down.
- Requests with no token — The gateway filters them out at the entrance.
Running with the default settings
- All of the user's requests come in through one place, the gateway. The gateway first does the common work (on this screen, token validation by Spring Security), then looks at the path and decides which server behind it to pass the request to. The user does not know how many servers are behind it.
- GET /orders/1: The path starts with /orders. This path is configured to be passed to the new service. → Order service (new)
- POST /payments: The path starts with /payments. It is not a path with its own rule, so it goes to the existing server. → Existing server (monolith)
- GET /members/me: The path starts with /members. It is not a path with its own rule, so it goes to the existing server. → Existing server (monolith)
- POST /orders: The path starts with /orders. This path is configured to be passed to the new service. → Order service (new)
- GET /orders/2: The path starts with /orders. This path is configured to be passed to the new service. → Order service (new)
- GET /members/7: The path starts with /members. It is not a path with its own rule, so it goes to the existing server. → Existing server (monolith)
- POST /orders: The path starts with /orders. This path is configured to be passed to the new service. → Order service (new)
- GET /payments/9: The path starts with /payments. It is not a path with its own rule, so it goes to the existing server. → Existing server (monolith)
- GET /orders/3: The path starts with /orders. This path is configured to be passed to the new service. → Order service (new)
- GET /orders/4: The path starts with /orders. This path is configured to be passed to the new service. → Order service (new)
Result of this run
- You do not move every feature at once; you split them off one route at a time. If something goes wrong with a new service, you can change the configuration back and send the requests to the existing server again (the data the new service has written has to be handled separately). Because the new system gradually wraps around the existing one and replaces it, this approach is called the strangler fig pattern.
Code
# Dependencies: spring-cloud-starter-gateway-server-webflux, spring-boot-starter-security-oauth2-resource-server
# Property names of Spring Cloud Gateway 4.3 (Spring Cloud 2025.0) and later (earlier versions use spring.cloud.gateway.routes)
spring.cloud.gateway.server.webflux:
routes:
- id: order
uri: http://order-service
predicates:
- Path=/orders/**
- id: legacy # everything else goes to the existing server
uri: http://legacy-monolith
predicates:
- Path=/**
# Token validation: the Spring Security resource server (JWT) requires a token on every request
spring.security.oauth2.resourceserver.jwt:
public-key-location: classpath:token-public.pem # public key that verifies the signature (the file name is an example)
Assumptions of this model
This is a fixed example of 10 requests. All twenty-four combinations were run and compared by starting the gateway from the application.yml on the screen as is, with Spring Boot 4.1.1, Spring Cloud Gateway 5.0.5 (Spring Cloud 2025.1.5, WebFlux and Reactor Netty), and the Spring Security 7.1.1 resource server, with the four servers behind it replaced by nginx 1.27.5 that returns the name of each server (legacy-monolith and the other server names are examples). The tokens are RS256 JWTs made for the test, and the security configuration is the default one that Spring Boot creates automatically (CSRF protection is on). The Weight predicate picks a route for each request with a random number from ThreadLocalRandom (WeightCalculatorWebFilter in Spring Cloud Gateway), so for the settings that split by ratio, the same 10 requests were sent 500 times to check that order requests go only to the two servers and that the share is close to the setting (in one run on this computer, 10.0% and 9.8% for 10%, and 49.7% and 51.6% for 50%). Other common features such as rate limiting and logging, and the hardest parts of moving a feature, separating the data and synchronizing the two systems, are not covered.
Previous lab: Saga and outbox · Next lab: CI/CD pipelines