Skip to content

Instantly share code, notes, and snippets.

@twalker
Last active September 12, 2018 20:45
Show Gist options
  • Save twalker/9899e5488618ecbb12efe66d33763378 to your computer and use it in GitHub Desktop.
Save twalker/9899e5488618ecbb12efe66d33763378 to your computer and use it in GitHub Desktop.
tech-alignment-redis-retry-strategy.md

Problem

A hard dependency on redis servers being available, with no way to implement a fallback if they're not.

The refresh token requests are dependent on our redis servers being available to store tokens (#3237) . If the redis server is down, refresh token requests will not complete. PR: https://scm.starbucks.com/starbucks-web/core/pull/3469

Solution

Inform consumers of the redis being in a faulted state (cannot connect, redis crashed, redis out of memory, servers not available, etc.), so they can implement a fallback strategy. e.g. If redis is down, go directly to the api for data that was expected to be in cache.

Demo

pane 1: start-redis

pane 2: redis-cli

pane 3: NODE_CONFIG_DIR=../config/ node --inspect ./cache-client-test.js | grep "CONSUMER" NODE_CONFIG_DIR=../config/ node --inspect ./cache-client-test.js | ../node_modules/.bin/bunyan

Show before, and after.

Usage

const cacheClient = require('./shared/server/lib/cache-client');

const getMyData = () => {
  return cacheClient.get('mykey')
    .then(reply => {
      // Cached data from a healthy redis-server
      return reply;
    })
    .catch(err => {
      if (cacheClient.isCacheError(err)) {
        // redis server is in a faulted state, fallback to an api call
        return fetch('api/my-data');
      }
      // Error isn't redis-server related, so do something else or rethrow.
      throw error;
    });
}

https://scm.starbucks.com/starbucks-web/core/pull/3469/files#diff-166da7076eb927327b2a41a3b7035fc5R247

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment