Beginners guide to REDIS

aditya goel
6 min readJun 4, 2022

Welcome to this blog. We shall be looking at following topics in this blog :-

  • Fundamentals of Redis.
  • Multi-threading Redis I/O with Redis 6.0 onwards.
  • Connection-Pooling with Redis.
  • Pipelining with Redis.

Question :- Which language is Redis written into ?

Answer :- Redis is an open source data-structure server written in NCC, which makes it friendly with Unix-based systems like Mac or Linux without any other dependencies. But for Windows, it is a different story, because we need a special port of Redis.

Question :- What’s the primary purpose of using Redis ? Quote an example.

Answer :-

  • We can store multiple data types like strings, hashes, and streams, and access them by a unique key name.
  • For example, if you have a string value “Hello World” saved under the key name “greeting,” you can access it by running the GET command followed by the key name “greeting.”

Question :- Are there any keySpaces or schemas in Redis ?

Answer :-

  • All keys in the Redis database are stored in a flat key space.
  • There is no enforced schema or naming policy, and the responsibility for organising the key space is left to the developer.

Question :- Can you now introduce Redis in layman’s language ?

Answer:- Redis is an open-source, in-memory data structure store which supports saving/retrieving variety of data, through Key-Value Pairs. Redis also has high availability, replication, and automatic-partitioning.

Question:- What are Keys and Values in Redis ?

Answer:-

  • Keys in Redis are pretty much like keys that you see in any other language i.e. objects with a key-value pair approach.
  • The key is the property name, and then you follow with a value.

Question :- Which data-structures are supported easily for the Values ?

Answer:- Variety of data-structures are supported, namely :-

  • Strings → Setting value as a string. In above example, we have set the value as a String only.
  • Lists → Lists are exactly what the name implies: a list of strings.
  • Hashes → Hashes are similar to objects where you can set a specific object and then add field-value pair. For example, you can add a user 456 and inside of that user, add first name field with value many.
  • Sets → Sets are basically a list of unordered and unique strings. So basically if you try to add the same value twice, say a Unix user, you won’t be able to add it more than once.
  • Sorted-sets → Sorted sets are exactly the same as sets, except the fact they are ordered by user-defined key for the values. For example, a sorted set could be strings of historical events sorted by the year associated with each event.
  • Bitmaps
  • Hyperloglogs
  • Geospatial-Indexes.

Question :- Why Redis is so popular about the Speed ?

Answer :- The speed Redis is famous for is mostly due to the fact that :-

  • Redis stores and serves data entirely from RAM-Memory instead of disk, as most other databases do.
  • Another contributing factor is the predominantly single-threaded nature. Single threading avoids race conditions and CPU-heavy context switching associated with threads.

Question :- Can Redis NOT take advantage of multiple CPU-cores ?

Answer :- Indeed, this means that open source Redis can’t take advantage of the processing power of multiple CPU cores, although CPU is rarely the bottleneck with Redis. We are more likely to bump up against memory or network limitations before hitting any CPU limitations.

Question :- What happens behind the scenes with every Redis request ?

Answer:- When a client sends a request to a Redis server :-

  • The request is first read from the socket.
  • Then request is parsed and processed.
  • And finally, the response is written back to the socket and sent to the user.

Note that, the reading and especially writing to a socket are expensive operations.

Question :- Has there any optimisations not been done to parallelise these expensive operations (i.e. Read & Write into Socket) ?

Answer:- Yes, In Redis version 6.0, multi-threaded I/O was introduced. When this feature is enabled, Redis can delegate the time spent reading and writing to I/O sockets over to other threads, freeing up cycles for storing and retrieving data, and boosting overall performance by up to a factor of two for some workloads.

Question :- Can you talk something about the Redis Client-Server architecture ?

Answer:- Redis has a client-server architecture and uses a request-response model.

  • Applications send requests to the Redis server,
  • Server processes them and returns responses for each.

Question :- What’s the role of a Redis client library ?

Answer:- Redis Client Library acts as an intermediary between our application and the Redis server. Client libraries perform the following duties :-

  • Implement the Redis wire protocol, the format used to send requests and receive responses from the Redis server.
  • Provide an idiomatic API for using Redis commands from a particular programming language.
  • Managing the connections to Redis.

Question :- How does Redis Clients communicates with Redis-Server ?

Answer:- Redis clients communicate with the Redis server over TCP using a protocol called RESP, Redis Serialisation Protocol, designed specifically for Redis.

  • The RESP protocol is simple and text based, so it is easily read by humans as well as machines.
  • This simple, well-documented protocol has resulted in Redis clients for almost every language we can think of. The Redis.io client page lists over 200 client libraries for more than 50 programming languages.

Question :- What’s the problem with creating new connections each time ?

Answer:- Creating and recreating new connections over and over again, creates a lot of unnecessary load on the server. Therefore, a good client library will offer some way of optimising connection management, by setting up a connection pool, for example.

Question :- How does Connection-Pooling helps ?

Answer:- With connection pooling, the client library will instantiate a series of (persistent) connections to the Redis server and keep them open.

  • When the application needs to send a request, the current thread will get one of these connections from the pool, use it, and return it when done.
  • So, if possible, always try to choose a client library, that supports pooling connections, because that decision alone can have a huge influence on your system’s performance.

Question :- What’s the usual process of interacting with Redis ?

Answer:- Each client does a (typically blocking) read on a socket and waits for the server response.

  • The server reads the request from the socket, parses it, processes it, and writes the response to the socket.
  • The time the data packets take to travel from the client to the server, and then back again, is called network round trip time, or RTT.

Now, say for example, you needed to execute 50 commands, you would have to send a request and wait for the response 50 times, paying the RTT cost every single time.

Question :- Can we optimise the aforementioned approach ?

Answer:- To tackle this problem, Redis can process new requests even if the client hasn’t already read the old responses. This way, you can send multiple commands to the server without waiting for the replies at all; the replies are read in the end, in a single step. This technique is called pipelining and is another good way to improve the performance of your system. Most Redis libraries support this technique out of the box.

Question :- What’s the main advantage of Pipelining ?

Answer:- Pipelining reduces the network round trip time by sending multiple commands to the server in a single round trip.

References :-

--

--

aditya goel

Software Engineer for Big Data distributed systems