Redis is god

Ioshellboy
4 min readJan 3, 2024

--

Why Redis?

Remote Dictionary Server is the most popular NoSQL database.

Use cases of redis

  • Database
  • Message broker
  • Cache
  • Streaming engine

Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

You can run atomic operations on these types, like appending to a string; incrementing the value in a hash; pushing an element to a list; computing set intersection, union and difference; or getting the member with highest ranking in a sorted set.

To achieve top performance, Redis works with an in-memory dataset. Depending on your use case, Redis can persist your data either by periodically dumping the dataset to disk or by appending each command to a disk-based log. You can also disable persistence if you just need a feature-rich, networked, in-memory cache.

Redis supports asynchronous replication, with fast non-blocking synchronization and auto-reconnection with partial resynchronization on net split.

How redis works and how is data stored?

We all know how an internal hashmap/ dictionary works.

  • Redis follows a client server architecture
  • All data is stored and maintained by redis server
  • Client authenticates and send across commands that would be executed by the server.

Redis commands

  • Get
  • Set
  • Hash
  • List
  • Sorted set

Data persistence

No persistence

No persistence: If you wish, you can disable persistence altogether. This is the fastest way to run Redis and has no durability guarantees.

RDB Files

RDB (Redis Database): The RDB persistence performs point-in-time snapshots of your dataset at specified intervals.

The main downside to this mechanism is that data between snapshots will be lost. In addition, this storage mechanism also relies on forking the main process, and in a larger dataset, this may lead to a momentary delay in serving requests. That being said, RDB files are much faster being loaded in memory than AOF.

AOF

AOF (Append Only File): The AOF persistence logs every write operation the server receives that will be played again at server startup, reconstructing the original dataset.

This way of ensuring persistence is much more durable than RDB snapshots since it is an append-only file. As operations happen, we buffer them to the log, but they aren’t persisted yet. This log consists of the actual commands we ran in order for replay when needed.

Then when possible, we flush it to disk with fsync (when this runs is configurable), it will be persisted. The downside is that the format isn’t compact and uses more disk than RDB files.

Data replication

Since it is a ram based database, we need to horizontally scale when we have a lot of data that can not be kept on a single machine.

Currently, the maximum RAM available in a single server is 24TIB, presently listed online at AWS.

Every Redis Cluster node requires two open TCP connections: a Redis TCP port used to serve clients, e.g., 6379, and second port known as the cluster bus port. By default, the cluster bus port is set by adding 10000 to the data port (e.g., 16379); however, you can override this in the **cluster-port**configuration.

Cluster bus is a node-to-node communication channel that uses a binary protocol, which is more suited to exchanging information between nodes due to little bandwidth and processing time. Nodes use the cluster bus for failure detection, configuration updates, failover authorization, and so forth.

RESP

To communicate with the Redis server, Redis clients use a protocol called REdis Serialization Protocol (RESP)

Where to go from here?

  • Reactor pattern
  • Forking
  • Redis as a Queue
  • REDIS DEEP DIVE
  • CAP Theorem

--

--

No responses yet