Use UUIDv7 for JTI claim on Python 3.13+#979
Open
jesustorres-code wants to merge 2 commits into
Open
Conversation
UUIDv7 is time-ordered, which makes it more efficient as a b-tree index key in databases used for token blacklisting. Falls back to uuid4 on Python < 3.13 where uuid7 is not available in the standard library. Closes jazzband#943
for more information, see https://pre-commit.ci
Andrew-Chen-Wang
requested changes
May 21, 2026
Comment on lines
+4
to
+7
| try: | ||
| from uuid import uuid7 as _uuid_for_jti # Python 3.13+ | ||
| except ImportError: | ||
| from uuid import uuid4 as _uuid_for_jti # type: ignore[assignment] |
Member
There was a problem hiding this comment.
Suggested change
| try: | |
| from uuid import uuid7 as _uuid_for_jti # Python 3.13+ | |
| except ImportError: | |
| from uuid import uuid4 as _uuid_for_jti # type: ignore[assignment] | |
| try: | |
| from uuid import uuid7 as _uuid_for_jti # Python 3.13+ | |
| except ImportError: | |
| from uuid import uuid4 as _uuid_for_jti # type: ignore[assignment] | |
| uuid = _uuid_for_jti |
so that users can override it
|
Hey @jesustorres-code, great work on this — UUIDv7 for JTI is a solid improvement especially for blacklisting at scale. Are you planning to address @Andrew-Chen-Wang's suggestion of exposing I'm actively looking to contribute to open source projects and this would be a great place to start! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #943
What
Replace
uuid4withuuid7for JTI generation on Python 3.13+, falling back touuid4on older versions.Why
UUIDv7 is time-ordered (monotonically increasing), which makes it significantly more efficient as a b-tree index key in the
OutstandingTokentable used for blacklisting. UUIDv4's random distribution causes index fragmentation and write amplification at scale; UUIDv7 inserts append near the end of the index, reducing page splits.Python 3.13 added
uuid.uuid7()to the standard library (PEP 769). Older supported versions (3.10–3.12) fall back touuid4, preserving existing behavior.Changes
rest_framework_simplejwt/tokens.py: conditional import;set_jti()uses_uuid_for_jti().hextests/test_tokens.py: added format assertion (32-char hex string) totest_set_jtiNo migration needed — the JTI column type and length are unchanged (hex string, 32 chars).