Friday, 20 March 2026

Why doesn’t SQL Server implement result-set caching like Redis?

This question is asked by an interview. In Redis we can caching the data but why not we are implementing cache like Redis.

Because result-set caching fundamentally conflicts with SQL Server’s transactional guarantees, isolation semantics, and write-heavy OLTP design. The cost of correctness and invalidation would exceed the cost of re computation. SQL Server doesn’t implement result-set caching because it would break transactional isolation, require impossible cache invalidation, and violate ACID guarantees. Redis can cache results because it delegates correctness and staleness decisions to the application. SQL Server refuses to.

Below are the reasons

Ø  Result-set caching breaks transactional isolation: - SQL Server supports

o   READ COMMITTED

o   REPEATABLE READ

o   SNAPSHOT

o   SERIALIZABLE

o   Each transaction can see different versions of the same data.

Ø  Cache invalidation is impossible at OLTP scale

o   Any INSERT / UPDATE / DELETE:

§  Could affect thousands of cached result sets

§  Across different predicates

§  Across different joins

o   One row change could invalidate:

§  Aggregates

§  Groupings

§  Subqueries

§  Window functions

Ø  Ad-hoc queries make result caching unbounded

o   Each query:

§  Slightly different predicate

§  Slightly different result

§  Slightly different cache key

Ø  Plan cache ≠ Result cache:

o   SQL Server does cache:

§  Execution plans

§  Metadata

§  Data pages (buffer pool)

o   But it never caches:

§  Rows returned by SELECT

Ø  Locks, latches, and versioning make caching unsafe

Ø  Indexed Views prove Microsoft chose storage, not caching

o   Indexed (materialized) views

§  Physically stored

§  Logged

§  Transactionally maintained

§  Deterministic

o   This design:

§  Preserves ACID

§  Keeps results correct

§  Pushes cost to write time, not read time

 

The difference between them.

Aspect

Microsoft SQL Server

Redis

Primary role

Transactional RDBMS

Cache / KV store

Consistency

Strong ACID

Eventually consistent

Query type

Ad-hoc relational

Key-based

Data mutability

High write frequency

Mostly read-heavy

Cache invalidation

Complex

Explicit / TTL-based

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts