Skip to content
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

backend calls are not sticky (AKA Session affinity) #7104

Closed
tobi-or-not-tobi opened this issue Mar 31, 2020 · 5 comments · Fixed by #7115
Closed

backend calls are not sticky (AKA Session affinity) #7104

tobi-or-not-tobi opened this issue Mar 31, 2020 · 5 comments · Fixed by #7115
Assignees
Labels
bug Something isn't working Resilience How storefront behaves during chaotic conditions
Projects
Milestone

Comments

@tobi-or-not-tobi
Copy link
Contributor

tobi-or-not-tobi commented Mar 31, 2020

With a backend running multiple pods/nodes, the backend will not been able to send cache invalidations cross a cluster when sub-sequential requests come in too soon. Moreover, if multiple requests are being scattered over multiple nodes, there's latency and unnecessary resources consumed.

Spartacus should interact with a single backend as much as possible for a single client. This is traditionally called "sticky sessions".

CCv2 is partially prepared for this. It adds a ROUTE cookie to the response. This cookie however is not configurable and uses no SameSite policy. This means that a decoupled storefront is likely going to fail to use it, as it's acting on a different domain. Today only chrome seem to have issues with this, but going forward it is expected to see more browsers following.

That being sad, Spartacus today doesn't use the ROUTE cookies. Cookies aren't send with any request at all. In order to leverage the ROUTE cookie, the following must be done:

  • Use the withCredentials: true option in the http client, so that cookies are send with each request
  • Configure the commerce backend with an additional CORS filter (Allow-Origin-With-Credentials:true) to ensure that the cookies are passing the filter.
@tobi-or-not-tobi tobi-or-not-tobi self-assigned this Mar 31, 2020
@tobi-or-not-tobi tobi-or-not-tobi added bug Something isn't working MUST labels Mar 31, 2020
@tobi-or-not-tobi tobi-or-not-tobi changed the title placeholder: fix sticky backend calls backend calls are not sticky Mar 31, 2020
@tobi-or-not-tobi
Copy link
Contributor Author

In order to ensure that Spartacus (or better: angular) will send cookies with a request, requests must be done with withCredentials: true option. This can be done in an interceptor so that we do this in a generic way and ensure that no request forget this.

Once this is in place, an error will be thrown, as an additional backend configuration must be done. A new configuration property must be added corsfilter.ycommercewebservices.allowCredentials = true.

@tobi-or-not-tobi
Copy link
Contributor Author

tobi-or-not-tobi commented Mar 31, 2020

The SameSite=None policy will be has been added in ccv2 in release a0096 (see https://jira.hybris.com/browse/MTD-12330)

@tobi-or-not-tobi tobi-or-not-tobi added this to BACKLOG in Aimed 2.0 via automation Apr 1, 2020
@tobi-or-not-tobi tobi-or-not-tobi moved this from BACKLOG to In Progress in Aimed 2.0 Apr 1, 2020
tobi-or-not-tobi added a commit that referenced this issue Apr 1, 2020
Sends cookies to each OCC request, with the withCredentials flag in the http client.
This requires a new OCC configuration, which (currently) defaults to false.

An additional CORS configuration is required to ensure that a decoupled storefront is allowed to pass cookies into the request.

closes #7104
@tobi-or-not-tobi tobi-or-not-tobi moved this from In Progress to To QA in Aimed 2.0 Apr 1, 2020
@tobi-or-not-tobi
Copy link
Contributor Author

QA:

backend: {
  occ: {
    baseUrl: 'https://storefront.c39j2-walkersde1-d4-public.model-t.cc.commerce.ondemand.com',
    useWithCredentials: true
  }
}
  • verify that a response cookie ROUTE is retrieved for the first OCC request
  • verify that the ROUTE cookie is added to all sub-sequential requests

@tobi-or-not-tobi tobi-or-not-tobi changed the title backend calls are not sticky backend calls are not sticky (AKA Session affinity) Apr 2, 2020
tobi-or-not-tobi added a commit that referenced this issue Apr 2, 2020
…ttp requests (#7139)

This could be considered a feature as well. It's been introduced in 2.0, but a backport was required for customers who scale out their API. In order to configure this behaviour (which is turned of by default), you must provide the interceptor in a module (i.e. in the AppModule)

`providers: [withCredentialsInterceptorProvider]`

Additionally, a CORS configuration is required in the backend to pass requests from another domain. The following configuration must be installed:

`corsfilter.ycommercewebservices.allowCredentials=true`

This can be done either in the backoffice or by using project.properties.

closes #7104
tobi-or-not-tobi added a commit that referenced this issue Apr 2, 2020
…ttp requests (#7146)

This could be considered a feature as well. It's been introduced in 2.0, but a backport was required for customers who scale out their API. In order to configure this behaviour (which is turned of by default), you must provide the interceptor in a module (i.e. in the AppModule)

`providers: [withCredentialsInterceptorProvider]`

Additionally, a CORS configuration is required in the backend to pass requests from another domain. The following configuration must be installed:

`corsfilter.ycommercewebservices.allowCredentials=true`

This can be done either in the backoffice or by using project.properties.

closes #7104
@KateChuen KateChuen self-assigned this Apr 3, 2020
@KateChuen
Copy link
Contributor

  1. When we first load SPA, we get the ROUTE cookie in the response of the first occ call.

Screen Shot 2020-04-03 at 11 17 10 AM

  • Then we get the ROUTE cookie in the subsequent occ requests.

Screen Shot 2020-04-03 at 11 16 38 AM

Screen Shot 2020-04-03 at 11 16 55 AM

  1. But then, when we do a login, we get the ROUTE cookie in the response again (I'm guessing it's because we're starting a new session?). But we have the "set-cookie" parameter displayed twice. ROUTE cookie is displayed in a separate "set-cookie" line. @tobi-or-not-tobi can you confirm it's ok please?

Screen Shot 2020-04-03 at 11 33 39 AM

  • Then we get the ROUTE cookie in the subsequent occ requests within the same session.

Screen Shot 2020-04-03 at 11 18 36 AM

Screen Shot 2020-04-03 at 11 19 37 AM

@tobi-or-not-tobi
Copy link
Contributor Author

@KateChuen
regarding your 2nd observation, I wasn't aware, but this is actually fine. The 2nd creation of the cookie isn't necessary, but doesn't harm either.

Your QA work seem to confirm the aim of this ticket, good stuff.

@KateChuen KateChuen moved this from To QA to Done in Aimed 2.0 Apr 6, 2020
@giancorderoortiz giancorderoortiz added the Resilience How storefront behaves during chaotic conditions label Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Resilience How storefront behaves during chaotic conditions
Projects
No open projects
4 participants