REST’s Superpower

Maintaining Session State on the Consumer Side Changes Everything

Dick Dowdell
Nerd For Tech
5 min readSep 23, 2024

--

Roy Fielding is a genius. It’s too bad so few people understand how powerful Representational State Transfer really is. Let’s take a quick look at how it can overcome one of the most stubborn challenges facing microservices development.

The REST principle that “session state is maintained on the consumer side” plays a significant role in addressing the challenges of multi-step transactions in microservices implementations — by promoting stateless interactions and simplifying the complexity of managing state across services.

Here’s how that principle can help solve microservices’ multi-step transaction problem:

Statelessness Reduces Complexity

In a microservices architecture, each service is designed to be stateless, meaning the service doesn’t remember the state between requests.

By maintaining session state on the consumer side, you remove the need for services to track multi-step transactions across different instances or services. Each request contains all the necessary information for the service to process the request independently.

For multi-step transactions, the consumer (client) can store the intermediate states or steps and send them back in subsequent requests. This prevents services from needing to manage the transaction state, which can be error-prone and complex, especially when scaling horizontally across many instances.

Compensating Transactions for Long-Running Operations

In multi-step transactions that span multiple services, the principle of keeping the state on the consumer side enables each service to focus on performing its operation and returning the result, leaving it up to the client to initiate the next step in the process.

If one step fails, the consumer can undo the actions of previous steps. By keeping the state on the consumer side, each individual microservice is isolated from the overall transaction, reducing dependencies and allowing for failure recovery on a per-step basis.

Client-Managed Orchestration

When session state is held by the consumer, the client becomes responsible for orchestrating the multi-step process. This is beneficial for microservices implementations because it decentralizes transaction management. The client controls the transaction flow and the sequence of operations across different services. In this approach, the client retains all intermediate states. The client decides when to commit or roll back.

The services stay simple, handling each request in isolation without needing to worry about multi-step coordination. This approach aligns with REST’s stateless constraint and minimizes the complexity of building distributed applications.

Scalability and Fault Tolerance

Stateless services are easier to scale because they don’t store session information. Each request can be routed to any instance of the service without worrying about session identity.

When the state is managed on the consumer side, services can be restarted or scaled dynamically without affecting the continuity of multi-step transactions.

This leads to better fault tolerance since the client holds the state, and service failures do not disrupt the transaction flow.

Reducing Service Dependencies

When each service is stateless and the session state is externalized, it reduces tight coupling between services.

Services don’t need to coordinate with each other to track the progress of a transaction. This simplifies service interactions, making it easier to maintain and evolve the application over time.

Implementing a Multi-Step Transaction in REST

Let’s take a scenario where a client needs to book a trip involving multiple microservices:

  1. Book a flight via the AirlineBookingService.
  2. Book a hotel via the HotelBookingService.
  3. Book a car rental via the CarRentalService.

The Steps

  1. The client (consumer) starts by calling the AirlineBookingService and stores the flight confirmation number (session state).
  2. It then uses this state to proceed to HotelBookingService, storing the hotel confirmation number, and so on.
  3. If any service (such as hotel booking) fails , the client can invoke compensating actions to cancel the previously booked flight, ensuring transactional consistency across services.

The only state management on the server side is the final update of the persistent resources affected by the scenario. All caching of state information to implement the separate steps was on the client side where it can be most straightforward, scalable, and economical.

Unlike gRPC and GraphQL, this consumer-side management enables microservices to remain stateless, while the client can manage the entire transaction without requiring complex coordination or distributed locking between services.

Wrapping Up

REST’s principles support the need for scalability and simplicity in microservices implementations better than anything that could be accomplished inside the individual services themselves.

REST is not an API, but is an architectural style! Proper RESTful APIs should implement REST principles.

If you found this article useful, a clap would let us know that we’re on the right track.

Thanks!

Suggested Reading

The Benefits of REST

REST (Representational State Transfer) is highly useful for networked applications due to several key features that make it both scalable and flexible:

1. Statelessness: Each REST request from a client to a server contains all the necessary information for the server to fulfill the request. The server does not need to retain session context, which simplifies scalability since each request is independent.

2. Uniform Interface: REST relies on a uniform interface, typically through standard HTTP methods like GET, POST, PUT, DELETE, etc. This allows for a consistent way to interact with different resources, simplifying how clients interact with services across a network.

3. Resource-Based: In REST, everything is considered a resource, and each resource can be identified by a URI (Uniform Resource Identifier). This clear structure helps in defining and managing resources, leading to easier mapping of business logic to web services.

4. Scalability: RESTful applications can be distributed across multiple servers, which increases scalability. Statelessness combined with caching mechanisms allows REST to handle a high number of simultaneous interactions across distributed applications.

5. Cacheability: HTTP’s caching mechanisms can be leveraged by RESTful applications to reduce the load on the server. If certain responses are cacheable, it helps improve performance and efficiency by minimizing repeated server calls.

6. Layered Architecture: REST supports a layered application architecture, allowing intermediaries like message moderators, load balancers, proxies, or gateways to sit between the client and server. This separation enables better scalability, security, and manageability.

7. Language and Platform Independence: REST relies on standard protocols (primarily HTTP), making it language- and platform-agnostic. This means that different services developed in different languages or on different platforms can still communicate seamlessly using REST.

8. Separation of Concerns: REST decouples the client from the server, allowing them to evolve independently. Clients do not need to know the details of how the server processes data, and the server doesn’t need to know about the client’s interface details.

9. Human-Readable and Machine-Friendly: RESTful services typically return responses in formats like JSON or XML, which are both human-readable and easily processed by machines. This makes debugging easier and fosters better integration between different applications.

10. Flexibility and Extensibility: REST is flexible in terms of data formats and communication patterns, making it a good choice for applications that evolve over time or need to support various types of devices or platforms.

These factors contribute to REST’s widespread use in networked applications, where simplicity, scalability, and flexibility are critical.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Dick Dowdell
Dick Dowdell

Written by Dick Dowdell

A former US Army officer with a wonderful wife and family, I’m a software architect and engineer who has been building software systems for 50 years.

Responses (1)