Engineering Notes
Backend Engineering4 min readUpdated

Redis, API Speed, and Tenant Safety in a Multi-Tenant Platform

Running a Redis container does not make an API faster. How I decide what to cache, how to scope keys per tenant, and how I would measure the result.

Multi-Tenant Education PlatformRedisDjangoCachingPerformanceSecurityMulti-Tenancy
On this page

Redis is part of our backend infrastructure. That sentence is true, and it says very little. A running Redis container does not make an API faster on its own. What matters is how the Django application uses it, and in a multi-tenant platform that decision has security consequences, not just performance ones.

When an application is small, a database query that takes a little extra time may not be noticeable. On a platform like this one, the same categories, course information, resource lists, and dashboard data can be requested again and again by many students and institutes. That is where caching starts to make sense.

Start with what repeats, not with what is slow

My first step is not to put every response into a cache. It is to answer three questions:

  • Which requests repeat?
  • Which data changes often?
  • Which data must never be shared across users or tenants?

A normal read request already does a lot of work: authentication, tenant resolution, permission checks, a database query, serialization, and the JSON response. A well-chosen cache can skip the repeated read work. It must never skip the security checks.

  1. Browser, or Mobile app
  2. Django API
  3. Tenant + permission validation
  4. Check Redis for approved cacheable data
  5. Cache hit → return cached data, or Cache miss → query PostgreSQL → store short-lived result → return data
Cache hit and miss path for an approved read endpoint

The validation step sits before the cache lookup on purpose. Whether the data comes from Redis or PostgreSQL, the request has already been checked.

Good first candidates

I would start with data that is read frequently and changes less often:

  • published public category information
  • public institute website content
  • selected reference lists

Things I would not cache without a stricter design

  • exam submissions
  • individual results
  • payment state
  • authorization decisions

Getting any of these wrong means showing a student the wrong result or the wrong access level. The speed gain is not worth that risk without a design built specifically for it.

A cache key is a security boundary

In a single-tenant app, a key like categories looks harmless. In a multi-tenant system it is a bug waiting to happen. Institute A and Institute B call the same API, and they must not receive each other's cached data.

So I make the scope explicit in the key:

# Illustrative pattern, not a copy of the production code.
from django.core.cache import cache

key = f"tenant:{tenant.id}:public_categories:v1"
data = cache.get(key)

if data is None:
    data = build_published_category_list(tenant=tenant)
    cache.set(key, data, timeout=300)

This pattern is for public, tenant-specific reference data. For user-specific data, the key also needs the user's context, and the API has to check authorization before deciding that a cached value is safe to return.

I also avoid treating arbitrary incoming request headers as the final source of truth for permissions. The tenant can be identified from an approved host or a trusted app context, but Django still enforces what the authenticated user is allowed to access.

Invalidation is the real work

Caching a category list takes a few lines. Keeping it correct after an institute admin edits a category is the actual challenge. There are three practical approaches:

ApproachWhen I would use it
Short TTLLess critical public content where a few minutes of staleness is acceptable
Explicit invalidation on updateData that admins edit often and expect to see change immediately
Versioned keys (:v1, :v2)Changing the shape of cached data without waiting for old entries to expire

For frequently edited data, I prefer a clear invalidation rule over a long TTL.

The cache also has to be optional for correctness. If Redis is temporarily unavailable, the application should still read authoritative data from PostgreSQL wherever the architecture allows. Redis should never quietly become the only copy of important records.

Measure before calling it an optimization

I do not have benchmark results for this, so I am not going to claim a percentage improvement. Before I call a change an optimization, I would compare the same endpoint under similar conditions and look at:

  • database query count per request
  • response time at the 95th percentile, not just the average
  • Redis hit rate
  • behavior after a cache miss, and after a Redis restart

The goal is specific: reduce repeated database work while keeping tenant and user boundaries correct. A fast response is not a good response if it belongs to the wrong institute. That is why I treat caching as part of API design: define what is cacheable, put the right scope in the key, invalidate it when the data changes, and measure the result.

Related reading