interview community logo

2
0
What is the difference between stateful and stateless service?
1 answer

What is the difference between stateful and stateless service?

Angular Robert Isaac asked 10 months ago


0

A stateful service is a service that can do different actions when being called multiple times, or depending on the order

the best example is AuthService, typically it will fetch the user details from the API on the first call to get it, and after that it will always return the same user (here different actions since first time it made HTTP call, second time it didn't), also if the user logged out it and you called to get the user details it won't call the user API but it will return null instantly so depending on if the user called logout or not it will have different response

a stateless service will always do the same action no matter how many times you called the method, and no matter what order of its methods you called

example for this is a the question service in this website, now no matter which component is calling it, no matter if the user is logged in or not it will always call the questions API with the provided question id, it can transform the response for example to match the UI model needed but it's still stateless because given the same ID it will always call the API and return the same model

Robert Isaac answered 9 months ago loading comments...