📘 SECTION 1: PYTHON FUNDAMENTALS


Q1. What are Python’s key features? Python is interpreted, dynamically typed, garbage-collected, and supports multiple paradigms (OOP, functional, procedural). It has a vast standard library, readable syntax, and strong community support. CPython is the reference implementation.


Q2. What is the difference between a list, tuple, set, and dictionary?

  • List: Ordered, mutable, allows duplicates. [1, 2, 3]
  • Tuple: Ordered, immutable, allows duplicates. (1, 2, 3)
  • Set: Unordered, mutable, no duplicates. {1, 2, 3}
  • Dictionary: Key-value pairs, ordered (Python 3.7+), keys must be unique. {"a": 1}

Q3. What is the difference between == and is? == checks value equality. is checks identity (same memory address). a = [1,2]; b = [1,2]; a == b is True, but a is b is False.


Q4. What are Python decorators? A decorator is a function that wraps another function to add behaviour without modifying it. Defined using @decorator_name. Commonly used for logging, authentication, caching.

def my_decorator(func):
    def wrapper(*args, **kwargs):
        print("Before")
        result = func(*args, **kwargs)
        print("After")
        return result
    return wrapper

Q5. What is *args and **kwargs? *args collects extra positional arguments as a tuple. **kwargs collects extra keyword arguments as a dictionary. Useful when you don’t know how many arguments will be passed.


Q6. What is a Python generator? A generator is a function that yields values one at a time using yield, pausing execution between each. It is memory-efficient for large datasets since it doesn’t load everything into memory at once.

def count_up(n):
    for i in range(n):
        yield i

Q7. What is a list comprehension? How does it differ from a generator expression? A list comprehension [x*2 for x in range(10)] evaluates immediately and stores all results in memory. A generator expression (x*2 for x in range(10)) is lazy — it produces values on demand. Use generators for large data.


Q8. Explain Python’s GIL (Global Interpreter Lock). The GIL is a mutex in CPython that allows only one thread to execute Python bytecode at a time. This limits true multi-threading for CPU-bound tasks. Use multiprocessing for CPU-bound work and threading or asyncio for I/O-bound work.


Q9. What is the difference between deepcopy and shallow copy?

  • Shallow copy (copy.copy() or [:]): Creates a new object but references the same nested objects.
  • Deep copy (copy.deepcopy()): Creates a new object and recursively copies all nested objects.

Q10. What are Python’s mutable vs immutable types?

  • Immutable: int, float, str, tuple, frozenset, bytes — cannot be changed after creation.
  • Mutable: list, dict, set, bytearray — can be modified in-place.

Q11. What is __init__, __str__, and __repr__?

  • __init__: Constructor, called when an object is instantiated.
  • __str__: Returns a readable string for end users (used by print()).
  • __repr__: Returns an unambiguous developer-facing string (used in REPL). If __str__ is missing, Python falls back to __repr__.

Q12. What is the difference between @staticmethod and @classmethod?

  • @staticmethod: No access to class (cls) or instance (self). Just a regular function namespaced inside a class.
  • @classmethod: Receives the class as the first argument (cls). Used as alternative constructors or to access/modify class state.

Q13. Explain Python’s MRO (Method Resolution Order). MRO defines the order in which base classes are searched when calling a method. Python uses the C3 linearization algorithm. You can inspect it with ClassName.__mro__. Relevant in multiple inheritance.


Q14. What is a lambda function? An anonymous, single-expression function. lambda x: x * 2. Used for short throwaway functions, often with map(), filter(), sorted().


Q15. What is the difference between @property and a regular method? @property allows a method to be accessed like an attribute (no parentheses). Useful for computed attributes or encapsulation.

class Circle:
    def __init__(self, r): self.r = r
    @property
    def area(self): return 3.14 * self.r ** 2

