Redis Overview

Overview

Redis stands for Remote Dictionary Server. It is an open source, non-relational database management system for storing and managing unstructured data in a key-value structure. It was first developed by Salvatore Sanfilippo in 2009. Since 2015, it has been supported by Redis Labs. Redis is used freely in many services.

Pareto principle

The Pareto principle says that 80% of the phenomena that occur in society are caused by 20% of the causes. This also fits website access patterns.

It is estimated that 80% of Internet traffic is access to only 20% of sites, and caching the data from these 20% of websites can dramatically improve efficiency.

Textbook for Infrastructure Engineers - Network Edition, 2017, Gilbut

Therefore, storing commonly used data in Redis as a cache can be an efficient way to use resources.

Redis features

  • Redis is a key-value store NoSQL DB.
    • Supports a key-value structure for simple strings
    • Supports writing binary data
  • It operates as a single thread.
    • It guarantees operation atomicity by operating as a single thread.
  • It supports various data structures.
    • Collection support: List, Set, Sorted Set, Hash, and other data structures
  • High speed
    • It provides performance around 100,000 QPS.
  • Disk storage
    • Data can be stored persistently on disk.
  • Replication
    • Supports a master/slave structure that can replicate data to other nodes.
  • Sharding support
    • Supports data sharding through Redis Cluster.
  • Publish/Subscribe support
    • Can be used as a message broker
  • Expire time
    • You can set an expiration time on a key so old data is automatically deleted.
  • Recovery is possible through data snapshots or AOF logs, so some persistence is guaranteed.
  • In Java Spring, it is often used for session management and caching.

Why are collections important?

Redis is an in-memory database. That means all data is stored and queried in memory. It is much faster than traditional relational databases such as Oracle and MySQL because memory access is faster than disk access. However, speed is only one of Redis’s characteristics. The biggest difference from other in-memory databases such as Memcached is that it supports various data structures. Redis stores various data structures in key-value form as shown below.

Redis basically provides String, Bitmap, Hash, List, Set, and Sorted Set, and as versions have advanced, it now also supports types such as Geospatial Index, HyperLogLog, and Stream.

Why is providing many data structures important? The reason is development convenience and difficulty.

For example, if you implement a real-time ranking server with an RDBMS, you need to store data in the DB, sort it by the stored SCORE value, and read it again. As the number of records increases, this process becomes slower because it uses disk. You could write code directly to process data on the server with an in-memory approach, but using Redis Sorted Set is faster and simpler.

Redis persistence

Redis uses RDB (Snapshot) and AOF (Append Only File) methods to store data on disk.
Both methods internally use the fork() function, so sufficient memory is required.

  • RDB (Snapshot)

    • A method of moving data in memory to disk.
    • Because snapshots capture the memory state as it is, they are useful for backup and recovery at a specific point in time, and they can load data into memory faster than AOF.
    • If the server goes down, data changed between backed-up snapshots is lost.
    • There are two methods.
      • SAVE: a blocking method that stops all Redis operations sequentially and stores the snapshot at that time on disk.
      • BGSAVE: a non-blocking method that stores the memory snapshot at the time of execution on disk through a separate process.
  • AOF (Append Only File)

    • A method of recording all Redis write/update operations as a log file.
    • When the server starts, operations are replayed sequentially to recover data.
    • Because it records every operation as it runs, logs can be kept up to the current point.
    • Because it only appends to the log file, write speed is fast and data loss does not occur even if the server goes down.
    • Because logs are recorded for all operations, the amount of data is very large.
    • When the server restarts, all operations must be executed again, so restart speed is slow.

Using Redis in the cloud

If you use Amazon Web Services or Microsoft Azure for application development, each cloud service provides a Redis service, so you can use it.

Cloud service Service name
Amazon Web Services Amazon ElastiCache for Redis
Microsoft Azure Azure Redis Cache

Of course, you can also install Redis directly on each cloud service’s compute instances, but considering management cost, choosing these services is recommended.

References