Skip to content

feat: Token caching for external identity providers#935

Merged
levkk merged 6 commits into
pgdogdev:mainfrom
MagicAbdel:main
May 16, 2026
Merged

feat: Token caching for external identity providers#935
levkk merged 6 commits into
pgdogdev:mainfrom
MagicAbdel:main

Conversation

@MagicAbdel
Copy link
Copy Markdown
Contributor

Summary

Introduces an in-memory token cache shared by azure_workload_identity and rds_iam authentication backends. Tokens are now fetched once and reused until expiry, instead of being fetched on every connection.

Motivation

Token fetching from external identity providers can be slow — Azure Workload Identity in particular was measured at ~30s per token fetch. This was directly impacting pool startup time, as each connection attempt would block waiting for a fresh token.

Changes

  • Added a shared token_cache module with get/set helpers keyed by host, port, and user
  • Refactored azure_workload_identity to extract fetch_token() returning (String, SystemTime), using the expires_on field from the Azure SDK response as the cache TTL
  • Refactored rds_iam to follow the same pattern, with a fixed 15-minute TTL (RDS IAM tokens are valid for 15 minutes but the AWS SDK does not return an expiry)
  • Added cache hit/miss tests for both backends

Impact

  • Pool startup is significantly faster when multiple connections share the same identity — only the first connection pays the token fetch cost
  • No behavioral change for single-connection or uncached scenarios
  • TTL-based expiry ensures tokens are refreshed before they become invalid

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 26, 2026

@levkk
Copy link
Copy Markdown
Collaborator

levkk commented Apr 26, 2026

Nice! Quick question: do you think it would be possible to run the token acquisition as a background task instead? That way, the token is always fresh when accessed for creating server connections.

Comment thread pgdog/src/backend/auth/azure_workload_identity.rs Outdated
@MagicAbdel MagicAbdel force-pushed the main branch 2 times, most recently from d5c05c8 to acb481a Compare May 3, 2026 15:00
@MagicAbdel
Copy link
Copy Markdown
Contributor Author

Sorry for the delay, I finally had some time to circle back to your comments.

That was a great suggestion. I’ve added EXPIRY_BUFFER to token_cache.rs to trigger the refresh task 45 seconds before expiry. I extracted that logic into get_or_fetch and applied the same pattern to rds_iam to account for their 15-minute validity.

Let me know if this looks good!

Comment thread pgdog/src/backend/auth/token_cache.rs Outdated
Comment thread pgdog/src/backend/auth/token_cache.rs Outdated
@MagicAbdel
Copy link
Copy Markdown
Contributor Author

I tried my best to address your review comments. I learned a lot going through the codebase. Happy to make changes based on feedback.

Copy link
Copy Markdown
Collaborator

@levkk levkk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you!

@levkk levkk merged commit d87123f into pgdogdev:main May 16, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants