Search code examples
nginxmiddlewarehaproxy

Reverse Proxy Locking feature


I want to request a Microsoft API which is not protected for concurrent access (pull the data with a GET, then Push it with a POST).

To prevent any weird behavior, I want to use a lock when I'm accessing this Api.

The easiest way I've found out (without messing up the code) is to create a middleware service (it will be targeted instead of the original one). When requested, it could save a Lock in a redis, and forward the request to Microsoft. When it's done, the lock is released.

Then if another request is coming to the server, it will be denied, and I'll be able to perform an exponential backoff until the lock is free.

My question is : Do I Have to code this thing, or is this a feature which could be found in an existing reverse proxy ?


Solution

  • To do this in a highly available way, I believe you would need to do what you wrote and use some sort of distributed lock to determine if the API was in use.

    However, if high availability is not required, you could use a single HAProxy instance with maxconn set to 1 for that server. You would also want to set timeout queue to something short so that you could handle the 503 response and do the exponential backoff you mention.

    backend microsoft_api_backend
        timeout queue 2s
        server microsoft_api 1.1.1.1:80 check maxconn 1
    

    In Nginx, you could do something equivalent:

    upstream microsoft_api {
      server 1.1.1.1:80 max_conns=1
      queue 1 timeout=2
    }
    
    server {
        location / {
            proxy_pass http://microsoft_api;
        }
    }