Q16. What are map(), filter(), and reduce()?

  • map(func, iterable): Applies func to each element.
  • filter(func, iterable): Returns elements where func is True.
  • reduce(func, iterable): Cumulatively applies func (from functools).

Q17. What is exception handling in Python? Use try/except/else/finally. else runs if no exception occurred. finally always runs (cleanup). Raise custom exceptions by subclassing Exception.

try:
    result = 10 / 0
except ZeroDivisionError as e:
    print(f"Error: {e}")
finally:
    print("Always runs")

Q18. What is the with statement and context managers? with ensures setup and teardown code runs automatically (e.g., file closing). Implemented via __enter__ and __exit__ methods, or contextlib.contextmanager.

with open("file.txt") as f:
    data = f.read()

Q19. What is asyncio in Python? asyncio is Python’s standard library for writing asynchronous, non-blocking code using async/await syntax. It uses a single-threaded event loop to handle concurrent I/O-bound tasks efficiently without threads.


Q20. What is the difference between threading, multiprocessing, and asyncio?

  • threading: Good for I/O-bound tasks; limited by GIL for CPU work.
  • multiprocessing: Bypasses GIL by using separate processes; good for CPU-bound work.
  • asyncio: Best for high-concurrency I/O (network, DB calls); single-threaded, non-blocking.

Q21. What are Python’s built-in data structures and when do you use each? Use list for ordered collections, tuple for immutable sequences or return values, dict for key lookups, set for membership testing and deduplication, deque from collections for efficient append/pop from both ends.


Q22. What is __slots__? __slots__ restricts instance attributes to a predefined list, saving memory by preventing the creation of __dict__ for each instance. Useful in classes with many instances.


Q23. What is pickling in Python? Pickling serializes a Python object to a byte stream (pickle.dumps()). Unpickling deserializes it back (pickle.loads()). Used for saving objects to disk or sending over a network. Never unpickle untrusted data.


🌐 SECTION 2: DJANGO


Q24. What is Django and what is its architecture? Django is a high-level Python web framework following the MVT (Model-View-Template) pattern. Model handles database, View contains business logic, Template handles HTML rendering. It follows “batteries-included” philosophy.


Q25. What is the Django ORM? Django’s ORM (Object-Relational Mapper) lets you interact with the database using Python classes instead of SQL. Each model class maps to a database table. It supports PostgreSQL, MySQL, SQLite, and Oracle.


Q26. What are Django models? A model is a Python class subclassing django.db.models.Model. Each class attribute is a database field. Django auto-generates the schema.

class Post(models.Model):
    title = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)

Q27. What is a migration in Django? Migrations are Django’s way of propagating model changes to the database schema. makemigrations creates migration files. migrate applies them. They track schema history and allow rollbacks.


Q28. What is the difference between select_related and prefetch_related?

  • select_related: Uses SQL JOIN for ForeignKey/OneToOne relationships. Fetches related data in a single query.
  • prefetch_related: Uses separate queries for ManyToMany/reverse ForeignKey. More efficient than repeated individual queries.

Use them to avoid the N+1 query problem.


Q29. What is the N+1 query problem? When fetching a list of objects and then accessing a related object for each one, Django fires a new query per object (N+1 total). Fix it with select_related or prefetch_related.

# Bad (N+1)
for post in Post.objects.all():
    print(post.author.name)
 
# Good
for post in Post.objects.select_related('author').all():
    print(post.author.name)

Q30. What is Django’s QuerySet and is it lazy? A QuerySet represents a collection of database queries. It is lazy — the database is not hit until the QuerySet is evaluated (iterated, sliced, or converted). This allows chaining filters efficiently.


Q31. What is Django middleware? Middleware is a framework of hooks into Django’s request/response processing. It’s a layer between the web server and view. Common uses: authentication, CORS headers, session handling, logging.


Q32. What is the difference between get() and filter() in Django ORM?

  • get(): Returns exactly one object; raises DoesNotExist or MultipleObjectsReturned otherwise.
  • filter(): Returns a QuerySet (possibly empty), never raises an exception.

