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

feat: improve session extend performance #3948

Merged
merged 2 commits into from
Jun 6, 2024
Merged

Conversation

aeneasr
Copy link
Member

@aeneasr aeneasr commented Jun 6, 2024

This patch improves the performance for extending session lifespans. Lifespan extension is tricky as it usually is part of the middleware of Ory Kratos consumers. As such, it is prone to transaction contention when we read and write to the same session row at the same time (and potentially multiple times).

To address this, we:

  1. Introduce a locking mechanism on the row to reduce transaction contention;
  2. Add a new feature flag which toggles returning 204 no content instead of 200 + session.

Be aware that all reads on the session table will have to wait for the transaction to commit before they return a value. This may cause long(er) response times on /session/whoami for sessions that are being extended at the same time.

BREAKING CHANGES: Going forward, the /admin/session/.../extend endpoint will return 204 no content for new Ory Network projects. We will deprecate returning 200 + session body in the future.

Checklist

  • I have read the contributing guidelines.
  • I have referenced an issue containing the design document if my change
    introduces a new feature.
  • I am following the
    contributing code guidelines.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security vulnerability, I
    confirm that I got the approval (please contact
    security@ory.sh) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added or changed the documentation.

Further Comments

  • Add tests

This patch improves the performance for extending session lifespans. Lifespan extension is tricky as it usually is part of the middleware of Ory Kratos consumers. As such, it is prone to transaction contention when we read and write to the same session row at the same time (and potentially multiple times).

To address this, we:

1. Introduce a locking mechanism on the row to reduce transaction contention;
2. Add a new feature flag which toggles returning 204 no content instead of 200 + session.

Be aware that all reads on the session table will have to wait for the transaction to commit before they return a value. This may cause long(er) response times on `/session/whoami` for sessions that are being extended at the same time.
@aeneasr
Copy link
Member Author

aeneasr commented Jun 6, 2024

You can play around yourself with table locking in this guide using cockroach demo as your DB: https://www.cockroachlabs.com/docs/stable/select-for-update#examples

jonas-jonas
jonas-jonas previously approved these changes Jun 6, 2024
return sqlcon.HandleError(err)
}

trace.SpanFromContext(ctx).AddEvent(events.NewSessionLifespanExtended(ctx, s.ID, s.IdentityID, s.ExpiresAt))
Copy link
Contributor

Choose a reason for hiding this comment

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

This must be moved outside of the transaction into a deferred function call in order not to send duplicate events when the TX automatically retries (see UpsertSession).

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch - thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

resolved

persistence/sql/persister_session.go Show resolved Hide resolved
@aeneasr aeneasr marked this pull request as ready for review June 6, 2024 12:59
Copy link

codecov bot commented Jun 6, 2024

Codecov Report

Attention: Patch coverage is 91.07143% with 10 lines in your changes missing coverage. Please review.

Project coverage is 78.18%. Comparing base (1a70648) to head (d416575).
Report is 4 commits behind head on master.

Files Patch % Lines
session/handler.go 40.00% 4 Missing and 2 partials ⚠️
persistence/sql/persister_session.go 86.66% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3948      +/-   ##
==========================================
+ Coverage   78.10%   78.18%   +0.08%     
==========================================
  Files         362      362              
  Lines       25329    25433     +104     
==========================================
+ Hits        19783    19885     +102     
  Misses       4028     4028              
- Partials     1518     1520       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aeneasr aeneasr enabled auto-merge (squash) June 6, 2024 15:31
@aeneasr aeneasr disabled auto-merge June 6, 2024 15:36
@aeneasr aeneasr merged commit 4e3fad4 into master Jun 6, 2024
29 checks passed
@aeneasr aeneasr deleted the improve-session-extend branch June 6, 2024 15:37
fenech pushed a commit to nbycomp/kratos that referenced this pull request Jun 14, 2024
This patch improves the performance for extending session lifespans. Lifespan extension is tricky as it is often part of the middleware of Ory Kratos consumers. As such, it is prone to transaction contention when we read and write to the same session row at the same time (and potentially multiple times).

To address this, we:

1. Introduce a locking mechanism on the row to reduce transaction contention;
2. Add a new feature flag that toggles returning 204 no content instead of 200 + session.

Be aware that all reads on the session table will have to wait for the transaction to commit before they return a value. This may cause long(er) response times on `/session/whoami` for sessions that are being extended at the same time.

BREAKING CHANGES: Going forward, the `/admin/session/.../extend` endpoint will return 204 no content for new Ory Network projects. We will deprecate returning 200 + session body in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants