How To Use Cache for Re-platform to Azure
This article is contributed. See the original author and article here.
Overview
Moving to Cloud is a crucial part of digital transformation as business grows. As a leading Cloud provider, Azure provides intuitive, guided, and repeatable support for migrating an on-prem application to Azure cloud. You can learn more at Reliable web app pattern – Azure Architecture Center.
To ensure a web application runs performantly and resiliently, you can use Azure Cache for Redis. This blog zooms into caching best practices and guidance in the Reliable Web App Pattern.
Moving In-Memory Cache to Distributed Cache in Azure
Suppose your online order web app saves shopping cart items in a user’s session data in memory. As you migrate a web application from on-prem to Azure Cloud, caching in-memory no longer works because the virtual servers may scale-in or shut down to save cost. In such cases, in-memory data will be gone. Distributed caching persists cached data by decoupling cache from the web server instances to support scalability and resiliency. In addition, if you were self-hosting Redis Cache, re-platform to use Azure Cache for Redis removes operational overhead from self-hosting. Azure resources located within the same data center have negligible network latency, and the in-memory nature of Azure’s Cache service ensures accessing data is fast.
Azure Cache for Redis is the 1st party caching solution in Azure. Developed in partnership with Redis Inc, Azure Cache for Redis provides the unparalleled up to 99.999% SLA with Enterprise SKUs, data replication across geographic locations, and scenario-drive cost effective hosting options to meet your business needs.
Patterns and Best Practices to Use Distributed Caching in Azure
To help customers successfully re-platform from on-prem to Azure, the Reliable web app pattern offers reference architecture scenarios to guide the process. Currently, the Reliable Web App Pattern for .NET and Java are available. Without losing generosity, we will look at the recently announced Java support for how to apply caching when migrating an application.
We recently announced GA for the Java version of Reliable Web App pattern, which is a set of architectural recommendations and guided, repeatable process with Reference Architecture applications to ensure customers can successfully migrate to Azure. You can learn more at Reliable web app pattern for Java – Plan the implementation – Azure Reference Architectures. In the example reference application, an online training web app written in Java Spring Framework is transformed to run on Azure. Figure 1 shows a screenshot of the web application, which is a real-world and production-ready training web application that shows a series of educational tutorials. The playlist of video tutorials might be different for each user and can be cached for quick access and saving a user’s training progress.
Figure 1: Java sample application homepage that shows training video playlist
In the reference architecture caching is applied to multiple scenarios such as logged in user settings, security settings, and displaying the playlist on the homepage as shown in Figure 1. Let’s look at how smooth it is to transition from local cache to Azure with our example.
Per Figure 2, initializing Redis in a Java application only takes a few lines of code, and the configuration is very flexible. You can find it at RedisConfig.java.
Figure 2: Initializing Redis client in a Java Spring web application.
Java Spring Caching abstraction allows tag annotations to be used for indicating if a method’s return value should be cached and how. In Figure 3, the tag annotations hide all the details on interacting with Redis Cache and allows developers to focus on business logic. Referring back to the video playlist shown in Figure 1 above, in its implementation from the PlaylistService.java, the playlist and playlist per user are cached for saving user progress and performantly and query-efficiently loading the result next time.
Figure 3: Java Spring Caching Abstraction for caching method return results.
Deploying the application to use Azure Cache for Redis is straight forward. Just point the app configuration to the right Azure Cache for Redis endpoints to get your app working! Follow the README.md instructions to deploy the app.
Use the Cache-Aside pattern for data consistency
As a user’s playlist is part of the session data and is saved in cache, what if the overall playlist gets updated? How do we make sure information in cache stays consistent with the backend database?
The cache-aside pattern guarantees application data consistency. It would attempt to read from cache every time, upon a cache miss it reads from the database and put the value in cache for the next read, and upon an update in database all related cache records will be cleared to keep data up-to-date. Figure 4 illustrates how the cache-aside pattern keeps data from cache to the backend data store to be consistent. You can learn more at Cache-Aside pattern – Azure Architecture Center.
Figure 4: cache-aside pattern keeps cache and datastore consistent
Referring to our Java sample application, the Spring Caching Abstraction hides the details as this algorithm is a highly repeatable process.
Next steps
You can learn more about the .NET reference application at Introducing the Reliable Web App Pattern for .NET – .NET Blog. Read more about the Reliable Web App Pattern at Reliable web app pattern – Azure Architecture Center.
Recent Comments