Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the "session per request" pattern take advantage of the cache? ("Session per session" or "Session per request")

I build a new application from scratch this days in a web application.
(The technologies are Asp.Net and the ORM I'm using is Entity Framework. if it matters)

I'm uncertain if the widely used pattern session per request is really a good one.
As I see it, the advantage of the pattern is that the cache isn't increases until the database session crash\ being too big and thus inefficient.

But isn't a new session for every request is too much? It means every server call reset the cache, even simple ajax request like auto-complete has a brand new cache, in fact for every key stroke the cache resets.

The chances you will query the same object-entity-row in one request is small.

Isn't Session per session is a better pattern? it has both the advantages meaning

  1. The cache won't grow for ever.
  2. The cache can actually be used...

So... Why is session per request is so widely used and session per session is not?


Clarifications:

  1. When I wrote ORM session it applies both to NHibernate's session and EntityFramework's DbContext.
  2. I do mean to flush-commit-SaveChanges of the session\dbcontext on each request.
like image 824
gdoron is supporting Monica Avatar asked Nov 17 '25 17:11

gdoron is supporting Monica


1 Answers

Session per request pattern is more natural and robust for using with ORM. It has smaller chances to get dirty entities and has more predictable resource management.

If I got you right and you mean DbContext instance under Session than Session Per Session can be used only in application without data modification, otherwise you would get unexpected data submitting by a request while other request performs data modification. Also I'm not sure Entity Framework context is thread safe - while processing requests is multithread.

I not totally sure but I think Entity Framework doesn't use cache (== identity mapping) as wide as you expect. On selecting entity set it queries database even if all data are in cache - it can only avoid constructing new entities but using existing ones from identity map.

For caching there are other solutions and they are better.

like image 189
STO Avatar answered Nov 19 '25 07:11

STO