Quickstart: Route Azure Cache for Redis events to web endpoint with the Azure portal

Azure Event Grid is an eventing service for the cloud. In this quickstart, you'll use the Azure portal to create an Azure Cache for Redis instance, subscribe to events for that instance, trigger an event, and view the results. Typically, you send events to an endpoint that processes the event data and takes actions. However, to simplify this quickstart, you'll send events to a web app that will collect and display the messages.

If you don't have an Azure subscription, create a free account before you begin.

When you're finished, you'll see that the event data has been sent to the web app.

Azure Event Grid Viewer scaling in JSON format.

Create an Azure Cache for Redis cache instance

  1. To create a cache, sign in to the Azure portal and select Create a resource.

    Create a resource is highlighted in the left navigation pane.

  2. On the New page, select Databases and then select Azure Cache for Redis.

    On New, Databases is highlighted, and Azure Cache for Redis is highlighted.

  3. On the New Redis Cache page, configure the settings for your new cache.

    Setting Suggested value Description
    DNS name Enter a globally unique name. The cache name must be a string between 1 and 63 characters that contains only numbers, letters, or hyphens. The name must start and end with a number or letter, and can't contain consecutive hyphens. Your cache instance's host name will be <DNS name>.redis.cache.windows.net.
    Subscription Drop down and select your subscription. The subscription under which to create this new Azure Cache for Redis instance.
    Resource group Drop down and select a resource group, or select Create new and enter a new resource group name. Name for the resource group in which to create your cache and other resources. By putting all your app resources in one resource group, you can easily manage or delete them together.
    Location Drop down and select a location. Select a region near other services that will use your cache.
    Pricing tier Drop down and select a Pricing tier. The pricing tier determines the size, performance, and features that are available for the cache. For more information, see Azure Cache for Redis Overview.
  4. Select the Networking tab or click the Networking button at the bottom of the page.

  5. In the Networking tab, select your connectivity method.

  6. Select the Next: Advanced tab or click the Next: Advanced button on the bottom of the page.

  7. In the Advanced tab for a basic or standard cache instance, select the enable toggle if you want to enable a non-TLS port. You can also select which Redis version you would like use, either 4 or (PREVIEW) 6.

    Redis version 4 or 6.

  8. In the Advanced tab for premium cache instance, configure the settings for non-TLS port, clustering, and data persistence. You can also select which Redis version you would like use, either 4 or (PREVIEW) 6.

  9. Select the Next: Tags tab or click the Next: Tags button at the bottom of the page.

  10. Optionally, in the Tags tab, enter the name and value if you wish to categorize the resource.

  11. Select Review + create. You're taken to the Review + create tab where Azure validates your configuration.

  12. After the green Validation passed message appears, select Create.

It takes a while for the cache to create. You can monitor progress on the Azure Cache for Redis Overview page. When Status shows as Running, the cache is ready to use.

Create a message endpoint

Before subscribing to the events for the cache instance, let's create the endpoint for the event message. Typically, the endpoint takes actions based on the event data. To simplify this quickstart, you'll deploy a pre-built web app that displays the event messages. The deployed solution includes an App Service plan, an App Service web app, and source code from GitHub.

  1. Select Deploy to Azure in GitHub README to deploy the solution to your subscription.

Deploy to Azure button.

  1. On the Custom deployment page, do the following steps:

    1. For Resource group, select the resource group that you created when creating the cache instance. It will be easier for you to clean up after you are done with the tutorial by deleting the resource group.
    2. For Site Name, enter a name for the web app.
    3. For Hosting plan name, enter a name for the App Service plan to use for hosting the web app.
    4. Select the check box for I agree to the terms and conditions stated above.
    5. Select Purchase.
    Setting Suggested value Description
    Subscription Drop down and select your subscription. The subscription under which to create this web app.
    Resource group Drop down and select a resource group, or select Create new and enter a new resource group name. By putting all your app resources in one resource group, you can easily manage or delete them together.
    Site Name Enter a name for your web app. This value cannot be empty.
    Hosting plan name Enter a name for the App Service plan to use for hosting the web app. This value cannot be empty.
  2. Select Alerts (bell icon) in the portal, and then select Go to resource group.

    Azure portal deployment notification.

  3. On the Resource group page, in the list of resources, select the web app that you created. You'll also see the App Service plan and the cache instance in this list.

  4. On the App Service page for your web app, select the URL to navigate to the web site. The URL should be in this format: https://<your-site-name>.azurewebsites.net.

  5. Confirm that you see the site but no events have been posted to it yet.

    Empty Event Grid Viewer site.

