Overview

7 Components for messaging and caching

Distributed applications often rely on asynchronous communication so independent services can work without being tightly coupled. In this chapter, message brokers are introduced as the middleware that routes messages between services, allowing one part of a system to hand off work to another part and respond quickly to the user. The chapter contrasts queue-based processing, where a single consumer handles each message, with topic-based processing, where multiple subscribers can receive copies of the same message.

The chapter then shows how to put these ideas into practice with RabbitMQ and Azure Queue Storage in an Aspire-hosted application. Using an order-placement example, the application accepts an order, stores it in a database, and publishes a message so a background service can react asynchronously. RabbitMQ is used to demonstrate durable queues, publishing, and consumption with acknowledgements, while Azure Queue Storage provides a simpler alternative with the same overall pattern. In both cases, Aspire is used to host the supporting infrastructure and connect the API and background worker to the queueing system.

After messaging, the chapter shifts to distributed caching and locking with Redis. It explains how a shared cache can reduce repeated database access and how a distributed lock can prevent concurrent updates from interfering with one another across multiple app instances. The sample uses Redis both to cache product data and to protect order processing with locks that are acquired before updates and released afterward. The chapter closes by showing how Redis Commander, exposed through Aspire, can be used to inspect, manage, invalidate, and troubleshoot cached data and locks directly.

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

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 application?A message broker is a system that mediates communication between services by sending, receiving, and routing messages asynchronously, so services do not need to be directly connected.
What is the difference between queue-based processing and topic-based processing?In queue-based processing, a message is handled by one subscriber and then removed. In topic-based processing, multiple subscribers can each receive a copy of the message.
Why would an application use RabbitMQ for order processing?RabbitMQ lets the app place an order quickly and send a background message to another service, so the user gets an immediate response while other work happens asynchronously.
How do you host RabbitMQ in an Aspire application?You add the Aspire.Hosting.RabbitMQ package to the Aspire host and register the RabbitMQ component in AppHost.cs, then reference and wait for it from the API project.
How does the RabbitMQ order endpoint publish a message?After saving the order in the database, the endpoint creates the orders.created queue, serializes the order ID as JSON, marks the message as persistent, and publishes it to RabbitMQ.
How does the RabbitMQ consumer process incoming order messages?The LocationUpdater background service listens to the orders.created queue, deserializes the message, triggers the initial delivery location update, and acknowledges the message after successful processing.
What is distributed caching and why is Redis useful for it?Distributed caching stores frequently used data in shared memory across multiple servers or app instances. Redis is useful because it provides fast shared access, so all instances can benefit from the same cached data.
How does the chapter use Redis for distributed locking?The app uses Redis lock keys to ensure only one thread or service can modify a resource at a time, preventing race conditions during order placement or product updates.
How do you cache product data with Redis in the /products endpoint?The endpoint checks Redis first using IDistributedCache.GetStringAsync(); if the cache miss occurs, it loads products from the database, serializes them to JSON, stores them with SetStringAsync(), and returns them.
How can Redis Commander help manage the Redis cache in Aspire?Redis Commander is enabled with WithRedisCommander() and appears in the Aspire dashboard, where you can inspect, modify, invalidate, or remove cached data and locks through a browser-based interface.

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


Close
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 Building Distributed Applications that Work ebook for free
Close
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 Building Distributed Applications that Work ebook for free
Close
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 Building Distributed Applications that Work ebook for free