Forward and Reverse Proxy

Overview

Before talking about forward proxy and reverse proxy let’s talk about what is the meaning of proxy.
Basically proxy means someone or something is acting on behalf of someone.
In the technical realm, we are talking about one server is acting behalf of the other servers.

In this blog, we will talk about web proxies. So basically we have two types of web proxies:-

  • Forward Proxy
  • Reverse Proxy
The forward proxy is used by the client, for example:- web browser, whereas reverse proxy is used by the server such as web server.

Forward Proxy

In Forward Proxy, proxy retrieves data from another website on the behalf of original requestee. For example:- If an IP is blocked for visiting a particular website then the person(client) can use the forward proxy to hide the real IP of the client and can visit the website easily.
Let’s take another example to understand it more clearly. For example, we have 3 server
Client                      -> Your computer from which you are sending the request
Proxy Site               -> The proxy server, proxy.example.com
Main Web server    -> The website you want to see
Normally connection can happen like this 
In the forward proxy, the connection will happen like this
So here the proxy client is talking to the main web server on the behalf of the client.
The forward proxy also acts as a cache server. For example:- If the content is downloading multiple times the proxy can cache the content on the server so next time when another server is downloading the same content, the proxy will send the content that is previously stored on the server to another server. 

 Reverse Proxy

The reverse proxy is used by the server to maintain load and to achieve high availability. A website may have multiple servers behind the reverse proxy. The reverse proxy takes requests from the client and forwards these requests to the web servers. Some tools for reverse proxy are Nginx, HaProxy.
So let’s take the similar example as the forward proxy
Client                      -> Your computer from which you are sending the request
Proxy Site               -> The proxy server, proxy.example.com
Main Web server    -> The website you want to see
Here it is better to restrict the direct access to the Main Web Server and force the requests or requestors to go through Proxy Server first. So data is being retrieved by Proxy Server on the behalf of Client.
  • So the difference between Forward Proxy and Reverse Proxy is that in Reverse Proxy the user doesn’t know he is accessing Main Web Server, because of the user only communicate with Proxy Server.
  • The Main Web Server is invisible for the user and only Reverse Proxy Server is visible. The user thinks that he is communicating with Main Web Server but actually Reverse Proxy Server is forwarding the requests to the Main Web Server.

Equals implementation for Lazy loaded objects in Hibernate

Most of you already know about the standard way to implement equals and hashCode for a class. As an example lets consider a User class


public class User {
private Long id;
private String name;
private String address;
}

If we consider Id as unique identifier for logical equality of users then the equals and hashCode method can be written over “id”, as given below


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}

Now lets assume User is an entity thats managed by hibernate and is lazily loaded. So actually you will be interacting with the proxy(HibernateProxy) of User and not the actual User class.

Now in this the equals implementation of User class will fail. If we check for equality of normal instance of User class with the proxy instance of User the equals method will return false. The reason for failure is the class check i.e


if (getClass() != obj.getClass())
return false;

Since the class of Proxy user object is not User but a HibernateProxy class.

To overcome this problem hibernate comes with a utility class HibernateProxyHelper which is used to get the actual class if the object passed to it is of type HibernateProzy. The above piece of code can be replaced with


if (  HibernateProxyHelper.getClassWithoutInitializingProxy(obj) != getClass() ) {
return false;
}

Another problem with the implementation of equals in case of proxy objects is we cannot directly access the fields i.e


if (!id.equals(other.id))
return false;

as proxy objects can access only the methods of the underlying object so instead of directly accessing the field we have to use the getter method


if (!id.equals(other.getId()))
return false;

So the updated equals implemention that should work perfectly fine in case of proxy objects also will be as given below


public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (  HibernateProxyHelper.getClassWithoutInitializingProxy(obj) != getClass() ) {
return false;
}
User other = (User) obj;
if (id == null) {
if (other.getId() != null)
return false;
} else if (!id.equals(other.getId()))
return false;
return true;
}