Peer to Peer Systems
Remote Method Invocation
Network Programming
Distributed Systems
Java RMI

peer to peer System with remote method invocation(rmi)

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction to Peer-to-Peer Systems

Peer-to-peer (P2P) systems are decentralized networks where each participant (peer) shares a part of their own resources. These resources could include network bandwidth, processing power, storage, etc. Unlike traditional client-server models, where communication often occurs between a client and a centralized server, P2P systems involve direct exchanges of information, services, or files between peers.

Remote Method Invocation (RMI)

Remote Method Invocation (RMI) is a Java API that performs the object-oriented equivalent of remote procedure calls (RPC), with support for direct transfer of serialized Java classes and distributed garbage-collection. It allows an object running in one Java Virtual Machine (JVM) to invoke methods on an object running in another JVM. RMI is used commonly in distributed systems or applications to allow network-accessible APIs to interact in a seamless manner.

How RMI Fits into Peer-to-Peer Models

In peer-to-peer architectures, RMI can be used to simplify the development of network applications. It enables peers to call methods on each other as if they are on the local machine. This helps in developing systems where peers provide services like file sharing, computations, or chat messaging directly to each other without needing a centralized server.

Technical Explanation of RMI

Basic Components of RMI

  1. Stub and Skeleton:
    • Stub acts as a gateway for the client. It resides on the client side and represents the remote object. When a client invokes a method on a stub, it forwards the request to the skeleton.
    • Skeleton resides on the server side. It receives method calls from the stub, performs them on the actual remote object, and returns the result back to the stub.
  2. Remote Interface:
    • This defines the methods that can be invoked remotely. Any object that implements this interface can be accessed remotely.
  3. RMI Registry:
    • It provides a simple naming service to register all remote objects. Either built into the Java Development Kit (JDK) or set up separately, the registry allows clients to look up remote objects by name, facilitating location transparency.

Example of RMI in Action

Suppose we have an application where peers need to compute parts of a dataset and share results. Here's a simplified RMI setup:

DataProcessor.java

java
1import java.rmi.Remote;
2import java.rmi.RemoteException;
3
4public interface DataProcessor extends Remote {
5    int process(int[] data) throws RemoteException;
6}

DataProcessorImpl.java

java
1import java.rmi.server.UnicastRemoteObject;
2import java.rmi.RemoteException;
3
4public class DataProcessorImpl extends UnicastRemoteObject implements DataProcessor {
5    protected DataProcessorImpl() throws RemoteException {
6        super();
7    }
8
9    public int process(int[] data) throws RemoteException {
10        return sum(data); // Just summing up for simplicity
11    }
12    
13    private int sum(int[] data) {
14        return Arrays.stream(data).sum();
15    }
16}

Server setup

java
1import java.rmi.registry.LocateRegistry;
2import java.rmi.registry.Registry;
3
4public class Server {
5    public static void main(String[] args) {
6        try {
7            Registry registry = LocateRegistry.createRegistry(1099);
8            DataProcessorImpl obj = new DataProcessorImpl();
9            registry.bind("DataProcessor", obj);
10            System.out.println("Processor bound");
11        } catch (Exception e) {
12            System.err.println("Server exception: " + e.toString());
13            e.printStackTrace();
14        }
15    }
16}

Client setup

java
1import java.rmi.registry.LocateRegistry;
2import java.rmi.registry.Registry;
3
4public class Client {
5    private Client() {}
6
7    public static void main(String[] args) {
8        try {
9            Registry registry = LocateRegistry.getRegistry("localhost");
10            DataProcessor stub = (DataProcessor) registry.lookup("DataProcessor");
11            int[] data = {1, 2, 3, 4, 5};
12            int result = stub.process(data);
13            System.out.println("result: " + result);
14        } catch (Exception e) {
15            System.err.println("Client exception: " + e.toString());
16            e.printStackTrace();
17        }
18    }
19}

Key Advantages and Challenges

AspectBenefitsChallenges
ScalabilityNew nodes can be added easily, distributing the load further.Managing state consistency across nodes can be complex.
Fault ToleranceNo single point of failure.Error handling must be robust against node failures.
Cost EffectivenessReduced need for centralized infrastructure.Initial development and setup can be more complex.

Conclusion

Peer-to-peer systems using RMI provide a robust framework for building decentralized applications. They can effectively reduce the need for monolithic, centralized servers, thus enhancing fault tolerance and reducing dependency on a single point of failure in network architectures. This Java-based technology helps in achieving seamless method invocation across distributed systems, making it a core component in many peer-to-peer applications like file-sharing systems or distributed computation platforms.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.