Q33. What is Django’s signals framework? Signals allow decoupled applications to get notified when certain actions occur. Common signals: pre_save, post_save, pre_delete. Use @receiver decorator to listen.

from django.db.models.signals import post_save
from django.dispatch import receiver
 
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

Q34. What is settings.py and what are key settings? settings.py is the central configuration file. Key settings: DEBUG, ALLOWED_HOSTS, DATABASES, INSTALLED_APPS, MIDDLEWARE, STATIC_URL, MEDIA_ROOT, SECRET_KEY.


Q35. What are Django views? Difference between FBVs and CBVs?

  • FBV (Function-Based View): Simple functions that take a request and return a response. More explicit.
  • CBV (Class-Based View): Class-based with built-in methods (get, post). Better for code reuse via inheritance. ListView, DetailView, CreateView are common generic CBVs.

Q36. What is Django REST Framework (DRF)? DRF is a toolkit for building Web APIs on top of Django. Key components: Serializers (data validation/conversion), ViewSets (combine CRUD logic), Routers (auto URL generation), Permissions, Authentication, Pagination.


Q37. What is a DRF Serializer? A serializer converts complex data (model instances, querysets) to Python-native types (JSON/XML) and vice versa. It also validates incoming data.

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ['id', 'title', 'content']

Q38. What is the difference between ModelSerializer and Serializer in DRF? ModelSerializer auto-generates fields from a model and provides default create() and update() methods. Serializer requires all fields to be explicitly defined — more control, more verbose.


Q39. What are DRF permissions? Permissions control who can access API endpoints. Built-in: IsAuthenticated, IsAdminUser, AllowAny, IsAuthenticatedOrReadOnly. Custom permissions subclass BasePermission and implement has_permission() or has_object_permission().


Q40. What is JWT and how does Django use it for authentication? JWT (JSON Web Token) is a compact, self-contained token for authentication. Django uses libraries like djangorestframework-simplejwt. On login, a signed access token (short-lived) and refresh token (long-lived) are issued. The client sends the access token in the Authorization: Bearer <token> header.


Q41. What is the Django admin panel? Django Admin is a built-in, auto-generated web interface for managing database records. Register models with admin.site.register(Model). Can be customized with ModelAdmin subclasses to add search, filters, custom actions.


Q42. What is CSRF and how does Django protect against it? Cross-Site Request Forgery tricks a user’s browser into making unwanted requests. Django protects against it using the CSRF middleware and csrf_token template tag, which embeds a secret token in forms that must match the one stored in the user’s session.


Q43. What is manage.py used for? manage.py is a command-line utility for managing a Django project. Common commands: runserver, makemigrations, migrate, createsuperuser, shell, test, collectstatic.


Q44. How does Django handle static and media files? Static files (CSS, JS) are served via STATIC_URL/STATICFILES_DIRS. Run collectstatic for production. Media files (user uploads) are stored at MEDIA_ROOT and served via MEDIA_URL. In production, use a CDN or object storage (e.g., S3).


Q45. What is F() expression and Q() object in Django?

  • F(): Refers to a model field value at the database level without fetching it to Python. Used for atomic updates: Post.objects.update(views=F('views') + 1).
  • Q(): Enables complex queries with AND, OR, NOT logic: Post.objects.filter(Q(title="a") | Q(title="b")).

Q46. What is the contenttypes framework in Django? The contenttypes framework provides a generic way to create relationships between any models using ContentType and GenericForeignKey. Used internally by Django’s permission system and admin.


🖥️ SECTION 3: BACKEND FUNDAMENTALS


Q47. What is the difference between SQL and NoSQL databases?

  • SQL (PostgreSQL, MySQL): Relational, structured schema, ACID transactions, good for complex queries.
  • NoSQL (MongoDB, Redis, Cassandra): Schema-flexible, horizontally scalable, optimized for specific access patterns (document, key-value, graph, column-family).

