Skip to content

perf(api) : API writes the login session to MongoDB on every request #3837

Backlog

Description

@Aryainguz

What happened?

Every API request from a signed-in browser updates that user's document in the sessions collection, and the response waits for the write.

On each request that leaves the session unchanged, express-session calls store.touch() to push its expiry forward. connect-mongo's touch() runs an updateOne unless touchAfter is set, and api-app.ts doesn't set it. So a dashboard or search page rewrites the same session document once for every chart query and other API call it makes.

Measured in the integration environment (API and MongoDB on the same machine), signed-in GET /me, two runs each:

main touchAfter: 1h
Session writes, 300 sequential requests 300 0
Session writes, 250 parallel requests 250 0
Sequential latency 3.27 ms/req 2.41 ms/req
10 rounds × 25 parallel requests, p95 38.5 ms 24.5 ms

The gap should be larger against a remote MongoDB or DocumentDB, where each write is a network round trip.

Steps to reproduce

  1. yarn dev, sign in.
  2. mongosh "<MONGO_URI>" --eval 'db.sessions.find({}, { expires: 1 })'
  3. Open any dashboard and run step 2 again: expires has moved. It moves on every API request.

Proposed fix

Set touchAfter to one hour, and backfill lastModified on existing sessions so the change also applies to users who are already signed in.

Activity

  1. added
    enhancement
    Needs Feedback
    on Nov 26, 2026
  2. changed the title [-]API writes the login session to MongoDB on every request[/-] [+]perf : API writes the login session to MongoDB on every request[/+] on Nov 26, 2026
  3. changed the title [-]perf : API writes the login session to MongoDB on every request[/-] [+]perf(api) : API writes the login session to MongoDB on every request[/+] on Nov 26, 2026
  4. Aryainguz commented on Nov 26, 2026

    @Aryainguz
    ContributorAuthor

    didn't flag it as bug, since I think its more of a performance optimization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Feedbackenhancementexternal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions