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

Account cache optimization #5792

Merged
merged 12 commits into from
Mar 22, 2023
Merged

Account cache optimization #5792

merged 12 commits into from
Mar 22, 2023

Conversation

tnorling
Copy link
Collaborator

Today on every lookup of an account in the cache we iterate through every entry in the cache (including cache entries not belonging to MSAL), attempt to parse it into JSON and then validate that it matches our account shape. This is incredibly inefficient and becomes more inefficient with each additional thing stored in browser storage.

This PR adds a new cache entry (with a static key) which contains an array of keys to all of the accounts known to MSAL. Now when the accounts need to be enumerated the worst case number of cache entries touched and parsed is 1 + number of accounts.

A new configuration option is added which determines whether or not this new cache entry should be created on initialization. This is to help migrate apps using localStorage from versions before this change.

Follow up PR will address the same issue for id, access and refresh tokens.

@github-actions github-actions bot added msal-browser Related to msal-browser package msal-common Related to msal-common package msal-node Related to msal-node package labels Mar 13, 2023
Copy link
Contributor

@peterzenz peterzenz left a comment

Choose a reason for hiding this comment

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

This PR is still marked as draft, I'll re-review it once it's marked ready for review. Thanks!

@codecov-commenter
Copy link

codecov-commenter commented Mar 17, 2023

Codecov Report

Merging #5792 (8d67f51) into dev (81d34b4) will decrease coverage by 1.60%.
The diff coverage is 50.38%.

Flag Coverage Δ *Carryforward flag
msal-angular 96.63% <100.00%> (+0.13%) ⬆️
msal-browser 86.17% <76.00%> (-0.30%) ⬇️
msal-common 84.26% <76.00%> (-0.28%) ⬇️
msal-core ?
msal-node 80.62% <36.00%> (-2.78%) ⬇️
msal-node-extensions 52.08% <11.86%> (-23.56%) ⬇️ Carriedforward from e3bb2ed
msal-react 94.68% <100.00%> (ø)
node-token-validation 88.67% <50.00%> (-0.22%) ⬇️

*This pull request uses carry forward flags. Click here to find out more.

Impacted Files Coverage Δ
...ib/msal-browser/src/app/PublicClientApplication.ts 95.19% <ø> (-0.14%) ⬇️
lib/msal-common/src/network/NetworkManager.ts 86.66% <ø> (ø)
lib/msal-node/src/config/Configuration.ts 100.00% <ø> (ø)
...l-node-extensions/src/broker/NativeBrokerPlugin.ts 8.96% <8.96%> (ø)
...ib/msal-node/src/client/PublicClientApplication.ts 49.27% <21.21%> (-27.05%) ⬇️
lib/msal-node/src/utils/NetworkUtils.ts 40.00% <25.00%> (-60.00%) ⬇️
.../msal-node-extensions/src/error/NativeAuthError.ts 37.50% <37.50%> (ø)
lib/msal-browser/src/error/BrowserAuthError.ts 95.95% <50.00%> (-0.95%) ⬇️
lib/msal-common/src/error/ClientAuthError.ts 83.83% <50.00%> (-1.43%) ⬇️
lib/msal-common/src/response/ResponseHandler.ts 73.24% <50.00%> (-0.78%) ⬇️
... and 29 more

... and 50 files with indirect coverage changes

@tnorling tnorling requested a review from derisen as a code owner March 20, 2023 20:08
@github-actions github-actions bot added the samples Related to the samples apps for the library. label Mar 20, 2023
Copy link
Contributor

@peterzenz peterzenz left a comment

Choose a reason for hiding this comment

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

Just the one comment on the '-' separator. I'm likely missing something here. Thanks.

lib/msal-browser/src/config/Configuration.ts Show resolved Hide resolved
lib/msal-common/src/cache/CacheManager.ts Show resolved Hide resolved
Co-authored-by: Doğan Erişen <v-derisen@microsoft.com>
Copy link
Member

@sameerag sameerag left a comment

Choose a reason for hiding this comment

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

Lgtm. Have some qs on size.

@github-actions github-actions bot added the documentation Related to documentation. label Mar 22, 2023
@tnorling tnorling merged commit c4432b9 into dev Mar 22, 2023
@tnorling tnorling deleted the account-cache-optimization branch March 22, 2023 20:44
tnorling added a commit that referenced this pull request Mar 29, 2023
Follow up to #5792, addressing the same problem but for token lookups.

Today on every lookup of tokens in the cache we iterate through every
entry in the cache (including cache entries not belonging to MSAL),
attempt to parse it into JSON and then validate that it matches our
credential shape. This is incredibly inefficient and becomes more
inefficient with each additional thing stored in browser storage.

This PR adds a new cache entry which contains an object of keys to all
of the tokens known to MSAL for a given clientId. Now when the tokens
need to be enumerated the worst case number of cache entries touched and
parsed is 1 + number of tokens of the given type.

Example:

```js
key: msal.token.keys.<clientId>
value: {
  idToken: ["idToken-key-1", "idToken-key-2"],
  accessToken: ["accessToken-key-1", "accessToken-key-2"],
  refreshToken: ["refreshToken-key-1"]
}
```

---------

Co-authored-by: Thomas Norling <thnorlin@microsoft.com>
konstantin-msft pushed a commit that referenced this pull request Apr 3, 2023
Today on every lookup of an account in the cache we iterate through
every entry in the cache (including cache entries not belonging to
MSAL), attempt to parse it into JSON and then validate that it matches
our account shape. This is incredibly inefficient and becomes more
inefficient with each additional thing stored in browser storage.

This PR adds a new cache entry (with a static key) which contains an
array of keys to all of the accounts known to MSAL. Now when the
accounts need to be enumerated the worst case number of cache entries
touched and parsed is 1 + number of accounts.

A new configuration option is added which determines whether or not this
new cache entry should be created on initialization. This is to help
migrate apps using localStorage from versions before this change.

Follow up PR will address the same issue for id, access and refresh
tokens.

---------

Co-authored-by: Thomas Norling <thnorlin@microsoft.com>
Co-authored-by: Doğan Erişen <v-derisen@microsoft.com>
konstantin-msft pushed a commit that referenced this pull request Apr 3, 2023
Follow up to #5792, addressing the same problem but for token lookups.

Today on every lookup of tokens in the cache we iterate through every
entry in the cache (including cache entries not belonging to MSAL),
attempt to parse it into JSON and then validate that it matches our
credential shape. This is incredibly inefficient and becomes more
inefficient with each additional thing stored in browser storage.

This PR adds a new cache entry which contains an object of keys to all
of the tokens known to MSAL for a given clientId. Now when the tokens
need to be enumerated the worst case number of cache entries touched and
parsed is 1 + number of tokens of the given type.

Example:

```js
key: msal.token.keys.<clientId>
value: {
  idToken: ["idToken-key-1", "idToken-key-2"],
  accessToken: ["accessToken-key-1", "accessToken-key-2"],
  refreshToken: ["refreshToken-key-1"]
}
```

---------

Co-authored-by: Thomas Norling <thnorlin@microsoft.com>
@ghost
Copy link

ghost commented Apr 3, 2023

🎉@azure/msal-common@v12.0.0 has been released which incorporates this pull request.:tada:

We recommend upgrading to the latest version of @azure/msal-browser or @azure/msal-node to take advantage of this change.

Handy links:

@ghost
Copy link

ghost commented Apr 3, 2023

🎉@azure/msal-browser@v2.35.0 has been released which incorporates this pull request.:tada:

Handy links:

@ghost
Copy link

ghost commented Apr 3, 2023

🎉@azure/msal-node@v1.17.0 has been released which incorporates this pull request.:tada:

Handy links:

hectormmg pushed a commit that referenced this pull request Apr 5, 2023
Today on every lookup of an account in the cache we iterate through
every entry in the cache (including cache entries not belonging to
MSAL), attempt to parse it into JSON and then validate that it matches
our account shape. This is incredibly inefficient and becomes more
inefficient with each additional thing stored in browser storage.

This PR adds a new cache entry (with a static key) which contains an
array of keys to all of the accounts known to MSAL. Now when the
accounts need to be enumerated the worst case number of cache entries
touched and parsed is 1 + number of accounts.

A new configuration option is added which determines whether or not this
new cache entry should be created on initialization. This is to help
migrate apps using localStorage from versions before this change.

Follow up PR will address the same issue for id, access and refresh
tokens.

---------

Co-authored-by: Thomas Norling <thnorlin@microsoft.com>
Co-authored-by: Doğan Erişen <v-derisen@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Related to documentation. msal-browser Related to msal-browser package msal-common Related to msal-common package msal-node Related to msal-node package node-token-validation samples Related to the samples apps for the library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants