Performance: DRF authentication (Token, JWT)
This affects the server response time and client perceived latency during API authentication.
Jump into concepts and practice - no test required
Using JWT authentication with stateless token validation: from rest_framework_simplejwt.authentication import JWTAuthentication from rest_framework.views import APIView class MyView(APIView): authentication_classes = [JWTAuthentication] # Token is validated cryptographically without DB lookup
Using Django REST Framework TokenAuthentication with database lookup on every request: from rest_framework.authentication import TokenAuthentication from rest_framework.views import APIView class MyView(APIView): authentication_classes = [TokenAuthentication] # Each request triggers a DB query to validate the token
| Pattern | DB Queries | Token Validation Cost | Latency Impact | Verdict |
|---|---|---|---|---|
| DRF TokenAuthentication | 1 query/request | DB lookup | Higher latency per request | [X] Bad |
| JWT Authentication | 0 queries/request | Cryptographic check | Lower latency, scalable | [OK] Good |
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.views import APIView
from rest_framework.response import Response
class MyView(APIView):
authentication_classes = [JWTAuthentication()]
def get(self, request):
return Response({"user": str(request.user)})from rest_framework.authentication import TokenAuthentication
class MyView(APIView):
authentication_classes = [TokenAuthentication]
def get(self, request):
return Response({"message": "Hello"})