Retry request on a different backend after initial backend returns 501 status?

Hi,

We run HAProxy in front of a custom Ruby application. We’re in the middle of a major version upgrade on that custom Ruby app, and we’d like to start using it for some of our traffic in parallel with the older version, but the new version doesn’t yet handle every kind of request. In some cases we can determine based on the URL whether a request can go to the new app instead of the old app, but in others there’s some complex business logic we need to check before knowing for sure that the new app can handle the request.

In these cases, I’m hoping we can:

  • send the request to the new app version, and have it check the business rules
  • if it can handle the request, it handles it
  • if not, it signals to HAProxy somehow (a 501 Not Implemented response, perhaps?) that it’s unable to handle the request
  • in that case, HAProxy should retry the request on the old version of the app (ie: by retrying on a different backend)

I’d like to avoid redirecting the user if at all possible, since I want this transition to be transparent to users. I’m hoping that with the layer 7 retry features in HAProxy 2.x that there’s a way to do this, but I can’t quite figure out where to start. Has anyone done something like this before?