Open source recommendation system based on time-series data and statistical analysis. Written in TypeScript
and Node.js
using Redis
for storage. The recommendation system uses the Jaccard index
to calculate the intersection between two sets. One set is represented by the maximum possible sum of tag score and the other set is the score sum of user events per tag. The higher the Jaccard index, the higher the recommendation. It uses numbers to represent sets to increase performance.
- Use tag score and Jaccard index
- Content-based filtering
- Event-driven powered engine
- Naive exploration of new tags
- Suitable for product and content recommendation
- Fine-tuning of tag weights
- Minimalist and lightweight
- Written in TypeScript and Node.js
- Actors are stored in Redis as simple
String
keys with create datetimestamps
as value. - Items are
Set
type withtags
as members. The item may have multiple tags. - Events are
String
type withactorId:id:tag:timestamp:ttl
and an expire attribute set to ensure freshness of recommendations.
- Get the actor with events
- Check if the actor exists with
EXISTS actor:${id}
- Get all user events with
SCAN ${loop cursor} MATCH actor:${id}
- Check if the actor exists with
- Delete a single actor
- Scan for each related to actor key
SCAN ${loop cursor} MATCH actor:${id}*
- For each key delete with
DEL ${key}
- Scan for each related to actor key
- Add a single actor
- Scan for each related to actor key
SCAN ${loop cursor} MATCH actor:${id}*
- For each key delete with
DEL ${key}
- Add new actor with
SET actor:${id} ${Date.now().toString()}
- Scan for each related to actor key
- Add a single event
- Check if actor exists if flag is set using
EXISTS actor:${id}
- Add event with
SET actor:${id}:${tag}:${date}:${ttl} ${score}
- If
TTL
has been provided, set expiration for event withEXPIRE actor:${id}:${tag}:${date}:${ttl} ${ttl}
- Check if actor exists if flag is set using
- Get all items with tags
- Get all items with
SCAN ${loop cursor} MATCH item:*
- For each item get all tags with
SMEMBERS ${itemKey}
- Get all items with
- Get a single item with tags
- Get all tags of item with
SMEMBERS item:${id}
- Get all tags of item with
- Delete single item
- Call with
DEL item:${id}
- Call with
- Add a single item
- Check if item already exists
EXISTS item:${id}
- If so, then remove
DEL item:${id}
- And add item with tags
SADD item:${id} ${tags}
- Check if item already exists
# clone repository with the source code
git clone
# install dependencies
yarn
# build the application from ts to js
yarn build
# start the application
yarn start
# alternatively use ts-node in the developer mode
yarn dev
To make deploys work, you need to create free account on Redis Cloud
To run tests use the following command:
yarn test
You should see the following output:
PASS src/__tests__/recommendations.spec.ts
Recommendations
β should item 1 has score 1 and be the first (7 ms)
β should item 2 has score 1 and be the first (1 ms)
β should return two recommendation with the same score of 1 (1 ms)
β should return two recommendation with the same score of 0.5
β should return three recommendation with 1, 1 and 0.5 score
PASS src/__tests__/envs.spec.ts
Envs
β should return one recommendation (2 ms)
β should return two recommendations
β should return recommendation with 0.125 score (1 ms)
β should return recommendation with 0.0625 score
β should clamp results to 0
# redis host
REDIS_HOST="localhost"
# redis port
REDIS_PORT="6379"
# redis password
REDIS_PASSWORD="mysecretpassword"
# write additional logs to stdout
VERBOSE="true"
# do not check if an actor exists when adding an event
DO_NOT_CHECK_ACTOR_EXISTENCE="true"
# maximum size of a candidate pool of items to calculate recommendation positions
ITEMS_LIMIT="100000"
# maximum number of events to use for recommendation calculation
EVENTS_LIMIT="100000"
# maximum number of recommendations to return
RECOMMENDATIONS_LIMIT=100
# wait to finish event insertion before http response
WAIT_FOR_EVENT_INSERTION="false"
# chance of recommending irrelevant item, used for exploration of new tags
# to minimize the recommendation bubble effect
EXPLORATION_NOISE="0.1"
# max sum of events score set per tag
# e.g. 1 (view event) + 2 (like event) + 5 (lead event) = 8
JACCARD_MAX_TAG_SCORE="8"
# clamp the recommendation results between 0 and 1
JACCARD_CLAMP_RESULT_RECOMMENDATIONS="true"
Set (add or replace) actor
POST /api/actors
{
"externalId": "string"
}
Get specific actor by actorId
GET /api/actors/:actorId
Get recommendation for actor
GET /api/actors/:actorId/recommendation
Delete specific actor
DELETE /api/actors/:actorId
Add new event to an actor
POST /api/actors/:actorId/events
{
"tag": "tag1",
"score": 3,
"ttl": 60
}
Set (add or replace) item
POST /api/items
{
"externalId": "string",
"tags": ["tag1", "tag2", "..."]
}
Get specific item by itemId
GET /api/items/:itemId
Delete specific item
DELETE /api/items/:itemId
Here some resources to help you quickly get started using Redis Stack. If you still have questions, feel free to ask them in the Redis Discord or on Twitter.
- Sign up for a free Redis Cloud account using this link and use the Redis Stack database in the cloud.
- Based on the language/framework you want to use, you will find the following client libraries:
- Redis OM .NET (C#)
- Watch this getting started video
- Follow this getting started guide
- Redis OM Node (JS)
- Watch this getting started video
- Follow this getting started guide
- Redis OM Python
- Watch this getting started video
- Follow this getting started guide
- Redis OM Spring (Java)
- Watch this getting started video
- Follow this getting started guide
- Redis OM .NET (C#)
The above videos and guides should be enough to get you started in your desired language/framework. From there you can expand and develop your app. Use the resources below to help guide you further:
- Developer Hub - The main developer page for Redis, where you can find information on building using Redis with sample projects, guides, and tutorials.
- Redis Stack getting started page - Lists all the Redis Stack features. From there you can find relevant docs and tutorials for all the capabilities of Redis Stack.
- Redis Rediscover - Provides use-cases for Redis as well as real-world examples and educational material
- RedisInsight - Desktop GUI tool - Use this to connect to Redis to visually see the data. It also has a CLI inside it that lets you send Redis CLI commands. It also has a profiler so you can see commands that are run on your Redis instance in real-time
- Youtube Videos
- Official Redis Youtube channel
- Redis Stack videos - Help you get started modeling data, using Redis OM, and exploring Redis Stack
- Redis Stack Real-Time Stock App from Ahmad Bazzi
- Build a Fullstack Next.js app with Fireship.io
- Microservices with Redis Course by Scalable Scripts on freeCodeCamp
MIT