-
Notifications
You must be signed in to change notification settings - Fork 719
POC for a session/cookie based sitcky load balancer implementation. #764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Nice and tidy PoC @fitzoh . It was easy to follow 👍 . Some thoughts: I can imagine that in some architectures other consumers will not be aware of other service instances, only the service id. I'm saying this as service instances are a bit ephemeral and consumers shouldn't need to know what service instance they should point to. Their only outlet should be the load balancer. The strategy for checking what service instance to "stick to" should maybe be configurable so that any use-case can be plugged in? Some developers maybe have Regarding the usage of context in this case, I think one needs to make changes to the So if there is an attribute value mapped to key And then the
and finally shoot your new
which will invoke If you want I could make some changes to your PoC, but would like to hear your opinions on this first :) |
Thanks @andaziar!
I'm struggling a little bit to follow this... For reference, I'm not super familiar with the library and haven't used it directly (I'm primarily here because I saw spring-cloud/spring-cloud-gateway#1176 (comment) and have a use case for sticky sessions in spring-cloud-gateway).
One caveat here... It wouldn't be sufficient to just grab the value out of the cookie/header, we also need to be able to propagate the selected header back to the user.
No objections here, but I'm definitely interested to hear what @OlgaMaciaszek has to say |
|
Hi @fitzoh @andaziar, sorry for getting back to you with a delay.
Apart from this, I think we can handle the cookies using I have created the first draft of possible Also, I'm going back to finishing up this PR now: #733 - will include the changes with passing along the |
I believe that's what I'm currently doing: // Check if the exchange has a cookie that points to a valid ServiceInstance
return serviceInstanceFromCookie(exchange, instances)
// if it does, then route to that server
.map(instance -> Mono.just((Response<ServiceInstance>) new DefaultResponse(instance)))
// otherwise we'll let the delegate pick a server
.orElseGet(() -> delegate.choose(request))
// either way we should set/renew the cookie
.doOnNext(response -> setCookie(exchange, response));
👍
Cool, I'll take a look |
|
Hi @spencergibb , @OlgaMaciaszek Could you please let me know if the sticky session implementation is complete?Is there any docs on the usage? |
|
Hi, @fitzoh Would you like to continue working on this? If yes, please merge the current |
|
Interested in picking it back up, not sure on the timeline. |
|
Ok @fitzoh, could you give me some kind of estimate? If it's up to a month, we could wait. If it's longer, I'd rather get it done earlier. Let me know. |
Sorry for the delay @OlgaMaciaszek , I'd so go ahead and implement it |
Possible implementation for #689, meant as a discussion starter (no tests, not wired up to anything).
How it works
The
StickySessionLoadBalancerlooks for a server id in thescg-instance-idcookie (not married to that name).If a cookie value is found and that value is a valid service instance ID, the request is routed to that service instance.
If it is not found or if the value is invalid (perhaps the server is no longer active or unhealthy), then a delegate
ReactorServiceInstanceLoadBalancerand the request is routed to that server instead.Regardless of the path taken, the cookie is updated with the instance ID that the request was routed to.
Assumptions/Requirements
This implementation assumes that caller will pass a
ServerWebExchangeas theRequestContext.It is also dependent on
ServiceInstances having valid IDs (the field is nullable, so it might not be available, right?)Rough spots
I wasn't seeing any existing context usage, so I just took a stab at it. Casting from an
Objectfeels a little grossFuture work
Make the cookie name configurable
Make the way the cookie value is customized (people may want to encrypt/hash/do something to not expose server IDs)