Skip to main content
QPSOFTWARE

Redis for Mobile Apps: How a Cache Makes Your App Run Faster

Updated on |By QPSoftware

Redis for mobile apps works as a cache between your app’s API and its database. Redis keeps data in memory (RAM), so it answers repeated requests much faster than a database reading from disk. Your iOS or Android app keeps calling the same API: the server simply returns cached results instead of running the same query again.

With faster operations, your entire business generates more revenue and less friction. For every application you use in your company, improving the response time of search results or database queries impacts all your services: sales and business development, human resources, customer relationships, production or design.

Business people applauding around a meeting table with a laptop and a glass of water

Below, we introduce Redis and show how we implement it to improve an application’s API performance, with a demo we built to try it out. Response time matters in iOS and Android app development: an app feels only as fast as the API behind it.

What Is Redis?

Redis is an in-memory database. It relies on a computer’s RAM instead of disk drives, which gives a faster response time than a physical drive, because reading data from memory is much faster. Redis is an in-memory data store to ingest, process and analyze real-time data with sub-millisecond latency. It is an ideal choice for search, ad targeting, personalization, IoT and more. It is language agnostic: you can use any programming language you specialize in.

The Redis logo: a red stack of cubes next to the word redis

The performance gain from Redis is noticeable when your system has a large dataset. It helps an application scale as it gets more and more users, with more and more data requested and returned. It is also a way to avoid database bottlenecks: Redis caches the results of complex, time-consuming database queries to return them faster on future requests.

Who Uses Redis?

Redis’s official website presents Redis as an in-memory database for caching, vector search and NoSQL workloads, and publishes customer stories from companies such as Ulta Beauty and Sony LIV. When this article was first written in 2022, many well-known companies had also used Redis in their production environments, such as Instagram, GitHub, Airbnb, Twitter and Stack Overflow.

Logos of Twitter, Instagram, Spotify, GitHub, Instacart, Airbnb, Alibaba Group and Uber, with the words and much more

Redis launched in 2009, and over the years it has proven fast, reliable and efficient in a wide range of projects and companies. That is why you should give it a try too.

What Is Redis Used For?

As mentioned above, Redis fits many kinds of use cases, thanks to a wide variety of data structures that can meet your application’s requirements.

Redis can cache data, build chat and messaging, maintain queues, create real-time leaderboards, store sessions, stream rich media, cache geospatial data, and serve machine learning and real-time analytics.

Our Demo: Talentiva, a Search Without a Cache

We built a demo to show the Redis caching feature, which we use to improve our API response.

Contact us to try our demo.

Talentiva is a talent database that contains many personal profiles for professional career searches. We added profile attributes such as country of birth, current country, skills and languages to add complexity. Users can search on different criteria and get different results based on their requests.

Talentiva by QPSoftware search page: filters by country and skill, five talent profiles and a search time of 0.09347 seconds

We built it with:

  • Next.js (server-rendered React framework);
  • Express.js (defines and maintains the business logic, then serves an API);
  • MongoDB (the database that stores all data).

Without Redis, the flow looks like this:

Diagram of a search without Redis: the user, a Next.js and Express.js server, and MongoDB processing every query

  1. The user sends a request to the server with the search field of the search page.
  2. The back end queries the database with the submitted criteria.
  3. A MongoDB aggregation operation processes a query over multiple documents or collections and returns computed results.
  4. The server sends the result back to the client.

The problem is that the client queries the database every single time.

If different users send the same request many times, the database must process it and return the same data again. That is not a cost-effective solution, and with a large dataset, it slows the whole process down.

How a Redis Cache Speeds Up Your App

Now it’s time to use the Redis cache to improve the process. We installed a Redis instance, then wrote a function in the server-side application that checks whether a query has been requested before and whether its result is already in the Redis cache.

Diagram of a Redis cache hit: the Next.js server finds the data in the Redis cache and answers without querying MongoDB

If the data is in the Redis cache (a cache hit), the server returns it to the client directly. This saves time, since getting data from Redis takes much less time.

Diagram of a Redis cache miss: the server queries MongoDB, saves the result in the Redis cache and returns it to the user

If the data is not in the Redis cache (a cache miss), the server queries the database, saves the result in the Redis cache and responds to the client.

In our demo, when you search twice with the same criteria, Redis gives a much shorter response time the second time: 0.00056 seconds from the Redis cache, against 0.09586 seconds from the database.

Talentiva search timed twice: 0.09586 seconds from the database and 0.00056 seconds from the Redis cache, on a bar chart

As the image and the chart show, the response from the Redis cache is much faster than the response from the database. Timing responses before and after a change like this is also how performance is checked when you test your app for the Chinese market.

What Should You Do Next?

Redis can cache data to avoid running the same queries on the database while the dataset and its results do not change. This saves unneeded database requests. Caching is not the only Redis solution to improve application performance either: Redis offers many others, depending on the problem you want to solve.

Things get more complex when the database and the Redis cache must stay consistent. Invalidating the cache when the dataset is updated can be challenging to implement, and problematic if done incorrectly. How long it takes to add Redis to an application depends on that application, and some areas require developers with experience and knowledge of the subject.

There is no single solution that solves every problem, and that is also true in software development. If you have a business problem, or an idea you want technology to make real, you are always welcome to consult QPSoftware. We are a digital agency with more than 15 years of experience, and our team can help you speed up an existing application or plan your mobile app development in Shanghai. Tell us about your project, consult us about your problem, or request a quote, free of charge.

Frequently Asked Questions

Why is Redis so fast?

Redis is fast because it keeps data in memory. It relies on RAM instead of disk drives, and reading data from memory is much faster than reading it from a physical drive. Redis is built to ingest, process and analyze real-time data with sub-millisecond latency. In our Talentiva demo, a repeated search took 0.00056 seconds from the Redis cache, against 0.09586 seconds from the database.

Do I need to change my server infrastructure to use Redis?

No. You can use Redis with your current server and your web or mobile app. In our demo, we installed a Redis instance next to the existing Next.js, Express.js and MongoDB stack, then added a function in the server-side application that checks the cache before querying the database. The app itself keeps calling the same API.

Is Redis better than Memcached?

For most applications, we suggest Redis. Memcached is made for simplicity, while Redis offers a much wider range of features: besides caching data, it can store sessions, maintain queues, power chat and messaging, create real-time leaderboards and serve real-time analytics. That makes Redis the more powerful option when your application needs more than a simple cache.

Submit your project

Get a free quote

Request a quote