Performance: Stateless authentication mental model
This concept affects server response time and client load by avoiding server-side session storage, improving scalability and reducing server memory usage.
Jump into concepts and practice - no test required
Use stateless JWT tokens for authentication, where client sends token with each request and server validates without storing session.
Use server-side sessions to store user authentication state, e.g., HttpSession in Spring Boot with session ID stored in cookie.
| Pattern | Server Memory Usage | Response Time Impact | Scalability | Verdict |
|---|---|---|---|---|
| Stateful session storage | High (stores session per user) | Slower due to session lookup | Limited by server memory | [X] Bad |
| Stateless JWT token | Low (no server session storage) | Faster due to token validation only | Highly scalable | [OK] Good |
@GetMapping("/profile")
public ResponseEntity<String> getProfile(@RequestHeader("Authorization") String authHeader) {
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
return ResponseEntity.status(401).body("Unauthorized");
}
String token = authHeader.substring(7);
if (token.equals("valid-token")) {
return ResponseEntity.ok("User Profile Data");
} else {
return ResponseEntity.status(403).body("Forbidden");
}
}Authorization: Bearer valid-token?public boolean isValidToken(String token) {
if (token == null || token.isEmpty()) {
return false;
}
// Token validation logic
return token.equals("valid-token");
}
public void doFilter(HttpServletRequest req, HttpServletResponse res) {
String auth = req.getHeader("Authorization");
if (auth != null && auth.startsWith("Bearer ")) {
String token = auth.substring(7);
if (!isValidToken(token)) {
res.setStatus(401);
}
}
// Continue filter chain
}