Rajat Gupta
Rajat Gupta

Reputation: 26587

Caching user specific data at database level or session

Should user specific data be cached at the database level or inside JSF sessions scope ?

Upvotes: 1

Views: 1116

Answers (1)

bryanmac
bryanmac

Reputation: 39296

There is no one size fits all answer to that question. In a distributed app, state can be stored client side (cookies, etc...), on the application tier in memory, or in more persistent storage like a database. You also don't have to pick one - you can combine.

Instead, there are considerations for your user state data:

  • In memory session data is simple but you have to be concerned with footprint - a large memory footprint with many users can be problematic. If you're storing MBs of data with a large number of users, consider persistent storage.
  • In memory session state can be problematic for services that you want to scale out to many application tier servers. It can be mitigated with load balancing affinity but that inhibits scalability as well.
  • In memory state should hold just that state - user data should be persisted. Always ask yourself what if this application tier goes down - do we care about that state? Can the user recover? What's the experience? Persistent storage has that advantage.
  • Databases are better for storing large amounts of data but you can incur round trips to retrieve it.
  • Databases are better for user specific date that needs to span sessions.
  • Client side cookies are an option but you have to keep the size very small since they're sent with requests.
  • Just like databases, client side cookies can span sessions if needed.

There's other combinations like in memory caches that read and write through to a database and cache data for a certain lifetime. That's an option if the data doesn't absolutely have to be up to date.

Another different approach is RESTful apps.

Don't hold state at all in the services - everything goes back to persistent storage and the resources are cacheable outside the app server (akamai, client etc...). It typically leads to more scalable applications (even if a single operation may not be as fast). At that point, persistent storage options define how you scale.

Upvotes: 2

Related Questions