V1

14. Patterns: Cached Result

The cached result pattern is used to alleviate load on an underlying system by returning a result that is stored in Flowgear for a specific request. Typical reasons to use this pattern include:

  • A large number of similar requests come in over a short period of time.
  • Querying the underlying service is slow.
  • Querying the underlying service is expensive in some way. For example, it might cause a lot of memory to be allocated or a lot of CPU to be used.

Under the pattern, the Workflow looks up the parameters for the request to see if there is a recent result for those parameters. If one is found, that stored result is returned instead of querying the underlying service. If no match is found, the underlying service is invoked and the result is stored (cached), so that it can be returned the next time the same request is received.

Exercise 19: Cached Result

In this exercise, we'll revisit the weather service we used in an earlier exercise but this time, we'll use caching to return the temperature of a city when it has been previously requested.

  1. Add Web Request 2, and rename it to Service. Connect Start.RunNow → Service.

  2. Set Service.Url to http://api.openweathermap.org/data/2.5/weather?q={city}&APPID={appid}&units=metric.

  3. Add Custom Properties on Service named city and appid.

  4. Add Variable Bar, and rename it to Inputs.

  5. Add Properties on Inputs named city and appid as outputs. Set appid to be a Hidden Property.

  6. Set Inputs.appid to c95dadb707ef003ac19b0236e63e348a.

  7. Set Inputs.city to a city name, e.g. atlanta.

  8. Connect Inputs.city → Service.city and Inputs.appid → Service.appid.

    Run the Workflow to check that it is executing correctly.

  9. Add Variable Bar to the right of Service, and rename it to Outputs.

  10. Add a Property on Outputs named temperature.

  11. Connect Service.ResponseBody → Outputs.temperature, and set the Data Mapping Expression to main.temp.

    Next, we will add a Key/Value to store the temperature of a city that has been queried.

  12. Move Outputs one block to the right. Add Set Key-Value 2, and rename it to Store Temp. Connect Service → Store Temp.

  13. Set Store Temp.Group to temperature.

  14. Connect Inputs.city → Store Temp.Key.

  15. Connect Service.ResponseBody → Store Temp.Value, and set the Data Mapping Expression to main.temp.

    Run the Workflow and confirm the Set Key/Value step is storing the temperature correctly.

    Now, we will introduce a test to determine whether the temperature for a city is already in the Key/Value store, which is acting as our cache.

  16. Move Service three blocks to the right. In the space created: add Get Key-Value 2, and rename to Query Temp; Add DateTime Compare, and rename to Compare; Add If.

  17. Reconnect Start.RunNow → Query Temp, Query Temp → Compare, Compare → If, and If.False → Service.

  18. Connect Query Temp.Missing → Service.

  19. Set Query Temp.MatchGroup to temperature.

  20. Connect Inputs.city → Query Temp.Key.

  21. Connect Query Temp.DateTime → Compare.DateTime1.

  22. Set Compare.TimeSpanUnit to Seconds.

    The DateTime Compare Node returns the difference between two dates and times. If one of the DateTime Properties is left empty, it defaults to the current UTC time for the comparison.

  23. Connect Compare.TimeSpan → If.Value.

  24. Set If.Expression to Value < 60.

    We are setting 60 seconds as the cache expiry period. This means that we will serve a temperature for a city from cache until that record is 60 seconds old.

  25. Connect Query Temp.Value → Outputs.temperature.

    Run the Workflow to see it re-cache the temperature for Atlanta. Then, run it again and it should return the cached temperature. Wait 60 seconds to see the cache invalidate and query the weather service again.

    Note that we don't need to connect any further steps to the If Node True Output, because we already injected the temperature to Outputs.temperature from Query Temp.Value.

    Notice also that the Workflow runs very quickly when operating through the cache since the "expensive" step of invoking the weather service is skipped.

Save and run your Workflow, and then click Submit Exercise to grade it.