Caching vs Database Indexing

Both are commonly confused. Here is a side-by-side breakdown of what each one does, when to reach for it, and when it would be the wrong choice.

Caching

Caching = storing results so you don't compute them twice.

Read full block →

Database Indexing

Database indexing = adding a lookup shortcut so the database jumps straight to matching rows instead of scanning every one.

Read full block →

When to use each

Use Caching when

  • Same data requested repeatedly

    Product pages, user profiles, search results, API responses. Anything multiple users (or the same user) request often.

  • Data doesn't change frequently

    If your product catalog updates once a day, there's no reason to query the database on every page load

Use Database Indexing when

  • Pages get slower as your data grows

    If a page loaded fine last month but crawls now, a full table scan on a growing table is the most likely cause

  • You filter, sort, or search on a specific column

    Any column that shows up in a WHERE clause, ORDER BY, or search query is a candidate for an index. If users filter orders by status, index the status column

  • A single query takes over 200ms

    For most web apps, a database query should take under 50ms. If Django Debug Toolbar shows a query taking hundreds of milliseconds, an index is the first thing to try

When to avoid each

Avoid Caching when

  • Data must always be real-time

    Live stock prices, real-time chat messages, collaborative editing. Stale data here means broken features.

  • Every request is unique

    If every query has different parameters and no patterns repeat, caching just wastes memory with zero hits

Avoid Database Indexing when

  • Your table has very few rows

    With under 1,000 rows, the database can scan the whole table in microseconds. Adding an index won't make a noticeable difference and just adds overhead on every insert

  • The column has very few unique values

    Indexing a column that only holds True or False doesn't help much. The database still has to read half the table. Indexes shine on columns with many unique values, like user IDs or email addresses

  • You write far more than you read

    Every index slows down inserts and updates slightly, because the database has to update the index too. For write-heavy tables (like a high-volume log), adding many indexes can hurt more than it helps