{"id":43342,"library":"mysql-redis","title":"mysql-redis","description":"A caching layer between MySQL (mysql/mysql2) and Redis (redis/ioredis) that transparently caches SELECT query results in Redis to reduce database load. Stable version 0.7.2, last updated in 2018. It hashes queries using configurable algorithms (md5, farmhash32/64, blake2b512, or full query) and stores results in Redis with TTL. Zero mandatory external dependencies (farmhash is optional). Designed for read-heavy workloads with static or rarely updated data. Falls back to MySQL if Redis is unavailable. Not suitable for frequently updated data due to potential cache staleness.","status":"active","version":"0.7.2","language":"javascript","source_language":"en","source_url":"https://github.com/kwalski/mysql-redis","tags":["javascript","node","mysql2","mysql","ioredis","redis","cache","caching","layer"],"install":[{"cmd":"npm install mysql-redis","lang":"bash","label":"npm"},{"cmd":"yarn add mysql-redis","lang":"bash","label":"yarn"},{"cmd":"pnpm add mysql-redis","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Optional dependency for farmhash32/64 hash types. Not needed if using md5 or other hash types.","package":"farmhash","optional":true}],"imports":[{"note":"CommonJS named export. No default export. Use destructuring.","wrong":"const MysqlRedis = require('mysql-redis')","symbol":"MysqlRedis","correct":"const { MysqlRedis } = require('mysql-redis')"},{"note":"No ESM exports. Package is CommonJS only. Use require.","wrong":"import { MysqlRedisAsync } from 'mysql-redis'","symbol":"MysqlRedisAsync","correct":"const { MysqlRedisAsync } = require('mysql-redis')"},{"note":"Named export, destructure or access via require result.","wrong":"const HashTypes = require('mysql-redis').HashTypes","symbol":"HashTypes","correct":"const { HashTypes } = require('mysql-redis')"}],"quickstart":{"code":"const mysql = require('mysql2');\nconst redis = require('redis');\nconst { MysqlRedis } = require('mysql-redis');\n\nconst mysqlConn = mysql.createConnection({ host: 'localhost', user: 'root', database: 'test' });\nconst redisClient = redis.createClient({ host: 'localhost' });\n\nconst cacheOptions = {\n  expire: 3600,\n  keyPrefix: 'sql.',\n  hashType: 'md5'\n};\n\nconst mysqlRedis = new MysqlRedis(mysqlConn, redisClient, cacheOptions);\n\nmysqlRedis.query('SELECT * FROM countries', (err, results) => {\n  if (err) throw err;\n  console.log(results);\n});\n\nmysqlRedis.query('SELECT * FROM countries', (err, results) => {\n  // This call will return cached result from Redis\n  console.log(results);\n});\n\nmysqlRedis.close(); // close both connections","lang":"javascript","description":"Shows synchronous callback usage of MysqlRedis with mysql2 and redis. Cache options are optional."},"warnings":[{"fix":"Install farmhash as a separate dependency: npm install farmhash. Or switch to md5 hash type.","message":"farmhash is now optional as of v0.7. If you were using farmhash hash types without explicitly installing farmhash, your code will break.","severity":"breaking","affected_versions":">=0.7.0"},{"fix":"Ensure you only use MySQLRedis.query() for SELECT statements. For writes, use the native MySQL connection directly.","message":"Only SELECT queries are cached. INSERT, UPDATE, DELETE queries are passed through to MySQL without caching.","severity":"gotcha","affected_versions":"*"},{"fix":"Manually delete or update the Redis keys when data changes, or use a TTL and accept eventual consistency.","message":"Cache is never invalidated on writes. Data can become stale if underlying MySQL data changes.","severity":"gotcha","affected_versions":"*"},{"fix":"Use md5 (default) or blake2b512 for collision-safe hashing, especially with millions of queries.","message":"farmhash32 and farmhash64 hash types use non-cryptographic hashing which may collide with many queries.","severity":"deprecated","affected_versions":"*"},{"fix":"Use require() or use a dynamic import with module.createRequire.","message":"The library does not support ESM imports. Using import will fail.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"search_vec":"'0.7.2':27 '2018':31 'algorithm':37 'back':71 'blake2b512':40 'cach':5,15,87,95,96 'configur':36 'data':69,83 'databas':23 'depend':54 'design':58 'due':84 'extern':53 'fall':70 'farmhash':55 'farmhash32/64':39 'frequent':81 'full':42 'hash':33 'heavi':62 'ioredi':93 'javascript':89 'last':28 'layer':6,97 'load':24 'mandatori':52 'md5':38 'mysql':2,8,73,92 'mysql-redi':1 'mysql/mysql2':9 'mysql2':91 'node':90 'option':57 'potenti':86 'queri':17,34,43 'rare':67 'read':61 'read-heavi':60 'redi':3,11,20,48,75,94 'redis/ioredis':12 'reduc':22 'result':18,46 'select':16 'stabl':25 'stale':88 'static':65 'store':45 'suitabl':79 'transpar':14 'ttl':50 'unavail':77 'updat':29,68,82 'use':35 'version':26 'workload':63 'zero':51","created_at":"2026-06-05T16:59:20.447172+00:00","updated_at":"2026-06-05T16:59:20.447172+00:00","problems":[{"fix":"Install farmhash: npm install farmhash, or change hashType to 'md5'.","cause":"Using farmhash32 or farmhash64 hash type without installing farmhash package.","error":"Cannot find module 'farmhash'"},{"fix":"Use const { MysqlRedis } = require('mysql-redis');","cause":"Importing the package incorrectly (e.g., using default import or wrong destructuring).","error":"TypeError: MysqlRedis is not a constructor"},{"fix":"Ensure Redis is running on localhost:6379 or provide correct connection options to redis.createClient.","cause":"Redis server is not running or connection options are incorrect.","error":"Error: Redis connection failed"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/kwalski/mysql-redis#readme","github":"https://github.com/kwalski/mysql-redis","docs":null,"changelog":null,"pypi":null,"npm":"mysql-redis","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}