Overview

7 Components for messaging and caching

The chapter explains how distributed applications rely on asynchronous communication and shared infrastructure to stay responsive and scalable. It begins with message brokers, showing how queues let one service publish work while another consumes it later, which keeps user-facing requests fast even when additional processing must happen in the background. It also distinguishes between queue-style processing, where a message is handled once, and topic-style processing, where multiple subscribers can receive the same message.

Using Aspire, the text demonstrates this idea with RabbitMQ and Azure Queue Storage. An order-placement flow is built so that a customer can submit an order, the API records it in the database, and a message is published for a background service to react to later. The consumer updates delivery tracking after receiving the message, and the examples show how Aspire wires up the broker, how queues are declared and used, and how the same general pattern can be implemented with different messaging technologies.

The chapter then shifts to distributed caching and locking with Redis. Caching is used to avoid repeated database reads by storing frequently requested product data in shared memory, while distributed locks prevent multiple threads or services from modifying the same resource at the same time. The examples show how to register Redis in Aspire, use it both as a cache and as a locking mechanism, and manage the stored data through Redis Commander, reinforcing the broader lesson that shared infrastructure can improve both performance and correctness in distributed systems.

Message-broker workflows
Triggering an order-tracking service when an order is placed
Multiple applications accessing the same distributed cache
Distributed locking controls which applications can access a database resource.
Redis Commander endpoint in the Aspire dashboard
Redis Commander interface

Summary

  • Message brokers transfer data between applications and services via message queues. Although message-broker technologies are built differently, their underlying operational principles are similar.
  • Aspire supports a wide variety of message-broker integrations:
    • RabbitMQ is a popular technology-agnostic message broker that is highly configurable.
    • Azure Queue Storage, as the name suggests, is specific to Azure and is part of an Azure Storage account.
  • Azure Queue Storage may be preferable to RabbitMQ in some situations because it’s significantly easier to set up and work with, though it loses some of RabbitMQ’s flexibility.
  • A cache is an in-memory data store from which data can be retrieved more quickly than from a database. Caching is useful for storing data temporarily so that it can be retrieved faster, which results in better user experience.
  • A distributed cache is an in-memory data store that multiple services on the network can access.
  • A distributed lock can be implemented with a caching mechanism and shared among services to ensure that only one thread at a time can access a resource. Locking functionality is useful while particular data is being updated because it ensures that no other processes try to update it.
  • Aspire can host a Redis component, which can be used for both distributed caching and locking. It’s the same component that can be used for output caching, but the locking and distributed caching functionality use a different client library from the one used for output caching.
  • Redis Commander is a tool that allows users to manage Redis directly. It can integrate with Aspire natively.

FAQ

What is a message broker in a distributed system?A message broker mediates communication between applications or services by sending, receiving, and routing messages asynchronously. It lets services communicate without being directly connected.
What is the difference between queue-based and topic-based processing?Queue-based processing usually has one subscriber, and a message is removed after it is processed. Topic-based processing can have many subscribers, and each subscriber receives a copy of the message.
Why would you use RabbitMQ in an Aspire application?RabbitMQ is useful when you need reliable asynchronous communication between services. In Aspire, it can be hosted as a component and used to handle background work such as order notifications.
How do you host a RabbitMQ component in Aspire?You add the Aspire.Hosting.RabbitMQ package to the host project and register the component in AppHost.cs, then reference it from the services that need to use it.
Why does placing an order use a message queue instead of doing all the work synchronously?Using a queue lets the order page respond immediately while background services handle follow-up work, such as notifying the delivery tracking system. This improves user experience and keeps the workflow responsive.
What is the purpose of orders.created in the RabbitMQ example?orders.created is the queue used to publish a message when an order is placed. The consumer listens to that queue and starts the downstream order-tracking workflow.
What is distributed caching used for in this chapter?Distributed caching stores frequently accessed data in shared memory so multiple application instances can reuse it. In the chapter, it is used to cache product data and avoid repeated database reads.
How does Redis help with both caching and locking?Redis can act as a shared distributed cache and also store lock entries for distributed locking. That means it can speed up reads and help prevent concurrent updates from interfering with each other.
Why is distributed locking useful when multiple users place orders at the same time?Distributed locking ensures that only one process updates a shared resource at a time. This prevents race conditions and keeps stock or order-related data consistent.
How can you manage Redis data in Aspire?You can enable Redis Commander with the Redis component and access it from the Aspire dashboard. It provides a visual and command-line interface for inspecting, modifying, or clearing cached data and locks.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work ebook for free