API / Microservices Design Patterns Interview Questions
What is the Access Token pattern (JWT/OAuth2) for service-to-client authentication?
The Access Token pattern uses short-lived cryptographically signed tokens — most commonly JWTs issued via OAuth 2.0 / OpenID Connect — to authenticate client requests to microservices. The client authenticates once with an Authorization Server (Keycloak, Okta, Cognito) and receives a JWT. Subsequent requests carry this token; any service that holds the corresponding public key can validate it locally without calling the Auth Server on every request.
JWT structure — three Base64URL-encoded segments separated by dots (header.payload.signature):
// Header (algorithm and token type)
{ "alg": "RS256", "typ": "JWT" }
// Payload (claims)
{
"sub": "user-42",
"iss": "https://auth.example.com",
"aud": "order-service",
"exp": 1714003200, // expiry Unix timestamp
"scope": "orders:read orders:write"
}
// Signature: RS256(base64(header) + "." + base64(payload), privateKey)
// HTTP request
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...
Validation at the receiving service (API Gateway or service itself):
- Decode the header to get the signing algorithm and key ID (
kid). - Fetch (or cache) the public key from the Auth Server's JWKS endpoint.
- Verify the signature using the public key.
- Check
exp(not expired),iss(trusted issuer), andaud(this service is the intended audience). - Extract
subandscopeclaims to enforce authorisation.
Short token lifetimes (5–15 minutes) limit the blast radius if a token is stolen. Refresh tokens (longer-lived, stored securely by the client) are exchanged for new access tokens when the old one expires, without requiring re-authentication.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
