├── .gitignore ├── package.json ├── readme.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redis-connection-benchmark", 3 | "version": "1.0.0", 4 | "description": "Benchmark the overhead of creating a Redis connection", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC" 11 | } 12 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Are Redis Connections Really Lightweight? 2 | 3 | This benchmark test compare the latency numbers of two approaches: 4 | 5 | 1- EPHEMERAL CONNECTIONS: We do not reuse the connection. Instead we create a new connection for each command and close the connection immediately. We record the latency of client creation, ping() and client.quit() together. See the `bench()` method in the code. 6 | 7 | 2- REUSE CONNECTIONS: We create a connection once and reuse the same connection for all commands. Here, we record the latency of `ping()` operation. See the `benchReuse()` method. 8 | 9 | See [the blog post](https://blog.upstash.com/serverless-database-connections). 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { performance } = require("perf_hooks"); 2 | const hdr = require("hdr-histogram-js"); 3 | const Redis = require('ioredis') 4 | 5 | const delay = ms => new Promise(resolve => setTimeout(resolve, ms)) 6 | 7 | const options = { 8 | host: 'END_POINT', 9 | port: 6379, 10 | // password: '' 11 | }; 12 | 13 | const total = 10000; 14 | 15 | async function benchReuse() { 16 | const client = new Redis(options); 17 | const hist = hdr.build(); 18 | for (let index = 0; index < total; index++) { 19 | let start = performance.now() * 1000 // to μs 20 | client.ping() 21 | let end = performance.now() * 1000 // to μs 22 | hist.recordValue(end-start) 23 | await delay(10) 24 | } 25 | client.quit() 26 | console.log(hist.outputPercentileDistribution(1, 1)); 27 | } 28 | 29 | async function bench() { 30 | const hist = hdr.build(); 31 | for (let index = 0; index < total; index++) { 32 | let start = performance.now() * 1000 // to μs 33 | const client = new Redis(options); 34 | client.ping() 35 | client.quit() 36 | let end = performance.now() * 1000 // to μs 37 | hist.recordValue(end-start) 38 | await delay(10) 39 | } 40 | console.log(hist.outputPercentileDistribution(1, 1)); 41 | } 42 | 43 | bench(); --------------------------------------------------------------------------------