{"id":51594447,"url":"https://github.com/cfryerdev/redis-vehicles","last_synced_at":"2026-07-11T17:02:42.539Z","repository":{"id":46299316,"uuid":"457886468","full_name":"cfryerdev/redis-vehicles","owner":"cfryerdev","description":"This is a sample redis dataset using generic vehicle data allowng us to view and filter vehicle data in single digit milliseconds.","archived":false,"fork":false,"pushed_at":"2024-03-23T21:28:38.000Z","size":13012,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-03-23T22:27:45.178Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/cfryerdev.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}},"created_at":"2022-02-10T17:47:07.000Z","updated_at":"2024-03-23T22:27:45.178Z","dependencies_parsed_at":"2023-02-14T09:47:03.577Z","dependency_job_id":null,"html_url":"https://github.com/cfryerdev/redis-vehicles","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cfryerdev/redis-vehicles","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cfryerdev%2Fredis-vehicles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cfryerdev%2Fredis-vehicles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cfryerdev%2Fredis-vehicles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cfryerdev%2Fredis-vehicles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cfryerdev","download_url":"https://codeload.github.com/cfryerdev/redis-vehicles/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cfryerdev%2Fredis-vehicles/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35368768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-11T02:00:05.354Z","response_time":104,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2026-07-11T17:02:40.254Z","updated_at":"2026-07-11T17:02:42.524Z","avatar_url":"https://github.com/cfryerdev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Set Theory using Redis \u0026 Vehicles (Beginners)\nSet Theory can be a deep and complex topic that may scare many away. Over the years I have found teaching the basic concepts can make this fun and useful in your daily life as an engineer.\n\n***This is a sample redis dataset using generic vehicle data allowng us to view and filter vehicle data in single digit milliseconds.*** In thie example we have two files:\n\n- `database.json`: The vehicle database we will be searching, 1800 records.\n- `loader.js`: Hydrates redis with sets based on the vehicle database. \n\nDuring this example, we will be using redis and basic set theory principles to search vehicles quickly.\n\n### Important Terms\n\n- **Set:** A unique list of values, belonging to a key.\n- **Superset:** A set of sets.\n- **Intersection:** The result of two or more sets joined, of only the values which belong to all sets. (No duplicates)\n- **Union:** The result of two or more sets joined, containing the values from all sets. (No duplicates)\n- **Inverse Sets:** A set containing keys, where the value is the key.\n\n## 🔴 Getting Started\n\nFor this demo we need to start redis and hydrate data in the new searchable format. Instead of standing up redis and configuring it and all that, lets just run it in docker to make this process simple. \n\n1) Start Redis: `docker run -d --name dev-redis -p 6379:6379 -d redis`\n2) Install Dependencies: `npm i`\n3) Lastly, Hydrate Redis: `npm start`\n\nFirst lets ensure docker is running...\n\n![Docker_Running](images/docker_running.png)\n\nNow we can start redis commander which is the tool we will use to look at the data.\n\n4) Install: `npm install -g redis-commander`\n\n## 🔴 Looking at the data\n\nFirst off start Redis Commander by running `redis-commander` in the terminal. Once you have that running, navigate to: `localhost:8081` in a web browser and add a connection to `localhost:6379`. Now you should see your sets which we will be working with today.\n\n![Redis_Hydrated](images/redis_hydrated.png)\n\nWell how do we create these sets... well its actually quite simple and requires very little code. As you see below, we have a simple loop which puts data in the correct set by using a namespace string separated by the `:` character.  Thats really it. This current example is written in node, but Redis supports almost every language you could think of. \n\n![Code_Hydrate](images/code_hydrate.png)\n\n**Note: Obviously the above code could be done in parallel but I kept it synchronous for ease of reading.**\n\nLets continue on...\n\n### Duplicative Data \u0026 Lenses\n\nYou will immediately notice the duplicitive nature of this pattern, but this is absolutely intentional. Each set is a \"lense\" in which you look at the data. We trade response time, for storage cost, which is signifigantly cheaper.\n\nWe want as many sets, inverse sets, and supersets as possible. Ideally as many ways you might look at data, we want sets for each.\n\n### Counts\n\nNatively in each set and namespace, we have a length that is stored at write. We dont have to query for this, we can simply use a command or visually look at the amount of vehicles we have in each set! \n\n![Redis_Counts](images/redis_counts.png)\n\n### Browsing a Set\n\nSimply expanding out shows the heiarchy of a set's namespace by using a folder structure. In redis these are flat sets, but Redis Commander gives us a good visual. We can see in the following example that a simple set has a key and a list of values. In this case a list of all cars with three doors.\n\n![Redis_Sets](images/redis_sets.png)\n\n### Inverse Sets\n\nInverse sets are a great way to see what sets, another key is in. So instead of a Key and a bunch of values, you have values with a list of keys in each. For this example, we see a list of vins, and what sets this vin is found in!\n\n![Redis_InverseSets](images/redis_inversesets.png)\n\n### Composits or Supersets\n\nComposits (or supersets) are essentially sets of sets. Lets take fuel efficency for example... What makes a vehicle fuel effient? Fuel Capacity? Electric Motors? 3 or 4 Cylendar engines? In the following example we create a composit called \"fuel efficient\" and within it we list the sets that contain vehicles with fuel efficent traits. We can use these to summarize business logic or grouping of data!\n\n![Redis_Composits](images/redis_composits.png)\n\n### Vehicle Data\n\nA common pattern I like to implement, is the data namespace set. This is just a simple set where the key is a vin and the value is a json payload of the entire vehicle. I can use this for lookups as well!\n\n![Redis_Data](images/redis_data.png)\n\n## 🔴 Finding Data\n\nAlright here is where the rubber hits the road: How do I use this data to create ultra high performant search/filter operations across this dataset? Lets get started...\n\n### Your first Union\n\nLets say we want to get a list of vehicles which are in fact cars. We could have a composite set with the following sets within it: `Sedan Coupe and Convertible` or we can query this using a union. What if a composite doesnt exist and you need to combine results in a one-off query. Well its easy, check it out:\n\nCommand: \n```\nSUNION sets:bodies:Sedan sets:bodies:Coupe sets:bodies:Convertible\n```\nResult:\n```\n1) \"WDDHF5KB0CA531376\"\n2) \"4T1B11HK3JU628223\"\n3) \"JTDKN3DU1F1873459\"\n4) \"4S3BNAR61K3034957\"\n5) \"3MYDLBYV4KY513525\"\n...\n```\n\n\n### Your first Intersection\n\nExample: Lets say we want to limit our responses to all  2019 cars with two doors... What would that command look like? Well if we look we have sets for these attributes: \n\n```\nsets:doors:*\nsets:types:*\nsets:years:*\n```\n\nSo if we want to find our vehicle, we need to ensure we only return vin numbers that exist in **ALL THREE** of these sets right? (the order of the sets does not matter here)\n\nCommand: \n```\nSINTER sets:doors:2 sets:types:Car sets:years:2019\n```\nResult:\n```\n1) \"1G1FB3DS1K0113498\"\n2) \"2C3CDZJG4KH617616\"\n3) \"WMWXP7C59K2A49781\"\n4) \"JN1AZ4EH0KM421666\"\n5) \"3VWFD7ATXKM717101\"\n6) \"1FA6P8CF4K5196010\"\n7) \"1FA6P8TH5K5175362\"\n8) \"2C3CDZAG0KH731743\"\n```\n\nAs we can see, we have 8 unique vins that exist in these three sets. Cool!\n\n### Extra: Returning json single result\n\nReturning the json for a single record is quite simple with the get command! The syntax requires just a single key to return the value:\n\nCommand:\n```\nGET data:vehicles:1G1FB3DS1K0113498\n```\nResult:\n```\n{ \"vin\": \"1G1FB3DS1K0113498\", \"year\": 2019, \"make\": \"Chevrolet\", \"model\": \"Camaro\"...\n```\n\n### Extra: Returning json list results\n\nSo we have an array of vin numbers, well now we want to return this as an array of json payloads right? Thats why we have our `data:vehicles:*` set right?\n\nTo make this work youc an use a library, or a redis-cli module called: \"[RedisJSON](https://redis.io/docs/stack/json/)\" Which will let you essentially return them as an array of vehicles:\n\nCommand: \n```\nJSON.ARRAPPEND data:vehicles:1G1FB3DS1K0113498 data:vehicles:2C3CDZJG4KH617616 $\n```\n\nBut I would recommend doing this in code with a library, just make sure you do this union of values on the redis server, not in code. We dont want to hit redis once for each record 😛. Example:\n\n![Redis_JsonAppend](images/code_jsonappend.png)\n\n## 🔴 Conclusion\n\nYou can use this pattern to easily build out complex search/filtering systems using basic set theory principles. \n\nWant to know more redis commands? [https://redis.io/commands/](https://redis.io/commands/)\n\nIf you want to know more about Set Theoryl This subject tends to be quite heavy, so I recommend watching these after this exercise. \n\n- https://www.youtube.com/watch?v=tyDKR4FG3Yw\n- https://www.youtube.com/watch?v=xZELQc11ACY\n- https://www.youtube.com/watch?v=4TlCToZZ5gA\n\nThanks!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcfryerdev%2Fredis-vehicles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcfryerdev%2Fredis-vehicles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcfryerdev%2Fredis-vehicles/lists"}