How to Implement a Distributed Caching System Using Java Networking
In this tutorial, we are going to learn how to implement a distributed caching system using java networking. The efficiency and scalability of the applications are increased by using a distributed caching mechanism to store frequently visited data in memory across several servers. With Java networking, we can create a basic distributed caching system that allows several cache nodes to communicate with one another in order to store and retrieve data that has been previously cached.
With Java networking, we may create a basic distributed caching system that includes a cache server, client server, and server that answers to requests from the client after them.
Implementation for Cache Server:
- Write the main method of the CacheServer class into it after creating it.
- 5000 is the port number that should be assigned when creating the server socket using ServerScoket.
- Make the try block mechanism to handle problems and output the phrase "Server starting."
- As soon as the server has finished creating itself, it accepts the request from the client and replies.
- The basic concept of adding up the numbers in case of invalid inputs returning an invalid message can be implemented in this server.
Server program:
Filename: DistributedCacheServer.java
import java.net.*; import java.io.*; public class DistributedCacheServer { public static void main(String[] args) { try (ServerSocket serverSocket = new ServerSocket(5000)) { System.out.println("Cache server running..."); while (true) { Socket soc = serverSocket.accept(); new CacheServerThread(soc).start(); } } catch (IOException e) { e.printStackTrace(); } } } class CacheServerThread extends Thread { private Socket soc; public CacheServerThread(Socket soc) { this.soc = soc; } public void run() { try (BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream())); PrintWriter out = new PrintWriter(soc.getOutputStream(), true)) { String inputLine; while ((inputLine = in.readLine()) != null) { String[] request = inputLine.split(","); if (request.length == 2) { try { int request1 = Integer.parseInt(request[0].trim()); int request2 = Integer.parseInt(request[1].trim()); int sum = request1 + request2; out.println(sum); } catch (NumberFormatException e) { out.println("Invalid input. Please provide valid integers separated by a comma."); } } else { out.println("Invalid input. Please provide two numbers separated by a comma."); } } } catch (IOException e) { e.printStackTrace(); } } }
Output:
Cache server running...
Explanation:
The program mentioned above serves as an example of the type of cache server that may be created with ServerSocket. When the client receives a request, the cache server can perform the simple logic of adding two numbers, and it can then respond to the client by returning the results.
ServerSocket: One way to monitor incoming requests from client connections on a designated port is to utilize the pre-defined Java networking socket class called ServerSocket.
Socket: It is a predefined class of socket that connects to the server and can be used to establish the client endpoint.
Client program:
Within this program, we can create a basic client that connects to the cache server on port 5000. The client then sends a request with two inputs to the cache, which the server processes and returns the results of.
Filename: DistributedCacheClient.java
import java.net.*; import java.io.*; public class DistributedCacheClient { public static void main(String[] args) { try ( Socket soc = new Socket("localhost", 5000); BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream())); PrintWriter out = new PrintWriter(soc.getOutputStream(), true) ) { System.out.println("Put a comma between each of the two numbers:"); String input = userInput.readLine(); out.println(input); String response = in.readLine(); System.out.println("Sum received from server: " + response); } catch (IOException e) { e.printStackTrace(); } } }
Explanation:
The program mentioned above serves as an example of a client-server system. The client sends a request to the server, which responds to it. In this instance, the client provides the two inputs, the server processes them, and the client receives the processed data back.
Benefits of distributed caching:
Having the cache spread out over several sites enhances its availability. The majority of the cache is still accessible on other machines even in the event that one goes down. For continuous access to the whole cache, you can also make backups for every machine.
It gets slower and less useful very fast to have a very large cache on one system. However, by spreading out the data across different sites, each cache can stay light and compact, making it easier to search for the relevant item.