Q48. What are database indexes and why are they important? An index is a data structure (typically B-tree) that speeds up read queries at the cost of slower writes and extra storage. Index columns used in WHERE, JOIN, ORDER BY clauses. Without indexes, the database performs a full table scan.


Q49. What is database normalization? Normalization organizes data to reduce redundancy. Key normal forms:

  • 1NF: Atomic values, no repeating groups.
  • 2NF: 1NF + no partial dependencies.
  • 3NF: 2NF + no transitive dependencies. Denormalization is sometimes done for read performance.

Q50. What are ACID properties?

  • Atomicity: A transaction fully succeeds or fully fails.
  • Consistency: Transactions bring the DB from one valid state to another.
  • Isolation: Concurrent transactions don’t interfere.
  • Durability: Committed transactions persist even after crashes.

Q51. What is the difference between GET, POST, PUT, PATCH, and DELETE?

  • GET: Retrieve resource (idempotent, no body).
  • POST: Create a new resource.
  • PUT: Fully replace a resource.
  • PATCH: Partially update a resource.
  • DELETE: Remove a resource.

Q52. What is idempotency? An operation is idempotent if calling it multiple times produces the same result. GET, PUT, DELETE, PATCH are idempotent. POST is not — calling it N times creates N resources.


Q53. What is HTTP status codes — common ones?

  • 200 OK, 201 Created, 204 No Content
  • 301 Moved Permanently, 302 Found
  • 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 405 Method Not Allowed, 409 Conflict, 422 Unprocessable Entity
  • 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable

Q54. What is the difference between authentication and authorization?

  • Authentication: Verifying who you are (login, tokens).
  • Authorization: Verifying what you’re allowed to do (permissions, roles).

Q55. What is session-based vs token-based authentication?

  • Session-based: Server stores session data; client holds a session cookie. Stateful.
  • Token-based (JWT): Server issues a signed token; client stores it. Stateless — server doesn’t need to store sessions. Better for APIs and microservices.

Q56. What is CORS and why does it matter? CORS (Cross-Origin Resource Sharing) is a browser security policy that blocks web pages from making requests to a different domain. Servers must send Access-Control-Allow-Origin headers to permit cross-origin requests. In Django, use django-cors-headers.


Q57. What is a foreign key constraint? A foreign key links a column in one table to the primary key of another, enforcing referential integrity. It ensures you can’t have a reference to a non-existent record (unless set to NULL or SET NULL on delete).


Q58. What is ORM and what are its advantages/disadvantages? ORM (Object-Relational Mapper) maps database tables to classes. Pros: Cleaner code, database abstraction, security against SQL injection. Cons: Can generate inefficient queries; less control than raw SQL for complex operations.


Q59. What is a transaction in a database? A transaction is a unit of work that groups multiple DB operations. Either all succeed (COMMIT) or all fail (ROLLBACK). In Django, use transaction.atomic().

from django.db import transaction
 
with transaction.atomic():
    user.save()
    profile.save()

Q60. What is connection pooling? Connection pooling maintains a pool of database connections that can be reused across requests instead of opening/closing a new connection each time. Reduces latency and DB load. Tools: PgBouncer (PostgreSQL), Django’s CONN_MAX_AGE.


Q61. What is the difference between horizontal and vertical scaling?

  • Vertical scaling: Add more CPU/RAM to the existing server (scale up). Has limits.
  • Horizontal scaling: Add more servers and distribute load (scale out). More flexible and fault-tolerant.

Q62. What is a message queue and why is it used? A message queue (e.g., Celery + RabbitMQ/Redis) decouples services by allowing one service to publish tasks and another to consume them asynchronously. Used for sending emails, processing images, background jobs — avoiding blocking the request-response cycle.


Q63. What is Celery in Django? Celery is a distributed task queue for Python. It allows you to run tasks asynchronously (outside the HTTP request cycle) using a broker (RabbitMQ or Redis). Common use: sending emails, generating reports, scheduled tasks (celery beat).


