Retrofit and Volley both are the REST client libraries. Before starting the comparison let me first introduce these libraries: –
Volley-: Volley is a networking library it offers great features like prioritization, ordered requests and of course caching, making multiple requests at the same time, asynchronous requests, synchronous requests;
Retrofit -: Retrofit is a REST client for Android, through which you can make easy to use interfaces which can turn any Android app into a powerful one. Retrofit can perform Async and sync requests with automatic JSON parsing without any effort
When it comes to libraries in Android development, seems like fanboy-ism is alive and well. Jake Wharton and Square Inc. libraries loom large! This should not be surprisingly to any Android developer. As a matter of fact, if you look at the GitHub awards for Java, it becomes clear that Square and Jake Wharton have arguably done at least as much for Android development as Google, if not more!
Volley or Retrofit?
I won’t lie to you, I prefer Volley over Retrofit. I do however have my reasons for this preference; I know the code for Volley well, it suits my coding/project structure style and you get image handling and lots of other things for free. Retrofit on the other hand (feel free to correct me on this) is more about code abstraction on top of a HTTP client which for Retrofit is OkHttp. Retrofit aims to make it easier to consume RESTful web services where as the goal of Volley is to handle all your networking needs for Android specifically.
Should you then use Volley or Retrofit? I really tried hard to come up with a rule of thumb, but it’s not easy. For one thing, Volley can run of legacy Apache, HttpUrlConnection, Apache-4 or OkHttp. Where are Retrofit really only runs of OkHttp. Right there it becomes clear that comparing Volley and Retrofit is probably not a good idea. In recent months, I’ve been concentrating on more reusable code and I’ve found Retrofit to be a better model to work with. Don’t be mistaken, Volley and Retrofit are 2 very different beasts and comparing them really isn’t fair. What I can say is this:
- Retrofit is a lot easier to configure. With Volley, if the default HurlStack isn’t enough for you, good luck! Most average developers will struggle to configure Volley, luckily there is a lot of code they can copy paste but even then, they may not know what they are doing.
- Volley will handle avoiding duplicate calls, not sure if Retrofit will.
- Volley provides network image handling support.
- Volley can integrate with most popular HTTP clients out there that include OkHttp where as Retrofit (as far as I know) relies on OkHttp.
- Retrofit remains up to date. Volley may be relying on a library bundled with your OS, hence updating the network client isn’t an option.
- Retrofit makes it much easier to configure HTTP intercepts (if you want to do something before or after an HTTP call).
- Volley can provide fine grain control over caching strategy, but its harder to configure caching than Retrofit.
- Retrofit relies on OkHttp, which relies on Okio which effectively makes this library huge compared to a basic configuration of Volley. If size is an issue, Volley is a better bet.
The list goes on.
My recommendation is that if you app has very simple networking requirements, use ION it handle network calls, images and it’s very simply to include in your project. If you app requires consumption of RESTful services and you don’t want to get hung up in Volley configurations, go with Retrofit. If you need to abstract your code, go with Retrofit. As you probably realise by now, Volley really isn’t winning here. My “rule of thumb” (which I just came up with) makes it seems like Retrofit is the winner in most scenarios. However, if you code is likely to make a lot of duplicate calls and supports screen orientation change and you don’t mind the complexity of configuring Volley, I would recommend going with Volley. Personally, if you have the time, try them both out and find out what suits you more.
Now on to the matter at hand… which is faster?
Methodology
- Since this is about measuring the “speed” of either library. I only measure the time is takes for an asynchronous call to return the raw response.
- I selected 8 RSS feeds from a local news site measure the average amount of time it took for each library to fetch all 8 RSS feeds from a clean install. In order to account of network latency and other real world factors, I ran each tests 10 times and averaged the results.
- I measured each library twice to account for any network issues that may have been taking place the first time I ran the 10 tests.
- All tests were run on the Android Studio 2.0 x86 emulator running API 23 on a Nexus 5.
- For Volley, the tests were run using OkHttp 3, Apache 4.4.12 and Android platform API (for API 9+ this is URLConnection, for older versions it may be Apache HttpClient 2.0+)
- POST was testing by creating a stubbed service on my localhost. Each call time was averaged.
The result
- Retrofit took on average 41ms to fetch the 8 API responses.
- Retrofit cache responses took an average of 21ms.
- Retrofit POST with 3 fields took 14ms on average.
- Volley-native took an average of 50ms.
- Volley-native cache responses took an average of 12ms.
- Volley-native POST with 3 fields took 18ms on average.
- Volley-Apache4 took an average of 48ms.
- Volley-Apache4 cache responses took an average of 13ms.
- Volley-Apache4 POST with 3 fields took 15ms on average.
- Volley-OkHttp3 took an average of 40ms.
- Volley-OkHttp3 cache responses took an average of 14ms.
- Volley-OkHttp3 POST with 3 fields took 15ms on average.
Looks like there is some truth to the fact that Retrofit is faster! Though Volley with OkHttp3 performed just as well.
Notes:
- Retrofit cache responses are a lot slower though presumably because by default Retrofit/OkHttp uses disk cache where as Volley probably uses memory cache alongside disk cache.
- It’s worth pointing out that Volley’s average response times for GET varied greatly compared to Retrofit, some times completing all 8 request with an average time of ~200ms. I’m not sure how to account for this. When I measure each responses individually, some responses would be faster for Volley; others faster for Retrofit but on cumulative average, Retrofit would be faster. Retrofit’s responses were far more consistent, varying only by a max of 10 milliseconds at worst.
- I wondered of the RSS feed supported SPDY which would account for OkHttp performing better, however as best I can tell, this is not the case.
- Retrofits performance advantage all but disappeared when using OkHttp with Volley. Volleys faster memory cache probably gives it the upper-hand here.
- Who is noticing a 10 millisecond difference in the cumulative average of API calls anyways? I can understand that there may be some-very-select apps where this 10 millisecond difference is a big deal, but it’s not even one refresh frame!
- POST responses varied greatly for everyone (±5ms variance) Volley just tended towards higher numbers. Again, not sure what can explain the variance, I tried to control the environment as strictly as was reasonable.
- Forking and customising Volley for your needs is a lot easier than doing so with Retrofit.
- You probably should use Glide for image handling, this effectively negates any advantage Volley has in this area.