---
metadata:
  - name: generator
    content: Diplodoc Platform v5.70.2
alternate:
  - https://ydb-platform--ydb.viewer.diplodoc.com/en/security/caching-authentication-results.md
  - https://ydb-platform--ydb.viewer.diplodoc.com/ru/security/caching-authentication-results.md
  - href: https://ydb-platform--ydb.viewer.diplodoc.com/en/security/caching-authentication-results.md
    type: text/markdown
    title: Markdown version
  - href: https://ydb-platform--ydb.viewer.diplodoc.com/en/llms.txt?revision=d94cda5622eadbc2010c4ce972644123d8f317b6&version=v26.3
    rel: describedby
sourcePath: en/core/security/caching-authentication-results.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ydb-platform--ydb.viewer.diplodoc.com/en/llms.txt?revision=d94cda5622eadbc2010c4ce972644123d8f317b6&version=v26.3

# Caching authentication results

During authentication, the user session receives an [authentication token](https://ydb-platform--ydb.viewer.diplodoc.com/en/concepts/glossary.md#auth-token), which is sent with each request to the YDB cluster. Since YDB is a distributed system, requests are processed on different [nodes](https://ydb-platform--ydb.viewer.diplodoc.com/en/concepts/glossary.md#node). Upon receiving a request, each node independently [verifies the authentication token](https://ydb-platform--ydb.viewer.diplodoc.com/en/security/authentication.md#token-validation) and, upon successful verification, creates a [user token](https://ydb-platform--ydb.viewer.diplodoc.com/en/concepts/glossary.md#user-token).

The authentication token and the user token are different entities. The authentication token is sent by the client. The user token is created inside the node based on the verification result, contains the [SID](https://ydb-platform--ydb.viewer.diplodoc.com/en/concepts/glossary.md#access-sid) of the user and groups, and is used for [authorization](https://ydb-platform--ydb.viewer.diplodoc.com/en/security/authorization.md). It is stored only on a specific node and is not sent to the client.

The node stores the user token in the cache. The record key includes the authentication token and the verification context, such as the database, attributes, and requested [access rights](https://ydb-platform--ydb.viewer.diplodoc.com/en/concepts/glossary.md#access-right). A subsequent request uses the cached record only if the key matches.

<!-- source: en/security/_assets/user-token.md -->
```mermaid
sequenceDiagram
    actor user as User
    participant node as YDB node
    participant cache as Node cache
    participant auth as Authentication subsystem

    user->>node: First query with authentication token
    node->>cache: Find record by key
    cache-->>node: Record not found
    node->>auth: Check authentication token
    auth-->>node: Check result
    node->>node: Create user token
    node->>cache: Save user token
    node-->>user: Process query

    user->>node: Next query with the same key
    node->>cache: Find record by key
    cache-->>node: User token
    node-->>user: Process query
```
<!-- endsource: en/security/_assets/user-token.md -->

The user token lifetime and other caching aspects are configured in [the `auth_config` section of the YDB configuration](https://ydb-platform--ydb.viewer.diplodoc.com/en/reference/configuration/auth_config.md#caching-auth-results).

## Cache record lifetime

A record with a user token is removed from the cache if one of the following conditions is met:

- The record has not been used for `auth_config.life_time`. Only a request that matches the cache key (authentication token and verification context) updates the last use time of the record.
- The record has expired. For login and password tokens and external identity provider tokens, the record lifetime is determined by the authentication token lifetime. For requests signed with an access key, `auth_config.as_signature_expire_time` is used; for other types, `auth_config.expire_time` is used.

An error is considered retryable if the verification can be repeated without changing the request. The type of error is determined by the corresponding authentication subsystem.

{% note warning %}

On a retryable update error, the node continues to use the stored user token. Therefore, deleting an account or changing the user's membership in [groups](https://ydb-platform--ydb.viewer.diplodoc.com/en/security/authorization.md#group) may take effect on a specific node only after a successful update or removal of the record from the cache.

[ACL](https://ydb-platform--ydb.viewer.diplodoc.com/en/concepts/glossary.md#access-control-list) are not cached in the user token, so ACL changes are applied at the next permission check.

{% endnote %}

<!-- source: en/security/_assets/user-token-lifecycle.md -->
```mermaid
sequenceDiagram
    actor user as User
    participant node as YDB node
    participant cache as Node cache
    participant auth as Authentication subsystem

    user->>node: Query with the same data
    node->>cache: Find record
    cache-->>node: Record found
    Note right of cache: life_time countdown restarts

    opt Update time reached
        node->>auth: Re-check authentication token
        alt Successful check
            auth-->>node: Check result
            node->>node: Create new user token
            node->>cache: Update record
        else Retryable error
            auth-->>node: Error
            Note right of node: Schedule retry
        else Permanent error
            auth-->>node: Error
            node->>cache: Stop using user token
        end
    end

    alt Record was not used for life_time or expired
        node->>cache: Delete record
    end
```
<!-- endsource: en/security/_assets/user-token-lifecycle.md -->

## Updating user tokens {#refreshing-user-tokens}

For refreshable authentication tokens, the node periodically re-validates and replaces the user's token in the cache. Depending on the authentication method, validation is performed locally or requires contacting an external system. After successful validation, the user's SID and group SIDs are updated.

The `auth_config.refresh_time` parameter sets the maximum interval before the next validation. The specific moment is chosen in the range from `refresh_time/2` to `refresh_time`.

After a retryable error, retries continue until a successful update, a permanent error, or the record is removed from the cache. The intervals between attempts are set by the `min_error_refresh_time` and `max_error_refresh_time` parameters. For login/password tokens and external identity provider tokens, attempts also stop after the authentication token expires.

Possible causes of errors when validating and updating cached results include, but are not limited to:

- Network errors and timeouts when contacting the external authentication system.
- Unavailability or overload of the external authentication system.
- Unavailability of the local security state required for validation.
- Overload of the YDB node or query processing timeout.