Q64. What is an environment variable and why should you not hardcode secrets? Environment variables store config values (API keys, DB credentials) outside source code. Hardcoding secrets exposes them in version control. Use .env files with python-decouple or django-environ and add .env to .gitignore.


Q65. What is the difference between synchronous and asynchronous programming?

  • Synchronous: Each task blocks until complete before the next one starts.
  • Asynchronous: Tasks can be initiated and control is returned while waiting (e.g., for I/O). Python supports this with asyncio, async/await.

🔌 SECTION 4: REST API, WebSockets/WebRTC, Docker, Redis & Caching


🔗 REST API


Q66. What is REST and what are its principles? REST (Representational State Transfer) is an architectural style for APIs. Principles:

  1. Stateless — server stores no client state between requests.
  2. Client-Server separation — UI and data storage are decoupled.
  3. Uniform Interface — consistent resource-based URLs.
  4. Cacheable — responses should declare cacheability.
  5. Layered System — client can’t tell if connected directly to the server.
  6. Code on Demand (optional) — server can send executable code.

Q67. What is the difference between REST and GraphQL?

  • REST: Multiple endpoints, fixed response shapes, potential over/under-fetching.
  • GraphQL: Single endpoint, client specifies exact data needed, solves over-fetching. Better for complex, evolving APIs but adds complexity.

Q68. What is API versioning and why is it needed? API versioning prevents breaking existing clients when you make backward-incompatible changes. Strategies: URL versioning (/api/v1/), header versioning (Accept: application/vnd.myapi.v1+json), query param (?version=1).


Q69. What is pagination in APIs and what strategies exist? Pagination limits the number of records returned per response. Strategies:

  • Offset/limit: ?offset=20&limit=10 — simple but slow on large offsets.
  • Cursor-based: Uses an opaque cursor pointing to a record — more efficient for large datasets.
  • Page-number: ?page=3&page_size=10 — easy to understand.

Q70. What is rate limiting and why is it important? Rate limiting restricts how many requests a client can make in a given time window. Prevents abuse, DDoS, and protects backend resources. Implemented using Redis-based counters or API gateway rules.


Q71. What is the difference between 200 OK and 201 Created? 200 OK is a general success response (GET, PUT, PATCH, DELETE). 201 Created indicates a new resource was successfully created (typically in response to POST) and usually includes a Location header pointing to the new resource.


Q72. What are HTTP headers? Name important ones. HTTP headers carry metadata in requests/responses.

  • Request: Authorization, Content-Type, Accept, Cookie, X-Request-ID
  • Response: Content-Type, Set-Cookie, Cache-Control, Access-Control-Allow-Origin, ETag, Location

Q73. What is OpenAPI / Swagger? OpenAPI is a standard specification for describing REST APIs. Swagger UI renders it as interactive documentation. DRF can auto-generate OpenAPI schemas using drf-spectacular or drf-yasg.


Q74. What is HATEOAS? HATEOAS (Hypermedia As The Engine Of Application State) is a REST constraint where API responses include links to related actions/resources, making the API self-discoverable.


🔁 WebSockets & WebRTC


Q75. What is the difference between HTTP and WebSockets? HTTP is request-response — the client initiates every communication. WebSockets provide a persistent, full-duplex connection where both client and server can push messages at any time after the initial handshake (via HTTP Upgrade header).


Q76. When would you use WebSockets over polling? Use WebSockets for real-time, low-latency, frequent bidirectional communication: chat apps, live notifications, collaborative tools, live dashboards, multiplayer games. Polling wastes bandwidth by repeatedly checking for updates even when there are none.


Q77. What is Django Channels? Django Channels extends Django to handle WebSockets, long-polling, and other async protocols alongside standard HTTP. It uses ASGI instead of WSGI and requires a channel layer (typically Redis) for communicating between consumers.


