Session Data Storage: What to Store and What to Avoid
What you'll learn: How to make smart decisions about what belongs in session storage and what creates security risks or performance problems.
The Golden Rule of Session Storage
Think of your session storage like a carry-on bag at the airport: you want just enough essentials to make your journey smooth, but overpacking creates problems. Sessions should contain only what's necessary for the server to recognize you and maintain your state between requests.
What TO Store in Sessions
Safe and appropriate session data:
- User ID or account reference (not the password!)
- Authentication status and timestamp
- User role or permission level
- Temporary workflow state (like multi-step form progress)
- Shopping cart IDs (store the cart reference, not every item detail)
- UI preferences (language, theme)
What to AVOID Storing
Never put these in sessions:
- Passwords or password hashes
- Credit card numbers or full payment details
- Social security numbers or national IDs
- Personal health information
- Full database records (store IDs instead)
- Large objects or binary data
Why This Matters
Storing sensitive data in sessions creates multiple risks: if an attacker hijacks a session through any vulnerability you've learned about, they instantly access everything stored there. Additionally, bloated sessions consume server memory, slow down your application, and make session management inefficient.
The reference principle: Store identifiers and look up details as needed. Instead of storing user: {name, email, address, phone, ssn}, just store userId: 12345 and query the database when you need those details.
Key Takeaway: Store only non-sensitive identifiers and minimal state data in sessions—never passwords, payment details, or full user records. Keep sessions lean for both security and performance.