In the old days, an All-In-One commerce solution is most companies’ choice. but recently, as Big software company pricing changes and cloud native application gain more traction, the MACH way of doing eCommerce get more attention than ever before.
MACH is an acronym for Microservices, API-first, Cloud-native, and Headless, and it’s a digital strategy and industry tech standard for modern e-commerce:
- Microservices: Breaks down commerce systems into smaller, reusable components
- API-first: An architectural approach
- Cloud-native: Uses cloud computing for scalability and resilience
- Headless: Decouples the front-end and back-end for unique customer experiences
MACH architecture is designed to be:
- Agile: Allows for continuous improvement to meet evolving business needs
- Flexible: Enables seamless omnichannel experiences and flexible integration management
- Future-proof: Allows businesses to stay adaptable and ready for future changes
- Innovative: Allows businesses to satisfy consumer demands and generate revenue
MACH architecture can help businesses:
- Develop and create content pages without needing to rely on developers
- Update product information and other static and dynamic content
- Easily swap or add new features
MACH architecture is considered a superior alternative to traditional monolithic architecture.
So that brings us a very important question, how to use Http/Rest API to communication between each Microservices effectively with build-in retry, error handling and more.
To solve this question, lets dig into most popular HTTP Client library in Java. Apache HttpClient, OkHttp, and Java’s built-in java.net.http (JDK HTTP Client):
Apache HttpClient
- Mature & Robust: A well-established library with a long history and a proven track record of reliability.
- Feature-Rich: Offers a wide range of features, including connection pooling, authentication, redirects, and cookie management.
- Extensible: Highly customizable and extensible through its pluggable architecture.
- Large Community: Strong community support, extensive documentation, and a wealth of resources available.
OkHttp
- Modern & Efficient: Designed for modern networking needs, focusing on performance and efficiency.
- Interceptors: Powerful interceptors allow for easy customization of requests and responses.
- Connection Pooling & Caching: Excellent connection pooling and caching mechanisms for improved performance.
- Focus on Mobile: Originally developed for mobile applications, making it suitable for resource-constrained environments.
Java JDK HTTP Client
- Modern & Asynchronous: Built-in to the JDK, leveraging asynchronous operations for improved performance and responsiveness.
- Simplified API: Offers a more modern and streamlined API compared to older Java networking classes.
- HTTP/2 Support: Provides native support for HTTP/2, including features like server push.
- Part of the JDK: No external dependencies required, making it easy to integrate into your projects.
Here’s a table summarizing the key differences:
Feature | Apache HttpClient | OkHttp | Java JDK HTTP Client |
---|---|---|---|
Maturity | Mature & Established | Modern & Efficient | Relatively Newer |
API | More traditional | Modern & Interceptors | Modern & Asynchronous |
HTTP/2 Support | Supported | Supported | Native Support |
Dependencies | External library | External library | Built-in to JDK |
Focus | General-purpose | Performance & Mobile | Asynchronous & Modern |
Choosing the Right Library:
- Apache HttpClient: A solid choice for projects requiring a mature and feature-rich library, especially if you value extensive community support and a large ecosystem of plugins.
- OkHttp: Ideal for performance-critical applications, mobile development, and scenarios where flexibility and customization are essential.
- Java JDK HTTP Client: The preferred choice for new projects that leverage asynchronous operations and require native HTTP/2 support. It’s also simpler to integrate due to its inclusion in the JDK.
Ultimately, the best choice depends on the specific needs and requirements of your project. Consider factors like performance, maintainability, ease of use, and the specific features you require when making your decision.
Let me show an example of Apache HttpClient from Commerce Tools Java SDK v2:
public static PoolingAsyncClientConnectionManagerBuilder createConnectionManager(final int maxConnTotal,
final int maxConnPerRoute) {
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create().useSystemProperties().build();
final TlsConfig tlsConfig = TlsConfig.copy(TlsConfig.DEFAULT)
.setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
.build();
return PoolingAsyncClientConnectionManagerBuilder.create()
.setMaxConnPerRoute(maxConnPerRoute)
.setMaxConnTotal(maxConnTotal)
.setDefaultTlsConfig(tlsConfig)
.setTlsStrategy(tlsStrategy);
}
you can see, we can setup HTTP version policy, TLS policy, max connection and max connection per route. In the meantime, you also can configure Async client if you want, please refer to CloseableHttpAsyncClient.java
Within Sprint boot, you can also do following
public RestTemplate commonRestTemplate() {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(commonHttpClient());
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
private CloseableHttpClient commonHttpClient() {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(this.commonHttpClientProperties.getConnectionTimeoutMs())
.build();
SocketConfig socketConfig = SocketConfig.custom()
.setSoTimeout(this.commonHttpClientProperties.getSocketTimeoutMs())
.build();
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(this.commonHttpClientProperties.getMaxConnectionsPerRoute());
cm.setMaxTotal(this.commonHttpClientProperties.getMaxConnectionsTotal());
cm.setDefaultSocketConfig(socketConfig);
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(requestConfig)
.build();
return httpClient;
}
As we fine-tuned LLMs as a hot topic this year, I’d like to introduce the fine tune Apache HttpClient for you. This is coming from John Coyne from Discover Financials, He dedicated a lot of efforts to fine tune Apache HTTP Client with retries, time-out and monitoring capacities. it is a must read for anyone interested in Apache HttpClient. I’d like to give credits to him. Ok, without too much intro, here is the repo.