DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Microservices Testing: Key Strategies and Tools
  • Spring Reactive Microservices: A Showcase
  • Reactive Messaging Examples for Quarkus
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation

Trending

  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • Secrets Sprawl and AI: Why Your Non-Human Identities Need Attention Before You Deploy That LLM
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Use Redis in Infrastructure Microservices

How to Use Redis in Infrastructure Microservices

Learn how to bring Redis into the fold.

By 
Martin Forstner user avatar
Martin Forstner
·
Jan. 24, 21 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
13.4K Views

Join the DZone community and get the full member experience.

Join For Free

Back in 2019, I wrote about how to create an event store in Redis. I explained that Redis Streams are a good fit for an event store, because they let you store events in an immutable append-only mechanism like a transaction log. Now, with an update of the sample OrderShop application introduced in that blog, I'm going to demonstrate how to use Redis as a message queue, further demonstrating Redis Enterprise's many use cases beyond caching.

A Quick Look at Microservices, Infrastructure Services, and Distributed Systems

Redis is a great solution for creating infrastructure services like message queues and event stores, but there are a few things you need to take into account when using a microservices architecture to create a distributed system. Relational databases were often good for monolithic applications, but only NoSQL databases like Redis can provide the scalability and availability requirements that are needed for a microservices architecture.

Distributed systems imply a distributed state. According to the CAP theorem, a software implementation can deliver only two out of these three attributes: consistency, availability, and partition tolerance (hence CAP). So, in order to make your implementation fault tolerant, you must choose between availability and consistency. If you choose availability, you'll end up having eventual consistency, which means that the data will be consistent but only after a period of time has passed. Choosing consistency impacts performance because of the need to synchronize and isolate write operations throughout the distributed system.

Event sourcing, which persists the state of a business entity such as an order, or a customer, as a sequence of state-changing events, goes for availability instead of consistency. It allows write operations to be trivial, but read operations are more costly because, in case they span multiple services, they may require an additional mechanism such as a read model.

Communication in a distributed system can be brokered or brokerless. Brokerless styles are well known, with HTTP as its most famous instance. The brokered approach has, as the name implies, a broker between the sender and the receiver of a message. It decouples the sender and receiver, enabling synchronous and asynchronous communication. This results in more resilient behavior as the message consumer does not have to be available at the moment when the message is sent. Brokered communication also allows independent scaling of sender and receiver.

(For more information, see our post on What to Choose for Your Synchronous and Asynchronous Communication Needs-Redis Streams, Redis Pub/Sub, Kafka, etc.)

OrderShop: A Sample E-commerce Implementation

The "Hello World" of a microservice architecture is the OrderShop, a simple implementation of an e-commerce system using an event-based approach. This sample application uses a simple domain model, but it fulfills the application's purpose.

OrderShop is orchestrated using Docker Compose. All network communication is done over gRPC. The central components are the event store and the message queue: each and every service is connected to and only to them over gRPC. OrderShop is a sample implementation in Python. You can see the OrderShop source code on GitHub.

(Note: This code is not production-ready and is for demo purposes only!)

Run and Fun

  • Clone the GitHub repository: https://github.com/redislabs-demos/ordershop-v2
  • Run OrderShop v2 in a simple five-step process:
  1. Start the application with docker-compose up
  2. Open your browser and go to http://localhost:5000/
    1. Watch events and browse state
  3. Run the client with python -m unittest tests/unit.py
  4. Open another tab in your browser to http://localhost:8001/
    1. Use redis:6379 to connect to the test database
  5. Stop the application with docker-compose down

OrderShop v2 Architecture

In this case, the server architecture consists of multiple services. The state is distributed over several domain services but stored in a single event store. The Read model component concentrates the logic for reading and caching the state, as shown here:

The diagram above shows the OrderShop v2 application architecture and data flow.

Commands and queries are communicated via the Message queue component, whereas events are communicated via the Event store component, which also acts as an event bus.

Infrastructure Services

In OrderShop v2, all unicast communication happens over the Message queue component. For this, I'll be using Redis Lists, and in particular, two lists combined into a so-called " reliable queue ". It processes simple commands (e.g. single entity operations) synchronously, but long-running ones (e.g. batches, mails) asynchronously and supports responses to synchronous messages out of the box.

The Event store is based on Redis Streams. Domain services (which are just dummies to demonstrate OrderShop's functionality) are subscribed to event streams named after the event topic (i.e the entity name) and publish events onto these streams. Each event is a stream-entry with the event timestamp acting as the ID. The sum of the published events in the streams results in the state of the overall system.

Application Services

The Read model caches deduced entities from the Event store in Redis using the domain model. Disregarding the cache, it's stateless.

The API gateway is stateless as well, and serves the REST-API on port 5000. It terminates HTTP connections and routes them either to the read model for reading state (queries) or to dedicated domain service for writing state (commands). This conceptual separation between read and write operations is a pattern called Command Query Responsibility Segregation (CQRS).

Domain Services

The domain services receive write operations over the Message queue from the API gateway. After successful execution, they publish an event for each of them to the Event store. In contrast, all read operations are handled by the Read model which gets its state from the Event store.

The CRM service (Customer Relation Management service) is stateless-it's subscribed to domain events from the event store and sends emails to customers using the Mail service.

The central domain entity is the order. It has a field called 'status' which transitions are performed using a state machine, as shown in the diagram below.

The diagram above shows the possible states an order can be in.

These transitions are done in several event handlers, which are subscribed to domain events ( SAGA pattern), for example:

Clients

Clients are simulated using the Unit testing framework from Python. There are currently 10 unit tests implemented. Take a look at tests/unit.py for further details.

A simple UI is served on port 5000 to watch events and browse state (using WebSockets).

A RedisInsight container is also available to inspect the Redis instance. Open the web browser to http://localhost:8001/ and use redis:6379 to connect to the test database.

The animation above shows RedisInsight, the very powerful Redis GUI.

Conclusion

Redis is not only a powerful tool in the domain layer (e.g. a catalog search) and application layer (e.g. a HTTP session store) but also in the infrastructure layer (e.g. an event store or message queue). Using Redis throughout these layers reduces operational overhead and lets developers reuse technologies they already know.

Take a peek at the code and try your hand at implementing it. I hope this helps demonstrate Redis' versatility and flexibility in domain and infrastructure services and proves how it can be used beyond caching.

Let me know how it goes on Twitter: @martinez099.

Redis (company) Database microservice Event Infrastructure Event store Web Service Relational database application unit test

Published at DZone with permission of Martin Forstner. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Microservices Testing: Key Strategies and Tools
  • Spring Reactive Microservices: A Showcase
  • Reactive Messaging Examples for Quarkus
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!