Posts

Showing posts with the label Redis

[Redis] Publisher/Subscriber

1) What is Publish-Subscribe (pub-sub)? publish-subscribe(pub-sub) is a messaging pattern where senders of messages, called publishers, do not send the message directly to the receivers, called subscribers, but not instead categorize publishes messages into classes without knowledge of which subscribers. 2) Implementation On 2 separate clients, subscribe to a key using "SUBSCRIBE <key> <..key(s)> On a third client, use the key used in step 1 and enter "PUBLISH <key above> <message>" You should then see the message show up in the two redis clients that subscribed to the key. 3) Unsubscribe The opposite of subscribing. 4) Pattern matching pub/unsub Use "PSUBSCRIBE <regex>" or "PUNSUBSCRIBE <regex>" Resource https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern https://redis.io/topics/pubsub

[Redis] Transactions

1) Overview Redis transactions allow a group of commands to be executed sequentially; avoiding the problem of race conditions. It is not possible that a request issued by another client is executed in the middle of the execution of a Redis transaction. Additionally, Redis transactions are atomic, meaning either all of the commands or none are processed. 2) Implementation A transaction follows the format of: multi <your command(s)> exec Any commands you enter between multi--exec are queued.  2.1) Example 127.0.0.1:6379> multi 127.0.0.1:6379> set num 10 127.0.0.1:6379> incrby num 10 127.0.0.1:6379> exec (integer) 20 2.2) Discard Example If you want to cancel your transaction while using the redis-cli, you can just enter discard. 127.0.0.1:6379> multi 127.0.0.1:6379> set num 10 127.0.0.1:6379> incrby num 10 127.0.0.1:6379> discard 3) Errors inside a transaction There are two types of error that can happen d...

[Redis] Redis Cluster vs Redis Sentinel

1) In-Depth Guides to Redis Cluster and Sentinel For more in-depth explanation for Redis Cluster or Redis Sentinel, please read my other posts: Sentinel:  https://notafraidofwong.blogspot.com/2019/05/redis-redis-sentinel.html Cluster:  https://notafraidofwong.blogspot.com/2019/05/redis-redis-cluster.html 2) Deciding between Redis Sentinel and Redis Cluster Similarities : Both solutions provide high availability for your system. 2.1) Redis Cluster 2.1.1) Pros Shards data across multiple nodes Has replication support Has built-in failover of master 2.1.2) Cons Not every library supports it May not be as robust (yet) as Standalone Redis or Sentinel Setup and maintenance is more complicated 2.2) Redis Sentinel 2.2.1) Pros Automatically selects new master in case of failure Easy to setup, (seems) easy to maintain 2.2.2) Cons No data sharding; master might be overutilized. Another distributed system to maintain. 3) So how do I dec...

[Redis] Redis Cluster

Image
1) Overview Provides better Scalability and Load Balancing - Redis Cluster allows your Redis data to be automatically sharded across multiple Redis nodes. High Availability - Cluster provides ability to continue operations when a subset of the nodes experience failures or unable to communicate with the rest of the cluster; however, large scale failures may stop the operation. 2) Data Sharding Data Sharding is a method to break up a big database into smaller parts. The reason for data sharding is that, after a certain scale point, it is cheaper and more practical to scale horizontally (by adding more machines) than to grow it vertically (by adding buffier servers or adding more CPU/ram to servers). 3) How it works? 3.1) Redis Cluster TCP ports Unlike sentinel, there is no dedicated monitoring. Instead, every cluster node have 2 TCP connections open. The first one is a standard redis TCP used to serve clients. Another is a cluster bus port (n...

[Redis] Redis Sentinel

1) Overview A system designed to help manage Redis instance. Primary purpose is to provide high availability system, by monitoring, notifying and providing instances failover. Does this by monitoring master and slave nodes. When master node is down, sentinels will coordinate to promote a slave node into master. 2)   4 main tasks 2.1) Monitoring Check if your master and slaves instances are working as expected 2.2) Notification Notify other program or system administrator via an API, when something goes wrong with the monitored instances 2.3) Automatic Failover On master failure, sentinel promotes one of the slaves to master, then make the other additional salves use the new master. 2.4) Configuration Provider Sentinel acts as source of authority for clients service delivery. Clients connect to Sentinels in order to ask for address of current Redis master responsible for given service. If a failover occurs, Sentinels will report the new add...

[Redis] Redis Persistence

1) Persistence Methods 1.1) No Persistence, dataset in memory  You can disable persistence if you want your data to exist as long as server is running. 1.2) Redis Database File (RDF) Snapshot of dataset per time interval 1.3) Append Only File (AOF) Log every write operation, which will be played again at server startup, reconstructing the original dataset. 1.4) Hybrid (RDB & AOF) Note that when Redis restarts, the AOF file will be used to reconstruct the original dataset since it is guaranteed to be the most complete. 2) RDB (Redis Database File) Allow you to snapshot dataset every N seconds if there are at least M changes. 2.1) How does it work? (also called snapshotting) When Redis create a snapshot, this happens: Redis forks. New child process starts in addition to the parent process. The child process begins to write the dataset to a temporary RDB file. Once the child process finishes writing to the new RDB file, it replaces the old one. 2.2) Commands...

[Cache] Caching Strategies

1) Lazy Loading Load data into cache only when necessary. Ask Cache - if unavailable, Ask Database. 1.1) Cache Hit When your data is in cache and not expired: Application requests data from the cache. Cache returns data to the application. 1.2) Cache Miss Application requests data from the cache. Cache doesn't have requested data and returns null. Application requests and receives data from database. Application updates cache with requested data for quicker access next time. 1.3) Pros & Cons 1.3.1) Pros Only requested data is cached. Most data are never requested, lazy loading avoids filling up your cache with data that isn't requested. Node Failures are not fatal When node fails and is replaced, new node continues to function as requests are being made. (as opposed to that in Write Through) 1.3.2) Cons Cache Miss Penalty Each cache miss results in 3 trips: Initial data request to cache Database query Writing data to node S...

[Redis] Quick Start to Redis

Officially, Redis is not supported on Windows. There is, however, a 3.2.1 version of Redis that was ported to Windows by MSOpenTech. Here, I will show you how to get a redis instance running very quickly. If you want a more stable implementation, please read from https://redislabs.com/blog/redis-on-windows-8-1-and-previous-versions. 1. Download Redis MSOpenTech has support for 3.2.1 of Redis.  Download the zip from  https://github.com/MicrosoftArchive/redis/release 2. Running Redis Server + Cli Unzip the downloaded zip, and run redis-server.exe Then, run redis-cli.exe In the cli, you should enter and expect: 127.0.0.1:6379> get foo (nil) 127.0.0.1:6379> set foo 123 OK 127.0.0.1:6379> get foo "123" 3. Accessing Redis Server from Python and Ruby Script Python: 1. install redis (pip install redis) 2. Run script: import redis r = redis.Redis(host='localhost', port=6379, db=0) print("Setting bar to key, foo")...