The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Architecture and operations
How is a server calling another server different from calling a method?
Why it exists — If all features are in one program, changing only the payment part means restarting the whole program, and features run by another company, such as card payment or sending text messages, cannot be done by my program alone. A remote call hands such features to a separately running server, sends a request over the network, and receives the result, so that different programs can divide the work among themselves.
Where it's used today — It is used when a shopping mall server asks the server of a payment company to process a payment, or asks the server of another service for information such as maps or weather. In a service whose features are split across several servers, it is common for the order server to call the member server or the stock server. In Spring, you send the request with the RestClient that Spring provides, set a connect timeout and a read timeout on the request factory, and handle the ResourceAccessException that comes from a connection failure or a timeout. To keep the thread that sent the request from standing still while it waits for the answer, you use WebClient, and work whose answer need not be received right away is sometimes passed to a message queue.
A large service sometimes splits its features among several servers. If the order server and the payment server are different programs that run separately, then instead of calling a method to make a payment, the code of the order server has to send a request (POST /pay) to the payment server over the network. In Spring, this request is sent with the RestClient that Spring provides. The code on the screen has two versions. If the payment feature is in the same program, you call the method of a PayService object (a class made for this example) with payService.pay(), and if it is on another server, you call the payment server with restClient.post().uri(...).retrieve().body(String.class). Both get a String, "Payment done", back, but when you call a server across a network, the other side may be off and refuse the connection, and if the other side is slow, you have to wait for the answer. So when you create the RestClient you decide how long to wait for an answer (the timeout), and you catch the ResourceAccessException that comes when the call fails. The result was made by compiling the code on the screen as it is and running it, with a small server standing in for the payment server started separately.
Checked against real execution · Compiled and ran the code on the screen as it is with the RestClient of Spring Framework 7.0.9, with a small HTTP server standing in for the payment server (normal, slow, and off), and checked twelve combinations (verify/spring/cmp_remote_basic.js)
// Code of the order server. Payment is handled by a separately running payment server. OrderApp is a name chosen for this example.
import java.time.Duration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestClient;
public class OrderApp {
public static void main(String[] args) {
// The wait time (timeout) is set on the request factory, which actually makes the connection and sends the request
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(Duration.ofSeconds(1)); // up to 1 second to make the connection
factory.setReadTimeout(Duration.ofSeconds(1)); // after connecting, waits up to 1 second for the answer
RestClient restClient = RestClient.builder()
.requestFactory(factory)
.build();
try {
String result = restClient.post()
.uri("http://localhost:9000/pay") // address of the payment server (9000 is an example number)
.retrieve()
.body(String.class); // receives the body of the answer as a String
System.out.println(result);
} catch (ResourceAccessException e) { // connection failure or timeout
System.out.println("Call failed: " + e.getCause());
}
}
}
The behavior was checked by running the RestClient of Spring Framework 7.0.9 (spring-web) on JDK 21.0.12.1 (Windows 11). For all twelve combinations (two places for the payment feature, three states of the payment side, and two for whether a timeout was set), the same text as the code on the screen was written as OrderApp.java, compiled with javac, and run. In the code that calls another server, only the 9000 of the address was replaced with a free port that was actually opened (9000 is an example number). The role of the payment server was played by a small HTTP server that the check program (verify/spring/src/RemoteBasicProbe.java) started on 127.0.0.1 with the ServerSocket of Java; when it receives POST /pay, it sends, after 0.1s or 3s, a 200 response with the body "Payment done" (text/plain, UTF-8). A payment server that is off was imitated by opening a port and then closing it, so that nobody listens on it. This payment server was built for this lab to finish processing without checking whether the order side had closed the connection, which does not mean that every server behaves that way. For each combination, we checked against the model the text that main printed, the time taken, the request line the payment server received (POST /pay HTTP/1.1), when the payment server finished processing, and whether the connection of the order side was already closed at that time, and we also confirmed that the code on the screen is the same text as the source that was run (verify/spring/cmp_remote_basic.js). The time taken is written roughly on the screen; the values measured in the run were 0.10 to 0.21s for about 0.1s, 0.06s or less for right away, 1.01s for about 1s, and 3.01 to 3.08s for about 3s. We made the same call once more in the check program and confirmed that the exception caught by catch is exactly org.springframework.web.client.ResourceAccessException and that the causes are java.net.ConnectException (connection refused) and java.net.SocketTimeoutException (Read timed out). The explanation after ConnectException in the text that catch prints differed with the request factory. The SimpleClientHttpRequestFactory with a timeout set added "Connection refused: getsockopt", and the one created with RestClient.create() printed only the class name with no explanation. In the library setup of this run (no Apache HttpClient, Jetty, or Reactor Netty), RestClient.create() chose JdkClientHttpRequestFactory, which uses the HttpClient of the JDK, and it set no timeout. With other libraries present, it may choose a different request factory. When the payment server was made to receive the request and never answer, the RestClient created with RestClient.create() waited past 4s (we did not watch beyond that), and the RestClient with a 1s timeout ended at about 1s with ResourceAccessException (cause SocketTimeoutException). PayService of the same program (a class made for this example) imitated the time the payment takes with Thread.sleep, and since "only the payment side is off" cannot happen to a method, it was run with 0.1s, the same as the default settings. That a method call has no connection step, and that the thread that called the method runs the body as it is, are explanations of how the Java language works and were not measured separately. A method call can also be handed to another thread and waited for with Future.get with a time limit, but this lab covers only the line that simply calls a method. The screen covers only the simplest case of one call; the cases where the connect timeout actually fires (a peer that gives no answer at all to the connection request) and 404 and 500 responses are covered in "Calling external APIs and timeouts", a device that shields you from failing servers in "Circuit breaker", ways to undo the work of several servers in "Saga and outbox", and receiving requests in one place and splitting them in "Gateway and gradual migration".
Previous lab: Processes · Next lab: Load balancers
Struck-through text was removed from the first version of the code; highlighted text was added.