{"id":18575553,"url":"https://github.com/jsiqbal/redis","last_synced_at":"2025-04-10T08:30:51.349Z","repository":{"id":245254940,"uuid":"817643553","full_name":"JsIqbal/redis","owner":"JsIqbal","description":"Complete redis course by stephen grider","archived":false,"fork":false,"pushed_at":"2024-10-27T19:56:38.000Z","size":296,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T18:49:30.827Z","etag":null,"topics":["hyperloglog","redis","redis-hash","redis-sets","redis-string","session","sort","sorted-sets","sveltkit"],"latest_commit_sha":null,"homepage":"","language":"Svelte","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JsIqbal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-20T06:51:45.000Z","updated_at":"2025-01-05T20:37:03.000Z","dependencies_parsed_at":"2024-06-20T23:35:51.154Z","dependency_job_id":"3456d6c5-4d33-419f-a115-81e705f467eb","html_url":"https://github.com/JsIqbal/redis","commit_stats":null,"previous_names":["jsiqbal/redis"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JsIqbal%2Fredis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JsIqbal%2Fredis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JsIqbal%2Fredis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JsIqbal%2Fredis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JsIqbal","download_url":"https://codeload.github.com/JsIqbal/redis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248185251,"owners_count":21061481,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hyperloglog","redis","redis-hash","redis-sets","redis-string","session","sort","sorted-sets","sveltkit"],"created_at":"2024-11-06T23:20:15.425Z","updated_at":"2025-04-10T08:30:50.675Z","avatar_url":"https://github.com/JsIqbal.png","language":"Svelte","readme":"# create-svelte\n\nEverything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).\n\n## Creating a project\n\nIf you're seeing this, you've probably already done this step. Congrats!\n\n```bash\n# create a new project in the current directory\nnpm init svelte@next\n\n# create a new project in my-app\nnpm init svelte@next my-app\n```\n\n\u003e Note: the `@next` is temporary\n\n## Developing\n\nOnce you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:\n\n```bash\nnpm run dev\n\n# or start the server and open the app in a new browser tab\nnpm run dev -- --open\n```\n\n## Building\n\nTo create a production version of your app:\n\n```bash\nnpm run build\n```\n\nYou can preview the production build with `npm run preview`.\n\n\u003e To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.\n\n---\n\n### Section 1: Get started here\n\n-   redis is a database faster than treditional databases.\n-   redis is faster because it uses the ram.\n-   radis stores data in simple data structures.\n-   redis has a small number of feature set\n\n#### Instructions:\n\n-   create a redis in redis.com\n-   create a free subscription\n-   create a new database\n-   connect the database in rbook.cloud\n\n##### Simple command testing:\n\n```bash\n    SET key value\n```\n\n---\n\n### Section 2: Commands for Adding and Querying data\n\n#### Strings:\n\n-   set string:\n\n```bash\nSET message \"Hellow World!\"\n```\n\n-   get string:\n\n```bash\nGET message\n```\n\n-   Check out the commands from redis official docs\n-   see the diagrams in section 2\n-   `GET`: get a value using key: `GET message`\n-   `NX`: create if the key doesn't exist yet : `SET color blue NX / SETNX color blue`\n-   `EX`: remove the record after limit time : `SETEX message 2 \"Hello World!\"`\n-   `MSET`: multiple set values for keys : `MSETNX colofgsdr blue moshfdel toyota`\n-   `MGET`: multiple get values for keys : `MGET colofgsdr moshfdel`\n-   `DEL`: deletes the key: `DEL message`\n-   `GETRANGE`: gets a string upon (start index - end index) position\n-   `SETRANGE`: sets a string upon from index value: SETRANGE model 2 bule\n-   using radis we can make robust minimized datas and retrieve them faster as well\n\n#### Numbers:\n\n-   `SET`: set a number : `SET key 1`\n-   INCR: increament a key with 1 : `INCR key`\n-   `DECR`: decrement a key with 1 : `DECR key`\n-   `INCRBY`: increament a number by given limit : `INCRBY key 10`\n-   `DECRBY`: decrement a number by given limit : `DECRBY key 5`\n-   `INCRBYFLOAT`: increament a number float wise limit : `INCRBYFLOAT key -2.55`\n\n*   Redis is synchronous and single threaded by nature.\n\n---\n\n### Section 3: E-commerce App Setup\n\n### Law of caching:\n\n#### Redis Design Methodology:\n\n-   Figure out what queries we need to answer\n-   Structure data to best answer those queries\n\n#### Key naming conventions:\n\n-   Keys should be unique\n-   Other engineers should understand what a key is for\n-   Tip - use functions to generate your key names so you never make a typo\n-   Extremely common practice is to use a ':' to separate different parts of the key\n-   Small twist on common practice - we are going to use a # before unique ID's to make implementing search easier : users#45\n\n### Section 4: Local redis Setup\n\n#### Install docker desktop\n\n-   run\n\n```bash\ndocker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest\n```\n\n-   moderate\n\n```bash\ndocker exec -it redis-stack-server redis-cli\n```\n\n-   connection for local redis with rbay:\n\n```env\nREDIS_HOST=localhost\nREDIS_PORT=6379\nREDIS_PW=\n```\n\n-   If you want to connect RBook to your local copy of Redis, you will need to run RBook locally.\n\n    -   To run RBook locally, run npx rbook at your terminal.\n\n    -   Navigate to localhost:3050\n\n    -   Open the connection settings window\n\n    -   Enter a host of 'localhost'\n\n    -   Enter a port of 6379\n\n    -   Leave the password blank\n\n    -   When running RBook locally, any notebooks you create will be added to the folder you ran npx rbook in.\n\n---\n\n### Section 5: Hash Data Structures\n\n#### Initial:\n\n-   A hash in redis can be an object of key value pairs but they will not be deeply nested:\n\n    -   will work:\n        ```json\n        {\n            \"name\": \"IQ \u0026 CO.\",\n            \"created\": 1989,\n            \"industry\": \"Technology\"\n        }\n        ```\n\n-   in redis hashes there can only be number and strings as value for the keys\n\n-   `HSET`: set multiple key value pare under a mother key : `HSET company name iq industry tech age 12.3 estd 1945`\n-   `HGET`: get a single key value : `HGET compnay industry`\n-   `HGETALL`: gets all the key values from a mother key : `HGETALL company`\n-   `HEXISTS`: checks if a key under monther key exists: `HEXISTS company age`\n-   `DEL`: deleted all the key value pair under a mother key: `DEL company`\n-   `HDEL`: deletes a single key value under mother key : `HDEL company age`\n-   `HINCRBY`: increament a certain key's value under mother key which must be an integer : `HINCRBY company estd 10`\n-   `HINCRBYFLOAT`: increament a certain key's value under mother key which must be an float : `HINCRBYFLOAT company a 10.5`\n-   `HSTRLEN`: gives the length of the value under a key under mother key : `HSTRLEN company industry`\n-   `HKEYS`: gives all the keys under the mother key: `HKEYS company`\n-   `HVALS`: gives all the values under all the keys under the mother key : `HVALS company`\n-   `HMSET`: can set a new key and value under the mother key: `HMSET company newKey \"new value\"` [The same can be achieved using HINCRBY]\n\n### Section 6: Redis has gotchas!\n\n-   please check out the file in sandbox: index.ts\n\n### Section 7: Powerful design pattern\n\n-   SQL Database Design Methodology\n\n    -   Put the data in tables\n    -   Figure out how we will query it\n\n-   Redis Design Methodology\n\n    -   Figure out what queries we need to answer\n    -   Structure data to best answer those queries\n\n-   Reasons to Store as Hash\n\n    -   The record has many attributes\n    -   A collection of these records have to be sorted many different ways\n    -   Often need to access a single record at a time\n\n-   Don't Use Hashes When...\n\n    -   The record is only for counting or enforcing uniqueness\n    -   Record stores only one or two attributes\n    -   Used only for creating relations between different records\n    -   The record is only used for time series data\n\n---\n\n### Section 8: Pipelining commands\n\n-   Redis does not give power to fetch multiple hashes(IDs) in one single command\n\n    -   Possible solutions [1]:\n        -   Loop through the Ids and execute HGETALL and get the records one by one.\n        -   We create a hash for each id and execute the HGETALL command each time of creating the id. meaning:\n            -   the loop is running\n            -   the loop is creating hash\n            -   the loop is executing individual command\n        -   !!! THIS IS NOT EFFECIENT\n    -   Possible solutions [2]:\n        -   We create batch of command using loop\n        -   we execute HGETALL command for all the ids in one go.\n            -   we create hash with ids in a loop but\n            -   we tell the loop not to send the request to redis yet\n            -   we complete the loop\n            -   we complete creating the batch of HGETALL\n            -   we give the list of request to redis\n            -   redis gives us a list of records\n\n---\n\n### Section 9: Enforcing uniqueness with set\n\n-   Redis has a special feature called set\n\n    -   In set:\n\n        -   [SADD] we can add unique value in a set\n\n            -   If we keep adding the same value its not going to be added in the set\n\n            *   Example:\n                -   `SADD`: Add a unique value with a key: `SADD name Iqbal` -\u003e will return 1 -\u003e Its going to add iqbal to a set of name.\n                    -   if we try to add `SADD name Iqbal` -\u003e will return 0 -\u003e Its not going to add any value in the key of name.\n\n        -   [SUNION] we can keep and show the same values existing in saparate sets\n\n            -   `SADD colors:1 red blue green`\n            -   `SADD colors:2 yellow purple green`\n            -   `SADD colors:3 shen green ken`\n\n                -   `SUNION colors:1 colors:2 colors:3` -\u003e will give us the combined values of unions from the sets:\n\n                    -   [\n                        \"red\",\n                        \"blue\",\n                        \"green\",\n                        \"yellow\",\n                        \"purple\",\n                        \"white\",\n                        \"shen\",\n                        \"ren\",\n                        \"ken\"\n                        ]\n\n                -   `SINTER colors:1 colors:2 colors:3` -\u003e will give us only the common value from all the sets\n                    -   [\n                        \"blue\"\n                        ]\n                -   `SDIFF colors:1 colors:2 colors:3` -\u003e will only give us the unique value that only exist in x but not in n numbers in sets.\n\n                    -   [\n                        \"red\"\n                        ]\n\n                -   `SINTERSTORE color:result colors:1 colors:2 colors:3` -\u003e will store the common values in a new key\n                -   `SMEMBERS color:result` -\u003e will show the values stored in the new key\n                    [\n                    \"blue\",\n                    \"green\"\n                    ]\n                -   `SISMENBER color:result blue` -\u003e will give 1 if blue exists on color:result key and 0 if not exist\n                -   `SMISMEMBER color:result blue green` -\u003e will give us an array of true and false.\n                    -   [\n                        1,\n                        1\n                        ]\n                -   `SCARD color:result` -\u003e this gives the number of elements in a key\n                -   `SSCAN colors:1 0 COUNT 2` -\u003e this gives the paginated data. 0 is the page number in here. 2 is the number of element to show in a page.\n\n*   -   Use cases for SETS of Redis:\n        -   Enforcing uniqueness of any value : suppose a user wants to register but in our cache we have the user already registered.\n        -   Creating a relationship between different records:\n        -   Finding common attributes between different things: SINTER to see the common items liked by 2 users.\n        -   General list of elements where order doesn't matter:\n\n---\n\n### Section 10: Implementing sets\n\n---\n\n### Section 11: Sorted SET\n\n-   Redis has a special feature called Sorted set\n\n    -   In sorted set all the members are sorted from low to high. 0 1 2 3 4 5....\n\n    -   Sorted Set:\n\n        -   `ZADD` is used to add a member-score pair to a sorted set. The command is a little unusual\n\n            -   you first provide the score _then_ the member: `ZADD products 45 monitor`\n\n        -   `ZSCORE` returns the score of a member: `ZSCORE products monitor`\n\n        -   `ZREM` removes a member-value pair: `ZREM products monitor`\n\n        *   Reference:\n\n        ```REDIS\n            DEL products\n\n            ZADD products 45 cpu\n            ZADD products 10 keyboard\n            ZADD products 55 power\n\n            ZCARD products\n        ```\n\n        -   `ZCARD` returns the total number of members in a sorted set: `ZCARD products` =\u003e 3.\n        -   `ZCOUNT` is very similar to `ZCARD`, but includes from filtering criteria.\n\n            You can use `ZCOUNT` to find the number of members between a range of two scores. By default, these scores are _inclusive_.\n\n            There is a variation on the score syntax. Adding in an opening paren before a score (E.g. `(45`) changes the comparison to 'greater than' when applied to the lower bounds, or 'less than' when applied to the upper bound.\n\n            You can also use `-inf` or `+inf` to specify negative or positive infinity, respectively.\n\n            -   `ZCOUNT -inf 55` =\u003e 3\n\n        -   `ZPOPMIN` removes and returns the member with the lowest score: `ZPOPMIN products 3` =\u003e [\"keyboard\",\"10\",\"cpu\",\"45\",\"power\",\"55\"]\n        -   `ZPOPMAX` removes and returns the member with the greatest score: `ZPOPMAX products 2` =\u003e [\"power\",\"55\",\"cpu\",\"45\"]\n        -   `ZINCRBY` adjusts the score of an existing member-score pair: `ZINCRBY products 15 cpu` =\u003e \"60\"\n            -   Provide a negative number to subtract from a score: `ZINCRBY products -15 cpu` =\u003e \"30\"\n            -   New key with value can also be added using `ZINCRBY`: `ZINCRBY products -15 x` =\u003e \"-15\"\n        -   `ZRANGE` Filter the set and find value from 0-60 and limit the result from index 1-3: `ZRANGE products 0 60 BYSCORE LIMIT 0 3` =\u003e [\"keyboard\",\"cpu\",\"power\"]\n            -   also can be applied using `WITHSCORE` `-inf` `+inf` `(45` `45)`\n            -   `REV`: will reverse the current set and after that will apply other commands to the set\n\n---\n\n### Section 12: Sorted SET Implementation\n\n-   Sorted set are needed for relationships between different sets in redis\n-   By using sorted set we can achieve Normalization of DB in Redis\n\n---\n\n### Section 13: Sort Data structure\n\n-   A special kind of data structure in redis.\n-   Normally a hash stores data as key value pair.\n-   A sorted set stores as members and score.\n\n*   but a `SCORE` data structure in redis sorts using the members of the `set`, `sorted set` or `list`!\n\n-   first create some sets and sorted sets:\n\n```bash\n    HSET books:good title 'Good Book' year 1950\n    HSET books:bad title 'Bad Book' year 1930\n    HSET books:ok title 'Ok Book' year 1940\n\n    ZADD books:likes 999 good\n    ZADD books:likes 0 bad\n    ZADD books:likes 40 ok\n```\n\nthen apply -\u003e\n\n-   SORT:\n\n-   `SORT books:likes ALPHA` : stores the members of the sorted set alphabetically and sends back the response:\n\n```bash\n[\n  \"bad\",\n  \"good\",\n  \"ok\"\n]\n```\n\n-   `SORT books:likes LIMIT 1 2 ALPHA` : stores the members of the sorted set alphabetically and sends back the response, skip the first element and give the next 2 element of the sort:\n\n```bash\n[\n  \"good\",\n  \"ok\"\n]\n```\n\n-   `SORT books:likes BY books:*-\u003eyear ` :\n    -   breakdown of the command:\n        -   first replace the \\* with the member from the sorted set : books:good\n        -   scan redis for the key books:good\n        -   if the key exists then find the value with the year: books:good has year 1950 in it.\n        -   create a volatile set with key value structure like:\n\n```bash\ngood - 1950\nbad - 1930\nok - 1940\n```\n\n        - BY means sort the set by descending order like:\n\n```bash\nbad - 1930\nok - 1940\ngood - 1950\n```\n\nresponse from redis:\n\n```bash\n[\n  \"bad\",\n  \"ok\",\n  \"good\"\n]\n```\n\n-   `SORT books:likes BY books:*-\u003eyear GET books:*-\u003etitle` :\n    -   breakdown of the command:\n        -   first replace the \\* with the member from the sorted set : books:good\n        -   scan redis for the key books:good\n        -   if the key exists then find the value with the year: books:good has year 1950 in it.\n        -   create a volatile set with key value structure like:\n\n```bash\ngood - 1950\nbad - 1930\nok - 1940\n```\n\n-   BY means sort the set by descending order like:\n\n```bash\nbad - 1930\nok - 1940\ngood - 1950\n```\n\n-   `GET books:*-\u003etitle`:\n    -   again scan redis and find the books:good and return the title of the book\n    -   use sorting criteria from BY year\n\nresponse from redis:\n\n```bash\n[\n  \"Bad Book\",\n  \"Ok Book\",\n  \"Good Book\"\n]\n```\n\n-   More examples:\n\n```bash\nSORT books:likes BY books:*-\u003eyear\n    GET books:*-\u003etitle\n    GET books:*-\u003eyear\n```\n\n-   response:\n\n```bash\n[\n  \"Bad Book\",\n  \"1930\",\n  \"Ok Book\",\n  \"1940\",\n  \"Good Book\",\n  \"1950\"\n]\n```\n\n-   in here # means also give the name of the member with associated record\n\n```bash\nSORT books:likes BY books:*-\u003eyear\n    GET #\n    GET books:*-\u003etitle\n    GET books:*-\u003eyear\n```\n\n-   response:\n\n```bash\n[\n  \"bad\",\n  \"Bad Book\",\n  \"1930\",\n  \"ok\",\n  \"Ok Book\",\n  \"1940\",\n  \"good\",\n  \"Good Book\",\n  \"1950\"\n]\n```\n\n-   in here `nosort` means give the associated record but not based on any sorting criteria:\n\n```bash\nSORT books:likes BY nosort\n    GET #\n    GET books:*-\u003etitle\n    GET books:*-\u003eyear\n```\n\n-   response:\n\n```bash\n[\n  \"bad\",\n  \"Bad Book\",\n  \"1930\",\n  \"ok\",\n  \"Ok Book\",\n  \"1940\",\n  \"good\",\n  \"Good Book\",\n  \"1950\"\n]\n```\n\n-   SORT has some more options attached to it. You should checkout the .d.ts file of the command!\n\n---\n\n### Section 14: HyperLogLog data structure\n\n-   This is a data structure that remembers a value under a key by using complex algorithm but not storing the data.\n\n    -   `PFADD` Adds a string value under a key: `PFADD vegetable celery` =\u003e 1 `e.g: 0 if already exists`\n    -   `PFCOUNT` Tells how many value are there in a key: `PFCOUNT vegetable` =\u003e 1 `e.g: 0 if already exists`\n\n*   use cases:\n    -   remembering how many view a product or item has\n    -   using in types of data that are not so important to be consistent\n    -   only 12kb ob static memory for even millions of view for an item.\n\n---\n\n### Section 15: List data structure\n\n-   This is not an array\n-   This is a doubly linked list\n\nTypically we use list to keep time series data or data in order like : weather data.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsiqbal%2Fredis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsiqbal%2Fredis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsiqbal%2Fredis/lists"}