Repository navigation
Stateless Stateful
State is information an application remembers, such as the selected patient or logged-in user.
A stateless component processes each request without relying on client information remembered from previous requests. Each request supplies the necessary inputs.
Example: You send the patient ID every time you request that patient's details.
A stateful component remembers information between requests and uses it in later interactions.
Example: You select a patient once. The application remembers the selection and uses it when you request the selected patient's details.
| Feature | Stateless | Stateful |
|---|---|---|
| Previous client information | Does not rely on remembered information | Remembers information between requests |
| Request inputs | Supplies the required context each time | Can reuse saved context |
| Example | Send patient ID with every lookup | Remember the selected patient |
| Multiple servers | Easier to distribute requests | Saved state must be available to the next server |
| Database access | Can read and write data | Can read and write data |
Note: Stateless does not mean “no database.” Database records and temporary session information are different.
This diagram compares two possible designs for viewing patient details.
sequenceDiagram
participant B as Browser
participant S as Stateless service
participant F as Stateful service
B->>S: Get details using patient ID
S-->>B: Patient details
Note over S: No saved patient selection required
B->>S: Send patient ID again
S-->>B: Patient details
B->>F: Select a patient
Note over F: Remember selection in session
F-->>B: Selection saved
B->>F: Show selected patient's details
F-->>B: Use remembered selection
-
Stateful behavior:
SessionControlleruses@SessionScopedto remember information such as the logged-in user and selected department. -
Stateless EJB:
PatientFacadeuses@Statelessfor database operations without retaining a client conversation.
@SessionScopedand@Statefulare different Java annotations. A controller can have stateful behavior without being an@StatefulEJB.