Communication Patterns
in Distributed Systems
Remote Procedure Call
Concept:
● RPC allows a program to cause a procedure (subroutine) to execute in another address space (commonly on another physical
machine).
Key Points:
● Synchronous communication: The client waits for the server to finish processing and return the result.
● Abstraction: It abstracts the details of the network communication, making remote calls appear like local calls.
● Languages: Protocols like JSON-RPC, XML-RPC, and implementations in gRPC, Thrift.
Example Use Case:
● A client application calling a function on a remote server to fetch user data.
Remote Procedure Call
Pros:
● Simplicity: Abstracts the complexities of network communication, making remote calls look like local calls.
● Language Agnostic: Various implementations (e.g., gRPC) support multiple programming languages.
● Standardization: Many well-established protocols and libraries.
Cons:
● Synchronous Nature: Can lead to blocking calls, which may affect performance if the remote service is slow or unresponsive.
● Tight Coupling: The client and server need to agree on the interface, which can make versioning and changes more difficult.
● Error Handling: Network issues and remote failures can complicate error handling.
Remote Method Invocation (RMI)
Concept:
● RMI is similar to RPC but is specific to Java. It allows objects to invoke methods on an object running in another Java Virtual
Machine (JVM).
Key Points:
● Object-Oriented: Supports the concept of remote objects and allows invoking methods on them.
● Built-in Java: Part of the Java standard library.
● Serialization: Java objects are serialized and deserialized during communication.
Example Use Case:
● A Java client invoking a method on a remote Java server to perform a calculation.
Remote Method Invocation (RMI)
Pros:
● Object-Oriented: Allows remote method calls on objects, integrating well with Java's object-oriented paradigm.
● Built-in Security: Provides built-in mechanisms for handling security.
● Ease of Use: Being a part of the Java standard library, it's easy to set up and use within Java applications.
Cons:
● Java-Specific: Tied to Java, making it less useful in polyglot environments.
● Complexity: Can be complex to debug and manage, especially with large distributed systems.
● Performance Overhead: Serialization and deserialization can add overhead.
Message Passing
Concept:
● Communication via message exchange where processes or objects send and receive messages.
Key Points:
● Asynchronous communication: Messages can be sent and received without blocking the sender.
● Loose coupling: Systems can be loosely coupled, as they do not need to know the details of each other.
● Message Queues: Systems like RabbitMQ, Kafka for managing messages.
Example Use Case:
● A microservice architecture where services communicate via a message broker like RabbitMQ.
Message Passing
Pros:
● Asynchronous Communication: Decouples sender and receiver, allowing for non-blocking communication.
● Scalability: Can handle a large number of messages efficiently, especially with message brokers.
● Fault Tolerance: Messages can be stored and retried in case of failures.
Cons:
● Complexity: Requires managing message queues and handling message delivery guarantees.
● Latency: Messages may experience delays depending on the broker's performance and load.
● State Management: Maintaining the state across distributed components can be challenging.
Shared Memory
Concept:
● Multiple processes share a common memory space and communicate by reading and writing to that memory.
Key Points:
● High speed: Direct memory access is faster than message passing.
● Synchronization needed: Requires mechanisms like semaphores or locks to prevent data races.
Example Use Case:
● A multi-threaded application where threads share a global variable for communication.
Shared Memory
Pros:
● Speed: Direct memory access is fast, offering high-performance communication.
● Simplicity: Simple to implement for local communication between processes.
Cons:
● Synchronization Issues: Requires careful synchronization to avoid data races and inconsistencies.
● Limited Scope: Generally limited to processes on the same machine.
● Scalability: Not suitable for large-scale distributed systems across multiple machines.
Publish Subscribe
Concept:
● A messaging pattern where senders (publishers) send messages without knowing the receivers (subscribers).
Key Points:
● Decoupled systems: Publishers and subscribers are decoupled from each other.
● Event-driven: Suitable for event-driven architectures.
● Topics: Messages are categorized into topics, and subscribers receive messages based on their subscriptions.
Example Use Case:
● A news service where different clients subscribe to different news topics.
Publish Subscribe
Pros:
● Decoupling: Publishers and subscribers are decoupled, allowing independent evolution.
● Scalability: Supports a large number of subscribers and can handle high message volumes.
● Flexibility: Suitable for dynamic and flexible systems where components can subscribe and unsubscribe at runtime.
Cons:
● Complexity: Managing topics, subscriptions, and message delivery can be complex.
● Latency: Can introduce delays, especially if there are many intermediaries or if the system is under heavy load.
● Reliability: Ensuring reliable message delivery in the face of network failures can be challenging.
Socket Programming
Concept:
● Low-level communication over a network using sockets.
Key Points:
● Flexibility: Allows custom protocols and fine-grained control over communication.
● Bidirectional: Supports two-way communication.
● TCP/UDP: Can use TCP for reliable communication or UDP for faster, connectionless communication.
Example Use Case:
● A chat application where clients communicate with each other using TCP sockets.
Socket Programming
Pros:
● Flexibility: Allows custom protocols and provides fine-grained control over communication.
● Efficiency: Direct communication can be very efficient with low overhead.
● Bidirectional: Supports two-way communication, making it suitable for interactive applications.
Cons:
● Complexity: Requires handling low-level details like connection management, data framing, and error handling.
● Development Time: More time-consuming to develop compared to higher-level abstractions.
● Security: Requires careful handling to avoid security vulnerabilities like buffer overflows and injection attacks.
Web Services (REST)
Concept:
● Web services enable communication over the web using standard protocols like HTTP.
Key Points:
● Interoperability: Can be used across different platforms and languages.
● REST: Uses standard HTTP methods and can use JSON, XML, etc.
Example Use Case:
● A RESTful API providing data to various client applications over HTTP.
Web Service REST - Pros and Cons
Pros:
● Interoperability: Can be used across different platforms and languages.
● Standardization: Established standards and protocols, making integration easier.
● Wide Adoption: Widely used and supported by many tools and frameworks.
Cons:
● Performance Overhead and Latency: Can be slower due to the use of text-based protocols (XML, JSON) and HTTP.