Open In App

How to Implement a Distributed Caching System Using Java Networking?

In Java, a distributed caching system is used to store frequently accessed data in the memory across multiple servers, improving the performance and scalability of the applications. We can implement the simple distributed caching system using Java networking, and it can implement multiple cache nodes communicating with each other to store and retrieve cached data.

Program to Implement a Distributed Caching System Using Java Networking

We can implement the simple distributed caching system using Java networking and it can implement the cache server and client server then client the server requests to the server then the server responds to the client.



Step-by-Step Implementation for Cache Server:




// Java program to  implement a Distributed Caching System
import java.net.*;
import java.io.*;
  
public class CacheServer 
{
    public static void main(String[] args) 
    {
        try (ServerSocket serverSocket = new ServerSocket(5000)) 
        {
            System.out.println("Cache server running...");
            while (true) {
                Socket socket = serverSocket.accept();
                new CacheServerThread(socket).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  
class CacheServerThread extends Thread 
{
    private Socket socket;
  
    public CacheServerThread(Socket socket) 
    {
        this.socket = socket;
    }
  
    public void run() {
        try (
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true)
        ) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                String[] numbers = inputLine.split(",");
                if (numbers.length == 2) {
                    int num1 = Integer.parseInt(numbers[0]);
                    int num2 = Integer.parseInt(numbers[1]);
                    int sum = num1 + num2;
                    out.println(sum);
                } else 
                {
                    out.println("Invalid input. Please provide two numbers separated by comma.");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Explanation of the program:

The above program is the example of the cache server we can implement using the ServerSocket. It can implement the simple logic of the sum of two number when the client sends the request then cache server responds to the client return the results.



Client Program:

In this program, we can develop the simple client that can connect the server port 5000 to the cache server then send the request to the cache with two inputs then server process the request and response with results.




import java.net.*;
import java.io.*;
  
public class CacheClient {
    public static void main(String[] args) 
    {
        try (
            Socket socket = new Socket("localhost", 5000);
            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true)
        ) {
            System.out.println("Enter two numbers separated by comma:");
            String input = userInput.readLine();
            out.println(input);
            String response = in.readLine();
            System.out.println("Sum received from server: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Explanation of the Program:


Article Tags :