Q78. What is ASGI vs WSGI?

  • WSGI (Web Server Gateway Interface): Synchronous, handles one request at a time. Standard for traditional Django apps.
  • ASGI (Asynchronous Server Gateway Interface): Supports async, handles WebSockets and HTTP/2. Required for Django Channels.

Q79. What is WebRTC? WebRTC (Web Real-Time Communication) is a browser API that enables peer-to-peer audio, video, and data communication directly between browsers without a central media server. Used in video calls, screen sharing. It uses ICE/STUN/TURN for NAT traversal.


Q80. What is the role of a STUN/TURN server in WebRTC?

  • STUN: Helps a client discover its public IP and port behind a NAT.
  • TURN: Acts as a relay server when peer-to-peer fails (restrictive NATs/firewalls). More expensive but needed as a fallback.

Q81. What is the WebRTC signaling process? WebRTC requires an out-of-band signaling server (typically via WebSockets) to exchange SDP (Session Description Protocol) offers/answers and ICE candidates between peers. Once connected, media flows peer-to-peer.


Q82. What is the difference between WebSockets and Server-Sent Events (SSE)?

  • WebSockets: Bidirectional, both client and server can send.
  • SSE: Unidirectional — server pushes events to client only. Simpler, uses plain HTTP, auto-reconnects. Good for live feeds, dashboards.

🐳 Docker


Q83. What is Docker and why is it used? Docker is a platform for packaging applications and their dependencies into containers — lightweight, portable, isolated units. Ensures “it works on my machine” = “it works everywhere.” Replaces inconsistent environment setups.


Q84. What is the difference between an image and a container?

  • Image: Read-only blueprint/template (like a class).
  • Container: A running instance of an image (like an object). You can run many containers from one image.

Q85. What is a Dockerfile? A Dockerfile is a script of instructions to build a Docker image.

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "myapp.wsgi:application"]

Q86. What is Docker Compose? Docker Compose is a tool for defining and running multi-container applications using a docker-compose.yml file. You define services (web, db, redis), their images, ports, volumes, and env vars. Run all with docker compose up.


Q87. What are Docker volumes? Volumes are Docker-managed persistent storage. Containers are ephemeral — data is lost when a container is removed. Volumes persist data (e.g., database files) between container restarts. Use volumes: in Compose or -v flag in docker run.


Q88. What is the difference between CMD and ENTRYPOINT in a Dockerfile?

  • CMD: Default command that can be overridden at runtime.
  • ENTRYPOINT: Sets the container’s main process; harder to override. Use ENTRYPOINT for the executable and CMD for default arguments.

Q89. What is a Docker network? Docker networks allow containers to communicate. By default, Compose creates a bridge network so services can reference each other by service name (e.g., db, redis). Options: bridge, host, none, overlay (for Swarm).


Q90. What is the difference between COPY and ADD in a Dockerfile? COPY simply copies files from host to container. ADD does the same but also supports URLs and auto-extracts .tar archives. Prefer COPY for clarity; use ADD only when you need its extra features.


Q91. How do you minimize Docker image size? Use slim/alpine base images, combine RUN commands to reduce layers, use .dockerignore to exclude unnecessary files, use multi-stage builds to separate build dependencies from the final runtime image.


Q92. What is a multi-stage build in Docker? A multi-stage build uses multiple FROM statements to separate build stages. The final image only includes what’s needed for runtime, keeping it small.

FROM python:3.11 AS builder
RUN pip install ...
 
FROM python:3.11-slim
COPY --from=builder /usr/local/lib ...

⚡ Redis & Caching


Q93. What is Redis? Redis (Remote Dictionary Server) is an open-source, in-memory key-value data store. It supports rich data structures: strings, hashes, lists, sets, sorted sets, streams. Used for caching, session storage, pub/sub, rate limiting, and as a Celery broker.


