What is Redis?
The definition for Redis given in the official Redis website is:
Redis is a open source(BSD licensed), in memory data structure store used as a database, cache and message broker.
An in-memory database is a data store which resides in the main memory. It means once the system is restarted all the data gets lost. So it can’t be used as a permanent database like Oracle database or MongoDB. In the backend of your application Redis can be used to cache data and speed up data query. Redis is one of the fastest NoSQL databases. In Redis data is saved as key-value pairs.
Some of you might be asking, what is cache?
Cache is a small but very fast memory store.
When the backend code generates a query two things are possible.
CASE 1: A copy of the queried data is available in the Redis cache(called a cache hit). Redis sends the data to the application(backend) and the query is not sent to the database. This speeds up the query response as Redis is a lot faster than conventional databases.
CASE 2: The queried data is not present in the Redis cache(called a cache miss), so the query is sent to the database which processes the query and sends back the data. Redis cache saves a copy of the data and passes on the data. Next time the same query is made the data will be present in the cache (case 1).
What if the cache memory gets full?
One of the ways in to add an expiration time to data in the memory.
These are the eviction policies followed by Redis Cloud in case all the memory gets full.
- no-eviction: no keys are evicted and an error is returned if memory gets full and attempt is made to insert more data
- allkeys-lru: the least recently used key is evicted out of all keys
- allkeys-lfu: the least frequently used key is evicted out of all keys
- allkeys-random: a key is evicted randomly of all keys
- volatile-lru: least recently used key is evicted which has an “expire” field set
- volatile-lfu: least frequently used key is evicted which has an “expire” field set
- volatile-ttl: the key with shortest time-to-live and least recently used keys out of keys with an “expire” field set in evicted
- volatile-random: a key with “expire” field set in randomly evicted out
Where is Redis is being used?
Some of the well-known companies using Redis are StackOverflow, Twitter, Github, Instagram, Pinterest, Snapchat. Here is a list of many other companies that used Redis