Professional Documents
Culture Documents
Speaker
Diwakar Singh
Founder Golang.Expert
Email: diwakar@golang.expert
Linkedin: https://www.linkedin.com/in/diwakar-singh-/
Redis
• Redis is an open source in-memory data structure store, used as a database, cache and
message broker.
• Redis holds its database entirely in the memory, using the disk only for persistence.
Redis
• It supports data structures such as strings, hashes, lists, sets, sorted sets with range
queries, hyperloglogs.
• It is Developer friendly.
• $ cd tmp
• $ wget https://download.redis.io/releases/redis-6.2.6.tar.gz
• $ tar xzf redis-6.2.6.tar.gz
• $ cd redis-6.2.6
• $ sudo make install
• It allows certain connections to be limited in terms of the commands that can be executed
and the keys that can be accessed.
• You can create role based users, so that untrusted clients have no access and trusted
clients have just the minimum access level to the database in order to perform the work.
Redis Persistence
• RDB
• RDB files are perfect for backups. For instance you may want to save your keys every
minute, hour etc.
• RDB is NOT good if you need to minimize the chance of data loss in case Redis stops
working (for example after a power outage)
RDB
• RDB needs to fork() often in order to persist on disk using a child process.
• Fork() can be time consuming if the dataset is big, and may result in Redis to stop serving
clients for some millisecond or even for one second.
AOF
• The AOF persistence logs every write operation received by the server.
• You can configure how many times Redis will fsync data on disk. There are three options:
• appendfsync always
• appendfsync everysec
• appendfsync no
AOF
• appendfsync always: fsync every time new commands are appended to the AOF. Very very
slow, very safe.
• appendfsync everysec: fsync every second and you can lose 1 second of data if there is a
disaster.
• appendfsync no: Never fsync, just put your data in the hands of the Operating System.
This is less safe method.
• Normally Linux will flush data every 30 seconds with this configuration, but it's up to the kernel exact
tuning.
Keys
• A key name in Redis is any binary string and can contain any data up to 512MB in size -
although very long key names are not recommended
• Redis keys are a binary safe, made up of a sequence of bytes. Therefore, any byte value
can be used.
• The string data type is useful for a number of use cases, like caching HTML fragments or
pages.
• Accessing an element by index is very fast in lists implemented with an Array and not so
fast in lists implemented by linked lists.
List
• You can do many interesting things with Redis Lists, for instance, you can:
• Model a timeline in a social network, using LPUSH in order to add new elements in the
user timeline, and using LRANGE in order to retrieve a few of recently inserted items.
• Every time a user posts a new photo, we add its ID into a list with LPUSH.
• When users visit the home page, we use LRANGE 0 9 in order to get the latest 10 posted
items.
Redis Hashes
• Redis Hashes are maps between string fields and string values.
• They are the perfect data type to represent objects (e.g. A User with a number of fields
like name, surname, age).
• Every hash can store up to 232 - 1 field-value pairs (more than 4 billion).
Transaction
• A transaction in Redis consists of a block of commands.
• MULTI, EXEC, DISCARD and WATCH are the foundation of transactions in Redis.
• It can never happen that a request issued by another client is served in the middle of the
execution of a Redis transaction. This guarantees that the commands are executed as a
single isolated operation.
Transaction
• Either all of the commands or none are processed, so a Redis transaction is also atomic.
• So if a client passes invalid command to the server in the context of a transaction then
none of the operations are performed.
Pipelining
• Clients and Servers are connected via a network link. Such a link can be very fast or very
slow (a connection established over the Internet with many hops between the two hosts).
• Whatever the network latency is, it takes time for the packets to travel from the client to
the server, and back from the server to the client to carry the reply.
• If the RTT time is 250 milliseconds (in the case of a very slow link over the Internet), even
if the server is able to process 100k requests per second, we'll be able to process at max
four requests per second.
• Serving each command without pipelines might look very cheap from the point of view of
accessing the data structures and producing the reply.
Pipelining
• But it is very costly from the point of view of doing the socket I/O.
• That means going from user land to kernel land. The context switch is a huge speed
penalty.
• When pipelining is used, many commands are usually read with a single read() system call,
and multiple replies are delivered with a single write() system call.
Sets
• Redis Sets are an unordered collection of Strings.
• O(1) (constant time regardless of the number of elements contained inside the Set).
• Want to know all the unique IP addresses visiting a given blog post? Simply
use SADD every time you process a page view. You are sure repeated IPs will not be
inserted.
• The max number of members in a set is 232 - 1 (4294967295, more than 4 billion of
members per set).
Publish/Subscribe
• It allows for simple message buses to be created.
• This allows Redis to act as a broker for multiple clients providing a simple way to post and
consume messages and events.
• In this section we'll finish up some typical use cases for publish and subscribe.
PUB/SUB Working
• Senders (publishers) are not programmed to send their messages to specific receivers
(subscribers).
• Rather, published messages are characterized into channels, without knowledge of what
(if any) subscribers there may be.
• Subscribers express interest in one or more channels, and only receive messages that are
of interest, without knowledge of what (if any) publishers there are.
Working
Subscribe ch1
Publish “World”
Subscribe ch1 ch2
• Every stream-entry has unique Ids. By default, ids are time prefix.
• Streams can be consumed by and processed by multiple distinct sets of consumers known
as consumer groups.
Common Use Case
• IoT and Artificial Intelligence to user activity monitoring, fraud detection, and FinTech.
• They all collect and process high volumes of data, which arrive at high velocities.
• After processing this data, these technologies then deliver them to all the appropriate
consumers of data.