• Level Up Coding
  • Posts
  • LUC #53: Embracing Simplicity: A Look Into Stateless Design

LUC #53: Embracing Simplicity: A Look Into Stateless Design

Plus, tokenization explained, how event-driven architecture works, and how the TCP handshake works

This week’s issue brings you:

READ TIME: 4 MINUTES

A big thank you to our partner Postman who keeps this newsletter free to the reader.

Imagine if you could auto-generate API tests. That’s now possible. All you have to do is send a request, Postbot can take care of the rest for you. Check it out.

Breaking Down Stateless Design

Stateless design is a powerful model that has led to the development of simple yet highly scalable and efficient applications.

“State” refers to stored information that systems use to process requests. This information can change over time as users interact with the application or as system events occur.

Before we dive into the specifics of stateless design, let’s take a look at the alternative—stateful applications.

What are Stateful Applications?

With stateful applications, client data such as user ID, session information, configurations, and preferences are stored to help process requests for a given user.

Depending on the functionality and requirements of the application, additional data may be saved, such as shopping cart information for an online store or transaction history for a FinTech service.

A stateful design allows applications to provide a personalized experience to their users while removing the need to share data across multiple requests.

For this reason, it is a popular approach for applications with user preferences such as streaming services and online games.

The Birth of Stateless Design

As applications grew in complexity and received increasing amounts of traffic, the limitations of stateful design became apparent.

Managing and maintaining session data adds significant performance overhead and complexity to a system.

This complexity made it difficult for systems to scale horizontally as sharing state across multiple instances becomes a challenge.

The rapid need for scalability and efficiency drove the popularity of stateless design.

Instead of having mechanisms for state management, requests contained all the information needed to process it.

This allowed systems to handle high levels of requests while adding flexibility to how the system scales, making it more resource efficient.

Use Cases for Stateless Design

Stateless design has risen in popularity due to its alignment with trends in modern computing such as serverless architecture and microservices.

One of the key principles behind microservices is that each service is stateless. This allows microservices to scale independently and ensures resource consumption stays efficient.

Serverless computing follows the same concept — each function is invoked independently.

Even applications that require session management can benefit from implementing a stateless design in components of their system.

For example, most RESTful APIs are stateless where each API call contains all necessary information.

Content delivery networks (CDNs) also follow a stateless design so that every request can be fulfilled by any server in the network without needing to sync session data between all servers or query a single session management store.

Disadvantages of Stateless Design

The size of requests can be considerably larger in stateless design.

Moreover, sending data across multiple requests can introduce significant inefficiencies that are far greater than the alternative of managing and querying this data from a central storage system.

It is important to note that stateless design should only be implemented for use cases that are truly stateless.

Although stateful design has its share of disadvantages, workarounds can add more complexity and fragility.

Final Thoughts

Most applications pick a hybrid approach between stateful and stateless design, depending on the needs and constraints of each component.

The key to a well-designed system is balance.

It should be scalable, simple, and fast without sacrificing functionality.

Tokenization Explained

Tokenization is a security technique that replaces sensitive information with unique placeholder values called tokens. By tokenizing your sensitive data, you can protect from unauthorized access and lessen the impact of data breaches, whilst simplifying the system by scaling back on security measures in other areas of the system.

Tokenization process:

Sensitive data is sent to a tokenization service when it enters the system. There, a unique token is generated, and both the sensitive data and the token are kept in a secure database known as a token vault. For extra protection, the sensitive data is generally encrypted within the secure data storage. The token is then used in place of the sensitive data within the system and third-party integrations.

Detokenization process:

When an authorized service requires sensitive data, it sends a request to the tokenization service that contains the token. The tokenization service validates that the requester has all the required permissions. If it does, it uses the token to get the sensitive data from the token vault and returns it to the authorized service.

How The TCP Handshake Works (Recap)

Transmission Control Protocol (TCP) is a transport protocol that is used on top of Internet Protocol to ensure reliable transmission of packets. Essentially, it ensures that all the data you send over the internet reaches its destination correctly and in order.

For devices on a network to exchange data, a connection must first be established. That's where the TCP handshake comes in.

The TCP handshake follows a three-step process to establish a connection:

1) SYN (Synchronize)
2) SYN-ACK (Synchronize-Acknowledge)
3) ACK (Acknowledge)

The TCP handshake uses a flag and sequence number at each step. The flag informs the receiving device of the segment's contents. The sequence number indicates the order of sent data, allowing the receiving end to reassemble data in the correct order.

What is Event-Driven Architecture, and How Does it Work? (Recap)

EDA is a software design pattern that emphasizes the production, detection, consumption of, and reaction to events.

Adding an item to a shopping cart, liking a post, and paying a bill are all state changes that trigger a set of tasks in their respective systems.

EDA has four main components: events, producers, consumers, and channels.

  • Events: These are significant changes in state. They're generally immutable, typically lightweight and can carry a payload containing information about the change in state.

  • Producers: The role of a producer is to detect or cause a change in state, and then generate an event that represents this change.

  • Consumers: Consumers are the entities that are interested in and react to events. They subscribe to specific types of events and execute when those events occur.

  • Channels: Meanwhile, channels facilitate sending events between producers and consumers.

That wraps up this week’s issue of Level Up Coding’s newsletter!

Join us again next week where we’ll explore API testing types, network protocols, data processing systems, and more.