Skip to content
beginnerPhase ·

Cookies

Learn how cookies work for state management in web applications.

30m
0 problems
Topic Progress0%

What are Cookies?

Cookies are small pieces of data stored on the client's browser. They enable stateful interactions in the stateless HTTP protocol.

How Cookies Work

Server Response:
Set-Cookie: session=abc123; HttpOnly; Secure; Path=/; Max-Age=3600

Browser stores the cookie

Subsequent Request:
Cookie: session=abc123

Cookie Lifecycle

1. Server sends Set-Cookie header
2. Browser stores the cookie
3. Browser includes cookie in subsequent requests
4. Server reads Cookie header
5. Cookie expires or is deleted

Cookie Attributes

Attribute Purpose Example
Name=Value Cookie data session=abc123
Domain Which domain can use it .example.com
Path Which URL paths use it /api
Expires Absolute expiration date 2026-12-31T23:59:59Z
Max-Age Relative expiration (seconds) 3600 (1 hour)
Secure Only send over HTTPS Secure
HttpOnly Not accessible via JavaScript HttpOnly
SameSite CSRF protection Strict, Lax, None

Cookie Security

# Secure cookie
Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Strict

# Insecure cookie (avoid)
Set-Cookie: session=abc123

SameSite Attribute

SameSite=Strict  → Never sent cross-site
SameSite=Lax     → Sent on top-level navigation
SameSite=None    → Always sent (requires Secure)

Cookies vs JWT Tokens

Feature Cookies JWT Tokens
Storage Browser (automatic) Client (manual)
XSS Protection HttpOnly flag None by default
CSRF Protection SameSite flag Not vulnerable
Size Limit 4KB No limit
Server-side Can be invalidated Stateless

Best Practices

Key Principles

  1. Follow SOLID principles
  2. Write clean, readable code
  3. Test thoroughly
  4. Document decisions
  5. Monitor in production

Implementation

  • Start simple, refactor as needed
  • Use established patterns
  • Consider trade-offs
  • Review with peers

Continuous Improvement

  • Learn from incidents
  • Update documentation
  • Share knowledge
  • Mentor others

Key Points

  • Understanding Cookies is essential for production systems
  • Always consider scalability and maintainability
  • Test thoroughly before deploying to production
  • Monitor performance and set up alerting

Common Patterns

  1. Validation: Always validate input at the boundary
  2. Error Handling: Use structured error responses
  3. Logging: Log key events for debugging
  4. Testing: Unit, integration, and load tests
  5. Documentation: Keep docs updated with code changes

Practice Problems

0/3solved
Implement Cookies

Design and implement a solution for Cookies in a backend system. Consider scalability, error handling, and production readiness.

Solution
// Cookies implementation
// Key aspects: validation, error handling, logging, testing

public class Cookies {
    // Production-ready implementation
}
Cookies Edge Cases

Identify and handle edge cases for Cookies. What happens under high load, with invalid input, or during failures?

Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency
Cookies Testing Strategy

Write a testing strategy for Cookies. Include unit tests, integration tests, and performance tests.

Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection

Quiz

1. What does the HttpOnly cookie attribute do?

Question 1 options

2. What is the main security risk with cookies that don't use SameSite?

Question 2 options

3. What is the primary purpose of Cookies?

Question 3 options

4. What is a common mistake when implementing Cookies?

Question 4 options

Flashcards

Question

What are cookies used for?

Answer

Storing small pieces of data on the client for state management

Question

What does HttpOnly do?

Answer

Prevents JavaScript access — protects against XSS attacks

Question

What is Cookies?

Answer

Cookies is a key concept in backend development.

Question

When to use Cookies?

Answer

Use Cookies when building production systems that require reliability, scalability, and maintainability.

Question

Cookies best practices

Answer

Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.

Revision Notes

Key Takeaways

  • 1.Cookies enable stateful interactions in stateless HTTP
  • 2.Key attributes: Secure, HttpOnly, SameSite
  • 3.HttpOnly prevents XSS cookie theft
  • 4.SameSite prevents CSRF attacks
  • 5.Cookies are limited to 4KB

Interview Tips

  • Know the difference between cookies and JWT tokens
  • Understand SameSite attribute values

Cheat Sheet

Cookies

  • Purpose: Client-side state storage
  • Security: HttpOnly (no JS access), Secure (HTTPS only), SameSite (CSRF)
  • SameSite: Strict, Lax, None
  • vs JWT: Cookies=automatic, HttpOnly; JWT=manual, stateless