Enable Event Grid resource provider

If you haven't previously used Event Grid in your Azure subscription, you may need to register the Event Grid resource provider.

In the Azure portal:

  1. Select Subscriptions on the left menu.
  2. Select the subscription you're using for Event Grid.
  3. On the left menu, under Settings, select Resource providers.
  4. Find Microsoft.EventGrid.
  5. If not registered, select Register.

It may take a moment for the registration to finish. Select Refresh to update the status. When Status is Registered, you're ready to continue.

Subscribe to the Azure Cache for Redis instance

In this step, you'll subscribe to a topic to tell Event Grid which events you want to track, and where to send the events.

  1. In the portal, navigate to your cache instance that you created earlier.

  2. On the Azure Cache for Redis page, select Events on the left menu.

  3. Select Web Hook. You are sending events to your viewer app using a web hook for the endpoint.

    Azure portal Events page.

  4. On the Create Event Subscription page, enter the following:

    Setting Suggested value Description
    Name Enter a name for the event subscription. The value must be between 3 and 64 characters long. It can only contain letters, numbers, and dashes.
    Event Types Drop down and select which event type(s) you want to get pushed to your destination. For this quickstart, we'll be scaling our cache instance. Patching, scaling, import and export are the available options.
    Endpoint Type Select Web Hook. Event handler to receive your events.
    Endpoint Click Select an endpoint, and enter the URL of your web app and add api/updates to the home page URL (for example: https://cache.azurewebsites.net/api/updates), and then select Confirm Selection. This is the URL of your web app that you created earlier.
  5. Now, on the Create Event Subscription page, select Create to create the event subscription.

  6. View your web app again, and notice that a subscription validation event has been sent to it. Select the eye icon to expand the event data. Event Grid sends the validation event so the endpoint can verify that it wants to receive event data. The web app includes code to validate the subscription.

    Azure Event Grid Viewer.

Send an event to your endpoint

Now, let's trigger an event to see how Event Grid distributes the message to your endpoint. We'll be scaling your Azure Cache for Redis instance.

  1. In the Azure portal, navigate to your Azure Cache for Redis instance and select Scale on the left menu.

  2. Select the desired pricing tier from the Scale page and click Select.

    You can scale to a different pricing tier with the following restrictions:

    • You can't scale from a higher pricing tier to a lower pricing tier.
      • You can't scale from a Premium cache down to a Standard or a Basic cache.
      • You can't scale from a Standard cache down to a Basic cache.
    • You can scale from a Basic cache to a Standard cache but you can't change the size at the same time. If you need a different size, you can do a subsequent scaling operation to the desired size.
    • You can't scale from a Basic cache directly to a Premium cache. First, scale from Basic to Standard in one scaling operation, and then from Standard to Premium in a subsequent scaling operation.
    • You can't scale from a larger size down to the C0 (250 MB) size.

    While the cache is scaling to the new pricing tier, a Scaling status is displayed in the Azure Cache for Redis blade. When scaling is complete, the status changes from Scaling to Running.

  3. You've triggered the event, and Event Grid sent the message to the endpoint you configured when subscribing. The message is in the JSON format and it contains an array with one or more events. In the following example, the JSON message contains an array with one event. View your web app and notice that a ScalingCompleted event was received.

    Azure Event Grid Viewer scaling in JSON format.

Clean up resources

If you plan to continue working with this event, don't clean up the resources created in this quickstart. Otherwise, delete the resources you created in this quickstart.

Select the resource group, and select Delete resource group.

Next steps

Now that you know how to create custom topics and event subscriptions, learn more about what Event Grid can help you do: