Tool for HR, Hiring Managers, and the Leadership Team

Server-Side Session vs Cache | Dot Net Interview Questions

Server-Side Session vs Cache is a very common interview topic — especially for .NET / Web API / System Design roles.

Server-Side Session vs Cache (Interview Guide)

1. What is Server-Side Session?

Server-side session stores user-specific data on the server.

Each user gets a unique Session ID which is stored in:

  • Cookie (usually)

  • URL (rare)

Server uses that Session ID to retrieve user data.

Example

User logs into a website:

HttpContext.Session.SetString("UserName", "Sampath");

Later:

var name = HttpContext.Session.GetString("UserName");

What happens internally

Browser → sends Session ID
Server → finds session data
Server → returns user-specific data

When to Use Sessions

Use Session when storing:

✅ Logged-in user info
✅ User preferences
✅ Shopping cart items
✅ Temporary user state

Session Example (Real-world)

E-commerce website:

User adds items to cart:

Session["Cart"] = [Item1, Item2]

Each user has different cart

2. What is Cache?

Cache stores application-wide shared data to improve performance.

Cache is NOT user-specific

Example

_memoryCache.Set("Products", productsList);

Later:

var products = _memoryCache.Get("Products");

When to Use Cache

Use Cache when storing:

✅ Frequently accessed data
✅ Database query results
✅ Configuration data
✅ Static data (countries, cities)

Cache Example (Real-world)

Fetching countries from database:

Instead of hitting DB every time:

var countries = _memoryCache.Get("Countries");

if (countries == null)
{
    countries = _repository.GetCountries();
    _memoryCache.Set("Countries", countries);
}

Key Differences (Important for Interviews)

Feature Session Cache
Data Type User-specific Application-wide
Performance Slower Faster
Scope Per User Shared
Lifetime User session Configurable
Example Shopping cart Countries list
Memory Usage Higher Optimized
Expiration Session timeout Absolute/Sliding

Example to Understand Clearly

Session Example

User A:

Session["User"] = "Sampath"

User B:

Session["User"] = "Ravi"

Each user has different values

Cache Example

Cache:

Cache["Countries"] = India, USA, UK

All users share same data

Interview Question

Q: When should you use Session vs Cache?

Answer:

  • Use Session for user-specific data

  • Use Cache for shared application data

.NET Core Example (Side-by-Side)

Session

HttpContext.Session.SetString("User", "Sampath");

Cache

_memoryCache.Set("User", "Sampath");

Difference:

  • Session → per user

  • Cache → global

Interview Trick Question

Q: Can Session use Cache internally?

Answer: Yes

Session can use:

  • In-Memory

  • Redis

  • SQL Server

Same applies to cache.

Types of Cache (Interview Bonus)

1. In-Memory Cache

IMemoryCache

Stored in server memory

2. Distributed Cache

IDistributedCache

Stored in:

  • Redis

  • SQL Server

  • NCache

Best for load balanced systems

Session Types (.NET Core)

1. In-Memory Session

Stored in server memory

2. Distributed Session

Stored in:

  • Redis

  • SQL Server

Interview Question

Q: Why not use Session for everything?

Answer:

❌ Uses more memory
❌ Not scalable
❌ Slower in distributed systems

Use Cache instead when possible.

Performance Difference

Fastest → Cache
Slower → Session
Slowest → Database

Interview One-Line Answers

What is Session?

Session stores user-specific data on the server.

What is Cache?

Cache stores frequently accessed shared data for performance.

Real Interview Example

Login System:

Use Session:

UserId
UserRole
UserName

Use Cache:

Country List
Settings
Configuration

Pro Interview Tip

Good answer:

Session is user-specific while cache is shared across users. Sessions maintain user state whereas cache improves performance.