Skip to content

Add `last_used_at` to the agent token API mitigating Redis N+1 issues

What's the problem

Original title: Consider a way for how to return last_used_at when listing agent tokens in REST API without N+1 problems


The draft for the Agent Tokens API doesn't return the last_used_at field when listing the tokens to avoid N+1 issues on the database and redis.

See the entire discussion here: !85704 (comment 921506873).

TL;DR;

The last_used_at column is cached in Redis via cached_attr_reader :last_used_at. So, when loading multiple tokens on the "list" API endpoint, the presenter will hit Redis once per token record. This is considered a N+1 problem.

Proposal

  • Add a limit of two active tokens per agent, while allowing any existing agents with >2 tokens to continue uninterrupted.
  • Only fetch active tokens (non-revoked).
  • Add last_used_at to the agent tokens API response.
Edited by Pam Artiaga