Q94. What is caching and why is it important? Caching stores the result of expensive operations (DB queries, API calls, computations) in a fast-access store (memory) so future requests are served instantly without repeating the work. Dramatically reduces latency and database load.


Q95. What are the main caching strategies?

  • Cache-Aside (Lazy loading): App checks cache first; on miss, loads from DB and stores in cache. Most common.
  • Write-Through: Write to cache and DB simultaneously. Cache is always up to date.
  • Write-Behind (Write-Back): Write to cache immediately, flush to DB asynchronously later. Fast writes but risk of data loss.
  • Read-Through: Cache sits in front of DB; cache handles DB read automatically on miss.

Q96. What is cache invalidation and why is it hard? Cache invalidation is removing or updating stale cached data. It’s hard because you must decide exactly when data is stale. Strategies: TTL (Time-To-Live) expiry, event-driven invalidation (delete cache on model update), versioned cache keys.


Q97. What is a cache TTL (Time-To-Live)? TTL is the duration a cached value remains valid before it expires and is removed. Short TTL = fresher data but more cache misses. Long TTL = more cache hits but potentially stale data.


Q98. What are cache hit and cache miss?

  • Cache hit: Requested data is found in the cache — fast response.
  • Cache miss: Data is not in cache; must fetch from the original source, then store in cache. Expensive.

Q99. How does Django integrate with Redis for caching? Set CACHES in settings.py using django-redis:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
    }
}

Use cache.set(key, value, timeout) and cache.get(key). Also supports per-view and template fragment caching.


Q100. What are the different levels of caching in Django?

  • Per-site cache: Caches entire pages (not granular).
  • Per-view cache: @cache_page(60 * 15) decorator.
  • Template fragment cache: {% cache 500 sidebar %} in templates.
  • Low-level cache API: cache.set() / cache.get() for custom logic.

Q101. What is a cache stampede (thundering herd)? When a cache key expires and many simultaneous requests try to regenerate it at once, all hitting the DB simultaneously. Prevention: probabilistic early expiration, locking mechanisms (mutex/semaphore on regeneration), or background refresh.


Q102. What are Redis data structures and their use cases?

  • String: Caching, counters, sessions.
  • Hash: Storing objects (HSET user:1 name "Alice").
  • List: Message queues, activity feeds (FIFO/LIFO).
  • Set: Unique members, tagging, social graphs.
  • Sorted Set (ZSet): Leaderboards, rate limiting (with scores as timestamps).
  • Stream: Event log, Kafka-like messaging.

Q103. What is Redis pub/sub? Redis pub/sub (publish/subscribe) is a messaging pattern where publishers send messages to channels and subscribers receive them. Real-time, but not persistent — if a subscriber is offline, it misses messages. For persistence, use Redis Streams.


Q104. What is the difference between Redis and Memcached?

  • Redis: Richer data structures, persistence (RDB/AOF snapshots), pub/sub, Lua scripting, clustering, atomic operations.
  • Memcached: Simpler key-value only, multi-threaded (better single-node throughput for simple caching), no persistence. Use Redis for most modern use cases.

Q105. What is Redis persistence?

  • RDB (Redis Database Snapshots): Periodic point-in-time snapshots. Fast restore, some data loss on crash.
  • AOF (Append-Only File): Logs every write operation. More durable, larger files, slower. Can use both together.

Q106. What is a distributed cache vs local cache?

  • Local cache: In-process memory (e.g., Python dict, lru_cache). Fastest but not shared across multiple server instances.
  • Distributed cache (Redis, Memcached): Shared across all server instances. Essential for horizontally scaled applications.

Q107. How would you implement rate limiting using Redis? Use a sorted set or a simple counter with TTL:

import redis
r = redis.Redis()
 
def is_rate_limited(user_id, limit=100, window=60):
    key = f"rate:{user_id}"
    count = r.incr(key)
    if count == 1:
        r.expire(key, window)
    return count > limit

Good luck tomorrow — you’ve got this! 🚀