To build a rest point application, there are many options available like Akka Http, Finagle, Play Framework, Lagom etc. In our project, we are using Akka HTTP for building rest applications. We have lots of requests related to analytics. There are many requests which are very expensive. So, we faced an issue where our application was taking time to respond to some analytics. We have to find a way to make it fast and avoid expensive computations. We know that Caching is a very important part of any real-time application. Fortunately, Akka Http gives support for caching. It is very easy to enable caching in Akka HTTP.
Akka Http Caching is a lightweight and fast in-memory caching based on Futures. Basically, we wrap the expensive computation in a layer of the cache using a certain type of Key K. Key K is used to get that value of K from the cache.
It implements the cache API which is built on Java 8 library Caffeine. Caffeine provides an in-memory cache using a Google Guava inspired API.
Let get started with the implementation. To be able to use caching, we have to add the Akka HTTP cache dependency to our project.
libraryDependencies += "com.typesafe.akka" %% "akka-http-caching" % "10.1.7"
Since we apply cache those routes which are expensive in terms of computation. For simplicity, I am trying to get factorial of a very large number i.e 200000 as it takes more than around 10 seconds. It fits best to understand the caching purpose.
Here is simple factorial implementation using tail recusion. We have wrapped the result in future as cache takes a key value and returns future value.
Here we first check whether the cache has value for a given number. If it has value it returns the result without calling the method. But if it does not find the key, it loads the key in the cache and call the method factorial and also stores the result in cache.
We have seen how we can use caching in our application. But how does it work?
Cache works on the basis of Least Frequently Used principle. It has a threshold value i.e maximul capacity; of keys to store in it. When the cache is full, it evicts the keys which are less used recently. It also provides options to evicts least frequently used keys on the basis of time. By setting time period for timeToLive and timeToIdle configurations. By default, their time is inifity duration.
Besides that, it also stores the future result of value T instead of actual value T. The idea of storing the future value of Type T instead of the actual value of Type T that If there are many requests for same key K. If one future of type T will be completed for keys K, all requests will be completed automatically . Hence, it reduces server load and processing time for all requests. We can also easily configure our default settings of cache as follow.
Here cache stores maximum keys up to 1024 entries. After that, it evicts keys which are less likely used. We can pick values from application.conf.
Akka Http Cache boosts all expensive routes and makes our application more responsive. It is easy to use and learn. There are other ways also to use cache i.e cacheDirectives. We will explore those in future blogs. For complete example refer
Hope this blog will help you.