In the new micro service architecture the traditional monolithic application is broken down into multiple small scoped domains. That brings several benefits both from a development perspective since micro services can be iterated individually and also performance since they can be scaled separately.
Now with the move to a micro service architecture one common problem is how to make all the services communicate with one another? Typically an application exposes a REST API interface to the client so naturally the first option is to start using HTTP. The question then becomes, is HTTP the best messaging protocol for inter service communication?
Looking at the HTTP’s definition from the official RFC
The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. It is a generic, stateless, protocol which can be used for many tasks beyond its use for hypertext, such as name servers and distributed object management systems, through extension of its request methods, error codes and headers.
It is clear that HTTP wasn’t built to provide a communication layer between micro services.
On the contrary, looking at the NATS protocol definition
Open Source. Performant. Simple. Scalable. A central nervous system for modern, reliable, and scalable cloud and distributed systems.
It becomes clear that it was built with new modern systems in mind.
One thing to keep in mind is that a RESTful API could be built on top of NATS across all micro services. The key point is that HTTP is not the best protocol for inter service communication.
In theory it all sounds good, but in practice how much faster is NATS? To test both approaches a simple benchmark was put together trying to mimic a real world example.
The idea is that a HTTP proxy server will accept client connections and then route the request to the appropriated internal micro service. In one scenario HTTP was used to proxy the messages and on the other NATS.
To generate the client traffic that would be hitting the proxy the bench-rest tool was used.
Sequential requests with no payload
Overall NATS performed 3x faster than HTTP. To process 50,000 requests using HTTP as the messaging protocol between the proxy and the micro service it took ~12 mins while using NATS it only took 5.
Sequential requests with 1K payload
When adding some data to the requests the behavior was similar and NATS still processed messages ~3x faster than HTTP.
When a factor of concurrency was added to the tests that’s when NATS performed the best. To process 50,000 requests with a batch of 2,500 concurrent requests at a time NATS took 1.6 mins while HTTP took 18 mins.
Overall the results shown that NATS beats HTTP both in speed and throughput.
The source code used for the benchmarks can be found here