Skip to content

add stateful session load balancer support - #2469

Closed
ramaraochavali wants to merge 6 commits into
istio:masterfrom
ramaraochavali:fix/stateful_session
Closed

ramaraochavali wants to merge 6 commits into
istio:masterfrom
ramaraochavali:fix/stateful_session

Conversation

@ramaraochavali

Copy link
Copy Markdown
Contributor

API support needed for istio/istio#39740 and istio/istio#37910

@istio-testing istio-testing added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 30, 2022
// Path to set for the cookie.
string path = 2;
// Lifetime of the cookie.
google.protobuf.Duration ttl = 3 [(google.api.field_behavior) = REQUIRED];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have a default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. We can have.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on a TTL - probably short ( 1h ?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assume this will not interfere with application cookie ttl

};

// Standard load balancing algorithms that require no tuning.
enum SimpleLB {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure moving these out is a backwards compatible change in proto?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not actually

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not - but why is it required for this PR ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is not a must to change this.

You can just add the session sticky in

oneof lb_policy {

// consistently till the backend host on which the session lives goes down.
message StatefulSession {
// Load balancer to use for the initial request, before a session is established.
SimpleLB load_balancer = 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why simpleLB here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from envoy doc

HTTP session stickiness in Envoy is generally achieved through hash-based load balancing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is one of the benefits of using stateful session. See my comments above the field

and from Envoy docs

The case where a non hash-based load balancer (Random, Round Robin, etc.) is used and session stickiness is still required. If stateful sessions are enabled in this case, requests for new sessions will be routed to the corresponding upstream host based on the result of load balancing. Requests belonging to existing sessions will be routed to the session’s upstream host.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the 2 features are separated - use hash load balancer for affinity, use the cookie for exact routing together with another load balancer.

Using both cookie and hash is probably possible but doesn't make a lot of sense.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@costinm statefull affinity feature is orthogonal to LB choice. Theoretically it should be usable with any LB, including the hash-based algorithms. This is because hash-based LB algorithms only provide "soft-affinity", I.e. when the backend set changes, at least 1/N of the sessions will land on new backend hosts which will break affinity.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand - my point was that there is little reason to use hash-based affinity when session is enabled ( since it provides even stronger stickiness). It is better to combine 'stateful session' with load-based or random or one of the other LBs.
Hash would work - just don't think it's a good choice.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, makes sense.

};

// StatefulSession load balancer load balances across backend hosts using 'strong' stickiness.
// This load balancer enforces stable session stickiness by routing requests to the same backend

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case one backend instance is down, how will this work? I do not see any explicitly saying about this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure i can get the answer, i am asking if the one that session indicates is down. How does envoy do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a further question, what if the instance is down and actually not in the eds?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the instance that hosts the session is down, session is recreated on another node.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is recreated on another node - using the LB policy in effect. Sticky/stateful just means 'original dest' routing is used
when the cookie with the IP is present, otherwise regular LB takes place.

// Name of the cookie.
string name = 1 [(google.api.field_behavior) = REQUIRED];
// Path to set for the cookie.
string path = 2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main problem is path.

Normally in envoy session affinity can be set in routes - so map /example to example.ns.svc, and the path of the cookie will be set to /example

If another route /example2 is also pointing to example.ns - the path will have to be /example2.

In Istio, having the path in DR - which is associated with a destination - it is not possible to do that.

With the Gateway API, sticky session would be attached to a route - and we could generate a working config by setting a per route config.

Please add a lot of docs on this field - with examples and what is not possible ( effectively a destination can only be mapped to a single path ).

This is very important because you may have
example.com/example -> example.svc - with stickyEndpoint1
example.com/admin -> admin.svc with stickyEndpoint2.

The admin and example can't have same path - and path can't be set to "/" unless there is a single service using sticky sessions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather not have path in the API then have it broken and confusing. Without path - we use "/", and it works as expected for normal mesh calls ( as long as user doesn't have routes that redirect to multiple backends ), and is mostly broken for ingress ( only one of the backends for a hostname can be sticky )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@howardjohn

howardjohn commented Aug 31, 2022 via email

Copy link
Copy Markdown
Member

oneof lb_policy {
SimpleLB simple = 1;
ConsistentHashLB consistent_hash = 2;
StatefulSession stateful_session = 5;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. I would leave lb_policy as is - and add the stateful_session as a separate option, since it applies to any lb_policy. That doesn't require shuffling protos around and seems safer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check if consistent_hash is allowed with stateful_session - I don't see why not, AFAIK envoy will use the cookie if present or whatever LB is set.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In gRPC the way it is proposed in https://github.com/grpc/proposal/blob/e49869d41d91ca5e0ea538c833a1480f90b348a0/A55-stateful-session-affinity.md the "child policy" could be consistent_hash (ring_hash) so if the cookie is not present it will go to the child policy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems very reasonable - users who want sticky (stateful) sessions probably also want consistent_hash or maglev, and
that may use a different cookie or parameters.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@costinm

costinm commented Aug 31, 2022 via email

Copy link
Copy Markdown
Contributor

@sanjaypujare

Copy link
Copy Markdown

@costinm

costinm commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

IMO we should not mix LB - which selects an endpoint based on different rules, including affinity - with session stickiness, i.e. regardless of LB go to this specific IP:port.

I did more research on how other implementations work - in particular when stickiness is exposed trough gateways or other
infrastructure is involved - I am leaning more towards just "enable sticky sessions" in the API - and maybe the duration, and
leaving the name of the cookie, path, etc out of the API. If we integrate with other infra - they may have already defined cookies
and mechanisms to attach them to paths. I don't think use defining a different cookie name for each service is that valuable or needed, and if we get requests we can add it later. And for the path - I would add it after we have a good understanding on how
it'll work with gateways and routing.

A smaller PR with just 'enable sticky' and time would move faster and allow us to get feedback - we can add more later, it's harder
to remove.

// Load balancer to use for the initial request, before a session is established.
SimpleLB load_balancer = 1;
// The session state to use.
oneof session_state {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not required too, add it when we need to support another option would not break compat

@costinm

costinm commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

One more question - I've looked at proxyless gRPC and gateway use cases, the client or browser handles the cookie jar.

For 'mesh' use case - is the client-side sidecar holding a cookie jar when making the requests ? Or does the user have to change their code to track cookies ?

And if the client is gRPC ( with envoy sidecar for security/telemetry/etc - not proxyless ) - does gRPC use a cookie jar already ?

If you find out - please add it to the comments, in particular if user code needs to track the cookie, I haven't figured it out from envoy docs.

@ramaraochavali

Copy link
Copy Markdown
Contributor Author

If you find out - please add it to the comments, in particular if user code needs to track the cookie, I haven't figured it out from envoy docs.

Yes. User code has to track the cookie. And we are supporting this envoyproxy/envoy#22475 in Envoy so that non browser based apps can use this header to track instead of cookie.

@ramaraochavali

Copy link
Copy Markdown
Contributor Author

and maybe the duration, and leaving the name of the cookie

I think leaving path seems OK. But name should be given. We can't assume/standarize the name from some annotations. If name is not given we can derive based on other infrastructure data as you mentioned. But for typical Istio/envoy use case, name is important

@ramaraochavali

Copy link
Copy Markdown
Contributor Author

@costinm I agree with most of the comments except the name of cookie. If we agree on that, I will delink it from LB policy and introduce statefulSesstion config, if provided, sticky ness would be enabled.

@costinm

costinm commented Sep 2, 2022 via email

Copy link
Copy Markdown
Contributor

Signed-off-by: Rama Chavali <[email protected]>
@istio-testing istio-testing added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 2, 2022
@ramaraochavali

Copy link
Copy Markdown
Contributor Author

@costinm changed. PTAL.

@costinm costinm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better - left few cosmetic/docs comments and waiting for others to review, but I think it's pretty close.

// Name of the cookie to be used. If not specified, environment specific
// configuration is used.
string name = 1;
// Session duration. If not specified, a duration of 1 hour is used.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it session duration or cookie duration ? Normally ( java ) the session can last indefinitely, as long as requests are received that refresh the cookie.
I would assume envoy does renew the cookie as well.

It is not very common or useful to have a limit on the entire session.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. It is duration of cookie.

uint64 minimum_ring_size = 4 [deprecated=true];
};

// Stateful Session configuration.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more comments here on client changes requirement ( use a cookie jar ).

And maybe some mention this is mainly for service-to-service, the gateway behavior is not yet supported ( since a hostname may route to different backends - we don't have a way to represent this AFAIK, cookie is single IP for single cluster )

// When the backend host that hosts the session goes down, sesion is recreated
// on another backend host. A new backend host is selected based on the the configured
// load balancer policy.
StatefulSession stateful_session_config = 5;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit allergic to using 'config', 'policy', 'data' as suffix to fields.
There is nothing on the API that is not a config - or used as policy or data.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wont increase your allergy :-)

google.protobuf.Duration duration = 2;
}
// The session state to use.
oneof session_state {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never understood oneof with a single value or all this nesting in protos.

Just one message - StatefulSession - with 2 fields ( duration, cookie_name ) would be easier to use and less confusing.
Nothing exclusive about having either cookie or header or request parameter, it is possible to configure more than one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Envoy may not support more than one now - but I wouldn't design a long-term API around limitations on the first implementation.

Persistent sessions are not new - and it is clear other forms of tracking will need to be added, so better to have the correct API with comments explaining that current version of envoy only supports one. Note that the Istio APIs are not officially 'envoy only',
and we are starting to support the multi-vendor K8S Gateway APIs.

As an extra benefit - it would allow us to avoid the nested message and oneof which are pretty confusing for both devs and users.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not saying Envoy may not support others. All I am saying the state would be exclusive i.e. either cookie or header or some thing else. But not combination of all. I can drop oneof here but I think nested message would help us to add oneof later if needed rather than spraying fields across.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree with removing oneof here. And once we need another option, we can add it back, which is compatable

Signed-off-by: Rama Chavali <[email protected]>
@costinm

costinm commented Sep 26, 2022 via email

Copy link
Copy Markdown
Contributor

@va7ish

va7ish commented Sep 27, 2022

Copy link
Copy Markdown

the Service annotation is more clearly a property of the service, indicating the associated workloads require and use persistent sessions.

Where would you apply the annotation to? Is this for a K8s Service or some other construct in Istio?
If the former then Istio would still need to configure the clients through xDS to inject and handle some sort of affinity tag (I.e. cookie). This also means that two different service subsets must must still be defined by a single K8s service, does it?

@costinm

costinm commented Sep 27, 2022 via email

Copy link
Copy Markdown
Contributor

@ramaraochavali

Copy link
Copy Markdown
Contributor Author

. For the annotation name bike-shedding, existing names I found are:

  • nginx.ingress.kubernetes.io/affinity: "cookie" ( plus few more
    annotations to specify the name, expires )
  • traefik.ingress.kuberenetes.io/affinity: "true" ( plus "
    trafik.ingress.kubernetes.io/session-cookie-name" )
  • router.openshift.io/cookie_name: "name"
  • run.googleapis.com/sessionAffinity: true - for CloudRun ( as an extension
    to KNative service spec )

I personally think 'persistent session' is a better name than 'affinity'
since we already use affinity for the weaker variant, and
java has used this term since ~2000, but happy with any other name the
community want.

I am fine with that. Should we use traffic.istio.io/session-persistence-cookie and move forward?

@costinm

costinm commented Sep 28, 2022 via email

Copy link
Copy Markdown
Contributor

@costinm

costinm commented Oct 11, 2022 via email

Copy link
Copy Markdown
Contributor

@ramaraochavali

Copy link
Copy Markdown
Contributor Author

@costinm @howardjohn any suggestions on annotation naming? I would like to move them to annotations and implement it.

@costinm

costinm commented Oct 21, 2022 via email

Copy link
Copy Markdown
Contributor

@istio-testing

Copy link
Copy Markdown
Collaborator

@ramaraochavali: PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@sanjaypujare

Copy link
Copy Markdown

May be it possible to record the cluster in the future. I will have a try. 🤔

@wbpcode have you been able to try it? Are there any plans to add support for weighted clusters? Any idea what changes would be required?

@arhamGH

arhamGH commented Feb 17, 2023

Copy link
Copy Markdown

Any idea when this can be merged ?

@howardjohn howardjohn removed the needs-rebase Indicates a PR needs to be rebased before being merged label May 15, 2024
@istio-policy-bot istio-policy-bot added the lifecycle/automatically-closed Indicates a PR or issue that has been closed automatically. label May 15, 2024
@ramaraochavali ramaraochavali reopened this Sep 4, 2026
@ramaraochavali
ramaraochavali requested a review from a team as a code owner September 4, 2026 11:03
@istio-testing istio-testing added the needs-rebase Indicates a PR needs to be rebased before being merged label Sep 4, 2026
@istio-testing

Copy link
Copy Markdown
Collaborator

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ramaraochavali ramaraochavali reopened this Sep 4, 2026
@istio-testing

Copy link
Copy Markdown
Collaborator

@ramaraochavali: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
gencheck_api 6e73a6c link true /test gencheck
release-notes_api 6e73a6c link false /test release-notes
build_api 6e73a6c link true /test build
Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@ramaraochavali

Copy link
Copy Markdown
Contributor Author

i will open a fresh PR

@ramaraochavali
ramaraochavali deleted the fix/stateful_session branch September 9, 2026 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lifecycle/automatically-closed Indicates a PR or issue that has been closed automatically. needs-rebase Indicates a PR needs to be rebased before being merged release-notes-none Indicates a PR that does not require release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.