Saturday, 24 August 2013

Database Session State Design Pattern

Master Enterprise Architecture & Java!

Subscribe to Ram N Java for clean, beginner-friendly software architecture tutorials.

👉 Subscribe Now on YouTube

What is Database Session State?

The Database Session State Design Pattern belongs to the session state patterns family within Martin Fowler's Patterns of Enterprise Application Architecture (PofEAA). Its core rule is simple: store user session data as committed data directly in a persistent database.

1. Why Do We Need Sessions?

When a user visits a web application and logs in with their credentials, a unique session is created. This session keeps track of conversational state across multiple requests, including:

  • The user's identity and account information.
  • Client details such as IP address and machine ID.
  • In-progress workflow items like shopping cart data or multi-step form progress.

2. How the Pattern Works Step by Step

Instead of relying solely on server memory (RAM), session records are written to a database table:

  1. Session Creation: When the user submits valid login credentials, the web server creates the session and persists the record into the database before closing the transaction.
  2. Subsequent Requests: When the user returns or makes subsequent HTTP requests, the client browser supplies the session ID.
  3. Data Retrieval: The server reads the session ID, queries the database, retrieves the stored user session data, and seamlessly resumes processing without recomputing the session state from scratch.

3. Advantages: Clustering and Failover

The primary reason architects choose this pattern is high reliability in distributed enterprise environments:

  • Straightforward Failover: If a specific application server crashes midway through a user's transaction, another node in the cluster can immediately take over. Because session data lives in a central database, nothing is lost.
  • No Sticky Sessions Needed: Any web server instance in a cluster can handle any request for any user.

4. Key Trade-Off: Database Overhead

While database persistence delivers resilience, it comes with a trade-off:

  • Performance Cost: Querying the database to fetch and update session state on every HTTP request is significantly slower than reading directly from in-memory cache or RAM.
  • Database Load: High concurrent traffic can place substantial read/write pressure on your database tier.

Recommended Related Videos from Ram N Java

Deepen your enterprise architecture knowledge with these related tutorials:


Click here to watch in Youtube : https://www.youtube.com/watch?v=nXBNY7GK--A

Click the below Image to Enlarge
Database Session State Design Pattern















See also:

  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials