Ask
58
@pumptest_pat ·

db session lookup adds 41ms to every request on neon, is that normal Sessions

Server sessions, sessions table in Postgres on Neon, serverless functions. Every authenticated request does one lookup by session token.

With the lookup: p50 around 68ms. With it stubbed out: p50 around 27ms. So the session is costing me 41ms on literally every request, which is more than the rest of my handler.

Is 41ms just what a database session costs on this stack, or is my setup wrong? The table has 4,000 rows and an index on the token.

8 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @burr_and_edge · 7mo ago · 3 replies

    41ms is not the cost of a session lookup, it is the cost of your connection. A primary key read against 4,000 rows is sub-millisecond in the database. What you are measuring is almost entirely connect plus TLS plus network round trip.

    In order:

    • Use the pooled connection string, not the direct one. Opening a fresh connection per invocation is most of your 41ms.
    • Check the region of your functions against the region of your database. Functions in one continent and Postgres in another is a flat 80-100ms tax that no query tuning will touch.
    • Then cache the session in whatever edge KV you have, keyed by the cookie, 60 second TTL. Most requests then never reach Postgres at all.

    After that a session lookup costs about 1ms and the debate about sessions versus tokens stops being about performance, which is where it should have been all along.

    87
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @serger_seth · 7mo ago

      The region mismatch is embarrassingly common because both defaults are sensible in isolation. Function defaulted to one region, database created from a laptop somewhere else, and you bought 90ms before anyone wrote any code.

      26
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
    • @rest_day_rita · 7mo ago

      If you add the KV cache, remember to bust it on logout and on ban, otherwise you have carefully rebuilt the token revocation problem you chose sessions to avoid. 60 seconds is short enough that most people accept it, but decide it rather than inherit it.

      14
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @p99_hana · 7mo ago

    Instrument before you change anything. Time the query itself, then time the whole session middleware, then time the handler. If the query is 0.8ms and the middleware is 41ms, everything you were about to do to the table is wasted effort.

    I have watched someone add three indexes to a table that was being read in under a millisecond.

    52
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @hemline_hana · 7mo ago

    Also ask how many of those requests needed to know who the user was. Static assets, public pages, cached routes, health checks. If your middleware runs on everything, you are paying 41ms on requests that would have been happy anonymous.

    33
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @late_stage_phd · 7mo ago · 2 replies

    Switch to JWT. This is exactly the problem stateless sessions were invented for - no lookup, no round trip, 0ms.

    16
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @burr_and_edge · 7mo ago

      It is what they were invented for and it will not fix this. You still verify the token, you still need user data for most pages so you make the round trip anyway, and now revocation is delayed. Meanwhile the actual cause - connection setup and possibly a cross-region hop - is still there and will show up in the next thing you build.

      Fix the connection, measure again, then decide. If it is 1ms you will not want to change anything.

      13
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @warranty_wes · 7mo ago

    Check whether your ORM is doing two queries. A lot of session adapters fetch the session and then fetch the user as a separate statement, so your "one lookup" is two round trips and the fix is a join or an include.

    11
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report