You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/identity-server/5.11.0/docs/setup/token-persistence.md
+87-3Lines changed: 87 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -127,15 +127,17 @@ By default, JWT access-token generation or validation triggers interactions with
127
127
128
128
### Why optimize JWT Acces token persistence?
129
129
130
-
In large-scale WSO2 Identity Server deployments, especially with millions of users and high concurrency, the number of tokens stored in the database can grow quickly, making it harder to scale. This can lead to reduced performance and lower Transactions Per Second (TPS) for token generation. For instance, a telecom provider with 1.4 million subscribers and a token generation rate of 1,000 per second might struggle with scaling using traditional methods like adding more nodes, partitioning the database, or running token cleanup scripts.
131
-
To address this, token persistence optimization helps by not storing access tokens. This approach avoids storing tokens during generation while still supporting essential features like token revocation and refresh grants, improving scalability and performance.
130
+
In large-scale WSO2 Identity Server deployments, especially with millions of users and high concurrency, the number of tokens stored in the database can grow quickly, making it harder to scale. This can lead to reduced performance and lower Transactions Per Second (TPS) for token generation. To address this, token persistence optimization helps by not storing access tokens. This approach avoids storing access tokens during generation while still supporting essential features like token revocation and refresh grants, improving scalability and performance.
132
131
133
132
-**Reduce database queries during token request**: When a token is issued, the **access token** will no longer be stored in the `IDN_ACCESS_TOKEN` table. Only the **refresh token** will be stored. As a result, authorization grants like **Client Credentials**, which do not issue a refresh token, will experience improved throughput due to the reduction in database query overhead.
134
133
-**Efficient database storage**: When tokens are stored in the persistent access_token mode, new entries are added to the database with each token request, even if the same refresh token is used. However, with the **non-persistent access token** feature enabled, only the **refresh token** is stored. If the current refresh token is still valid for the grant, no additional database rows will be added during token requests, leading to more efficient database storage.
135
134
-**Improved token validation**: With the **non-persistent access token** feature enabled, revoked tokens can be stored in the database. However, deployments have the option to opt out of storing revoked tokens and can instead listen for revoked token events, providing greater flexibility in token management. This approach is particularly beneficial when the Identity Server acts as a **Key Manager** for **WSO2 API Manager**, as the Gateway can self-validate JWTs without additional hops to the Key Manager, improving performance and reducing latency.
136
135
137
136
### Things to consider when using JWT access token persistence optimization
138
137
138
+
- This mode is particularly suitable for the following scenarios:
139
+
- When most token requests are based on authorization grants such as **Client Credentials**, which do not issue a refresh token.
140
+
- When **Refresh Token Grants** are configured **without refresh token rotation**, reducing the need for persistent token tracking.
139
141
- The **token persistence optimization** feature works only with **JWT access tokens**, as they can be self-validated.
140
142
- When enabling this feature in an existing setup:
141
143
-**Opaque token generation** will continue to work as expected for applications configured to use opaque tokens.
@@ -599,7 +601,7 @@ To address this, token persistence optimization helps by not storing access toke
599
601
```
600
602
601
603
??? Example "Postgres"
602
-
604
+
603
605
```sql
604
606
CREATE TABLE IF NOT EXISTS IDN_OAUTH2_REFRESH_TOKEN (
605
607
REFRESH_TOKEN_ID VARCHAR(255) PRIMARY KEY,
@@ -681,3 +683,85 @@ To address this, token persistence optimization helps by not storing access toke
681
683
[oauth.token_persistence]
682
684
retain_revoked_access_token = false
683
685
```
686
+
687
+
### Removing unused refresh tokens and revoke entries from the Database
688
+
689
+
!!! note
690
+
This section applies to database cleanup when the non-persistent access token feature is enabled. For persistent mode, refer to [Clean unused tokens from database](../../setup/removing-unused-tokens-from-the-database).
691
+
692
+
As you continue to use **WSO2 Identity Server (WSO2 IS)**, the number of **revoked**, **inactive**, and **expired** tokens increases in the `IDN_OAUTH2_REFRESH_TOKEN` table. When a token is revoked, a record is also added to the `IDN_OAUTH2_REVOKED_TOKENS` table. These tokens are retained for purposes such as **logging**, **auditing**, and **validation**.
693
+
694
+
However, over time, the accumulation of such records can impact overall server performance and increase the size of the database. To ensure optimal performance, it is recommended to periodically clean up unused and expired tokens.
695
+
696
+
The following sections guide you through the difference ways to perform cleanup and how to configure them.
697
+
698
+
- [**Token Cleanup via Stored Procedure** (Recommended)](#token-cleanup-via-stored-procedure)
699
+
- [**Configuring WSO2 Identity Server for token cleanup**](#configuring-wso2-identity-server-for-token-cleanup)
700
+
701
+
#### Token Cleanup via Stored Procedure
702
+
703
+
You can use the provided stored procedures to run a
704
+
token cleanup task periodically to remove the old and invalid tokens and clean up `IDN_OAUTH2_REVOKED_TOKEN` table.
705
+
Follow the instructions below to configure token cleanup using this
706
+
method.
707
+
708
+
!!! tip
709
+
It is safe to run these steps in read-only mode or during a time when traffic on the server is low but that is not mandatory.
710
+
711
+
1. **Disable Internal Token Cleanup**
712
+
Update the `deployment.toml` file located in `<IS_HOME>/repository/conf`:
713
+
714
+
```toml
715
+
[oauth.token_cleanup]
716
+
enable = false
717
+
```
718
+
719
+
2.**Run the Cleanup Script**
720
+
Select the appropriate SQL script based on your database type and execute it.
- Deletes eligible expired, revoked, or inactive entries
728
+
729
+
3.**Restart the Server**
730
+
Once cleanup is complete, restart **WSO2 Identity Server** with the updated and cleaned database. You can optionally **schedule periodic execution** of the cleanup procedure.
731
+
732
+
#### Configuring WSO2 Identity Server for token cleanup
733
+
734
+
You can also configure **WSO2 Identity Server** to trigger **refresh token cleanup** during the following events:
735
+
736
+
1.**On token refresh**: When issuing a new refresh token
737
+
2.**On token revocation**: When a refresh token is explicitly revoked
738
+
3.**User revoke events**: This includes events like **user deletion**, **user locked**, or **user role deletion**.
739
+
4.**Application revoke events**: This includes **application credentials revocation** or **application deletion**.
740
+
741
+
!!! warning "Manual Cleanup Required For Cleaning IDN_OAUTH2_REVOKED_TOKENS and IDN_SUBJECT_ENTITY_REVOKED_EVENT"
742
+
The tables `IDN_OAUTH2_REVOKED_TOKENS` and `IDN_SUBJECT_ENTITY_REVOKED_EVENT` do not have an automatic cleanup procedure by default. You must **manually implement** cleanup scripts for these tables to avoid database bloat and maintain performance.
743
+
744
+
Enable token cleanup by configuring the following properties in the `deployment.toml` file found in the `<IS_HOME>/repository/conf` folder.
745
+
746
+
```toml
747
+
[oauth.token_cleanup]
748
+
enable = true
749
+
```
750
+
751
+
<table>
752
+
<thead>
753
+
<tr class="header">
754
+
<th>Property</th>
755
+
<th>Description</th>
756
+
</tr>
757
+
</thead>
758
+
<tbody>
759
+
<tr class="odd">
760
+
<td><code>enable</code></td>
761
+
<td>
762
+
<p>Set this property to <code>true</code> to enable refresh token cleanup.</p>
763
+
<p>Set it to <code>false</code> to disable token cleanup.</p>
0 commit comments