# Sessions and Cookies

> Implement login with server-side sessions and secure cookie settings, the classic and still excellent approach. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/backend/sessions-cookies
Last updated: 2026-11-28

Implement login with server-side sessions and secure cookie settings, the classic and still excellent approach.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [HTTP in Depth](https://www.internhack.in/roadmaps/tracks/backend/http-protocol)

HTTP is stateless, so after a user logs in the server needs a way to recognize them on the next request. The classic approach: create a **session** (a record on the server, often in Redis or the database), and send the browser a **cookie** containing a random, unguessable session id. The browser sends it back automatically on each request.

### Cookie attributes that matter

- `HttpOnly`: JavaScript cannot read the cookie, limiting damage from XSS.
- `Secure`: only sent over HTTPS.
- `SameSite=Lax` or `Strict`: not sent on most cross-site requests, a strong defense against **CSRF**.
- `Max-Age` / `Expires`, `Path` and `Domain`: control lifetime and scope.

### Session lifecycle

- **Regenerate** the session id at login to prevent session fixation.
- Expire sessions after inactivity and absolutely after some maximum.
- On logout, delete the server-side session, not just the cookie.

### Why sessions are a great default

They are simple, revocable instantly (delete the record), and keep sensitive data on the server. For a typical web app with its own frontend, sessions are often a better choice than JWTs.

### Pitfalls

- Storing sessions in process memory, which breaks as soon as you run two instances.
- Using cookies for cross-site APIs without understanding CORS credentials and SameSite.

You are ready to move on when you can build login and logout with Redis-backed sessions and explain each cookie attribute you set.

## Key concepts

- **Stateful sessions**
- **HttpOnly, Secure, SameSite**
- **Session stores (Redis, database)**
- **Session fixation and regeneration**
- **CSRF protection**
- **Remember-me and expiry**

## Mini project

Add email and password login to an API using Redis-backed sessions, with session regeneration at login, an idle timeout and a logout-everywhere endpoint.

## Check yourself

How does SameSite=Lax help against CSRF, and what cases does it not cover?

## Free resources

- [MDN: Using HTTP cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) (MDN)
- [OWASP Session Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html) (OWASP)

Previous: [SOAP and Legacy Integrations](https://www.internhack.in/roadmaps/tracks/backend/soap-legacy-apis) · Next: [Token-Based Auth and JWT](https://www.internhack.in/roadmaps/tracks/backend/token-auth-jwt)

## More in Who Is Calling and What Can They Do

- [Sessions and Cookies](https://www.internhack.in/roadmaps/tracks/backend/sessions-cookies)
- [Token-Based Auth and JWT](https://www.internhack.in/roadmaps/tracks/backend/token-auth-jwt)
- [OAuth 2.0 and OpenID Connect](https://www.internhack.in/roadmaps/tracks/backend/oauth2-oidc)
- [SAML and Enterprise Single Sign-On](https://www.internhack.in/roadmaps/tracks/backend/saml-enterprise-sso)
- [Authorization: Roles and Permissions](https://www.internhack.in/roadmaps/tracks/backend/authorization-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
