🦉
Programming Notes
  • My Programming Notes
  • CKA Exam Preparation
    • Certified Kubernetes Administrator
    • Setup Minikube
    • Network Design Principles
    • Role-Based Access Control (RBAC)
    • Namespace
    • Resource Quota
    • Pod
    • Deployment
    • Deployment: Rollout
    • ConfigMap
    • Service
    • Service: kubectl expose
    • Pod: Resources Management
    • Pod & Container: Quality of Service Class
    • Pod & Container: Probes
    • Limit Range
    • Scaling: Manual
    • Scaling: Horizontal Pod Autoscaler
    • Persistent Volume & Claim
    • Secret
    • Ingress: Routing
    • Ingress: TLS
    • Ingress: Rate Limit
    • Ingress: Basic Auth
    • Ingress: CRD (Custom Resource Definition)
    • Job
    • CronJob
    • Mutli-Node Cluster
  • Golang
    • Generics
    • Context
    • Goroutines and Channels in Go
    • Goroutine: Concurrency vs Parallelism
    • Goroutine: Performance & Tradeoffs
    • JSON: omitzero
  • Rust
    • Arrays & Slices
    • Closures
    • Generics & Traits
    • Iterators
    • Run Code Simultaneously
    • String vs &str
    • Tests
    • Rustlings Exercises
      • Variables
      • Functions
      • If
      • Primitive Types
      • Vectors
      • Move Semantics
      • Structs
      • Enums and Matching Pattern
      • Strings
      • Modules
      • Hashmaps
      • Options
      • Error Handling
      • Generics
      • Traits
      • Lifetimes
      • Tests
      • Iterators
      • Smart Pointers
      • Threads
      • Macros
      • Quiz 1
      • Quiz 2
      • Quiz 3
  • Software Engineering
    • CAP Theorem
    • Circuit Breaker
    • Decoupling
    • GraphQL: Query Caching
    • HMAC Signature
    • Idempotency
    • Monolith VS Microservice
    • OWASP Top 10 2021
    • PCI DSS
    • PostgreSQL: Partitioning
    • PostgreSQL: Replication
    • Protobuf & gRPC
    • Redis: Streams
    • Resource Scaling
    • Signed URL
    • SOLID
    • Stack VS Heap
    • Stateful VS Stateless
  • Site Reliability Engineering
    • Chaos Engineering
    • Distributed Tracing
    • Kubernetes (k8s)
    • SLA, SLO, and SLI Metrics
    • Site Reliability Engineer
  • Others
    • FFMPEG Cheat sheet
Powered by GitBook
On this page
  • Stateful
  • Characteristics
  • Advantages
  • Disadvantages
  • Examples
  • Stateless
  • Characteristics
  • Advantages
  • Disadvantages
  • Examples
  • Conclusion
  1. Software Engineering

Stateful VS Stateless

The terms stateful and stateless are often used in software development to describe how systems, applications, or services handle and manage state, which refers to data or information about previous interactions.

Aspect
Stateful
Stateless

State Retention

Yes

No

Request Context

Depends on previous interactions

Independent

Complexity

Higher

Lower

Scaling

Challenging

Easier

Recovery

Requires state restoration

Simple, as no state exists

Examples

Online games, database sessions

RESTful APIs, HTTP, DNS

Stateful

A stateful system or application remembers the state of a client’s interactions across multiple requests or sessions.

Characteristics

  • Maintains State: Keeps track of client-specific data or the progress of operations.

  • Dependent on Context: Each request is tied to previous interactions.

  • Session Management: Often requires mechanisms like session IDs or cookies to maintain state.

  • Resource Intensive: Needs additional storage or memory to track state.

Advantages

  • Tailored, context-aware experiences for users.

  • Efficient for workflows requiring multiple steps.

Disadvantages

  • Complex to scale because state must be shared or replicated across servers.

  • More challenging to recover from failures as the state needs to be restored.

Examples

  • TCP: because bot system need to maintain information about the session it self during it's life cycle.

  • Session-based Authentication: because server save all the information about the session using session_id.

Stateless

A stateless system or application does not retain any state between requests. Each request is treated as independent and self-contained.

Characteristics

  • No Memory of Previous Interactions: Every request contains all the information needed for processing.

  • Easier to Scale: Each request is independent, allowing for horizontal scaling.

  • Simpler Recovery: No state to restore after a failure.

  • Lightweight: Requires fewer resources since no state is stored.

Advantages

  • Scalability and fault tolerance are simpler to achieve.

  • Easier to maintain and test due to independence of requests.

Disadvantages

  • May require additional data to be sent with each request, increasing overhead.

  • Limited suitability for complex workflows requiring state.

Examples

  • HTTP Protocol: Each HTTP request is stateless by default.

  • RESTful APIs: REST principles advocate for stateless communication.

  • Serverless Computing: Functions are invoked and execute without retaining context.

  • DNS: Resolves domain names to IP addresses without maintaining state.

Conclusion

  • Stateless APIs are the preferred choice for scalable systems because they simplify horizontal scaling and fault tolerance.

  • Stateless APIs are inherently easier to scale because each request is self-contained and does not rely on server-side state. This makes it easier to distribute requests across multiple servers.

  • Use stateless communication protocols (e.g., REST) and include all necessary data (like authentication tokens or request context) in every API request.

  • Use token-based authentication like JWT (JSON Web Tokens), where authentication data is encoded in tokens and verified without server-side state.

PreviousStack VS HeapNextSite Reliability Engineering

Last updated 4 months ago