Redis is a key-value store that stands out from others, like memcached, in that it has built-in support for data structures like lists, sets, and hashes, and that it can persist data to disk. As such, it is quite useful as both a cache store and as a full-fledged, NoSQL data store.

Uses of Redis include

Installation and uses


you need first to install redis database

apt install redis-server

then you can add the client to your rails app
gem 'redis-rails'

-in config/environments/production.rb, add
config.cache_store = :redis_store, "redis://localhost:6379/0/cache", { expires_in: 90.minutes }

-for session storage, add in config/initializers/session_store.rb
MyApplication::Application.config.session_store :redis_store,
  servers: ["redis://localhost:6379/0/session"],
  expire_after: 90.minutes,
  key: "_#{Rails.application.class.parent_name.downcase}_session",
  threadsafe: false

-for http caching, in config/environments/production.rb , add
# config/environments/production.rb
config.action_dispatch.rack_cache = {
  metastore: "redis://localhost:6379/1/metastore",
  entitystore: "redis://localhost:6379/1/entitystore"
}

Redis Advantages

There are a lot of advantages of using Redis in your application. I've listed them below.

  1. Redis allows storing key and value pairs as large as 512 MB. You can have huge keys and values of objects as big as 512 MB, which means that Redis will support up to 1GB of data for a single entry.

  2. Redis uses its own hashing mechanism called Redis Hashing. Redis stores data in the form of a key and a map, i.e. string fields and string values. For example, the following code uses Redis hash to save user details:

    Map<String, String> user = new HashMap<>();
     user.put("username", "john123");
     user.put("firstName", "John");
     // use Jedis client
     jedis.hmset("user:john123", user);
  3. Redis offers data replication. Replication is the process of setting up master-slave cache nodes. The slave nodes always listen to the master node, which means that when the master node is updated, slaves will automatically be updated, as well. Redis can also update slaves asynchronously.

  4. The Redis cache can withstand failures and provide uninterrupted serviceSince Redis can be used to set up efficient replication, at any point in time, the cache service will be up-and-running — even if any of the slave nodes are down. However, the nodes are resilient and will overcome the failure and continue providing service.

  5. Redis has clients in all the popular programming languages. Redis has client APIs developed in all the popular languages such as C, Ruby, Java, JavaScript, and Python. A full list of languages that Redis supports can be found on the Redis Wikipedia page.

  6. Redis offers a pub/sub messaging systemYou can develop a high-performing messaging application using the Redis pub/sub mechanism using any language of your choice.

  7. Redis allows inserting huge amounts of data into its cache very easilySometimes, it is required to load millions of pieces of data into the cache within a short period of time. This can be done easily using mass insertion, a feature supported by Redis.

  8. Redis can be installed in Raspberry Pi and ARM devicesRedis has a small memory footprint and it can be installed in Raspberry Pi to enable IoT-based applications.

  9. Redis protocol makes it simple to implement a client. A Redis client communicates with its server using RESP (Redis Serialization Protocol). This protocol is simple to implement and is human-readable.

  10. Redis support transactionsRedis supports transactions, which means that commands can be executed as a queue instead of executing one at a time. Typically, commands after MULTI will be added to a queue and once EXEC is issued, all the commands saved in the queue will be executed at once.