Synchronous vs Asynchronous message processing
In client-server communication, a client will normally request work to be done on another system (server) by sending messages between each other & communicating the outcome of the requested work.
Management of this communication grows in complexity on the network layer depending on the rate at which messages are sent, amount of requests a service can handle at a given time and the speed at which client expects a response.
Synchronous Processing
A more traditional way of managing client – server communication.
A client sends a request to the server & awaits the server to complete the job and send the response before client can continue doing any other work.
This process referred to ‘blocking’ ie client is blocked from doing any work until it receives final response from the server.
Asynchronous Processing
In this mode, the client does not have to wait for the server’s final response after a request is made.
Also referred to as non-blocking. the client’s thread is not blocked after making a request & allows the systems to scale better as more work can be done in a given time.
Key Differences Between Synchronous & Asynchronous
Synchronous | Asynchronous | ||
---|---|---|---|
1 | Block thread execution of the client, causing them to wait for as long as requests take before processing any other action | Requests do not block and allow more work be done in a given amount of time. | |
2 | Blocking nature of synchronous requests can cause client consume more resources as the execution thread is blocked during waiting period. | Asynchronous requests immediately frees up the execution thread to perform more functions without having to wait for response. | |
3 | Difficult to build responsive apps with synchronous processing as its hard to determine how long requests will take. | Response time is quick client does not have to wait for response thus apps can be responsive. | |
4 | Low fault tolerance due to clients having to wait for response. | Failure tolerance is high, easy. to build a retry mechanism | |
5 | Requests can only be processed sequentially | Allows for requests to be processed in parallel. |
Achieving Asynchronous Communication
To achieve asynchronous communication a message broker can be introduced between the client and the server. It will ingest messages which are then ingested by consumers depending on their capability.
The client will get an acknowledgement once a message is queued and proceed performing other tasks.
Example Message Brokers.
- Active MQ
- Rabbit MQ
- Kafka
On the Next post we’ll explore Kafka and Rabbit MQ.