< Homepage

DM Puzzle - Use Node to proxify requests to an API and cache them in Redis

Objective

Have you ever wanted to convert your very slow and resistant to change website/blog with tons of content to something really cool using modern technos (like mobile app or API centric web app)? We'll try to do that.

Let's build a cache server using NodeJS. The main idea is: when a request arrives, check if it is already cached, if yes, render it immediately, if not, proxify the request and store (cache) the content in your cache server, and then, render the result. Simple, isn't it? Check this article for more info about the idea.

The steps:

  1. Create your Node server
  2. Build your routes using ExpressJS
  3. Create the caching logic:
  4. Create the rendering logic
  5. BONUS 1: Create a method to manually flush a specific cache key
  6. BONUS 2: Add an expiration time to your cache keys
  7. BONUS 3: Maybe global settings could be better for your cache and proxy
Example:

You can use this simple API if you don't know which one to use:

  • http://cultiz.com/api/get_posts/ : list of articles (oyu can use ?count= to limit the number of items)
  • http://cultiz.com/api/get_post/?post_id=23668 : get 1 article (by ID or by slug: ?post_slug=)

When I go to http://localhost:1234/get_posts I should get the JSON result I asked for, not cached the first (~4 s response time), cached then (~40 ms response time).

How

Prerequisites

Instructions

  1. Keep it fun and simple!
  2. Do it quick and dirty! Do not reach for perfection (you know what I mean)!
  3. Do not spend more than the expected time. If you're having a hard time ask for help or have a look at the solution.
  4. Have fun and if you want to, read more about the puzzle subject once you're done.
    Curiosity is the path to knowledge.

To be resolved before

February 26 2015

Expected time to resolution

Around 2 hours

Expected number of lines

Less than 200

Lost?

  • GET and DELETE routes should be enough for this puzzle

  • Redis need to be installed and the server should run ;) > redis-server.

    You can also test what has been stored using the Redis client: > redis-cli & > KEYS *.

  • In Redis, you can just use GET, SET, EXISTS and DEL commands like this redisClient.set(key, value, function(error){ }).

  • If you have trouble setting a new key in Redis, you can use "crypto" to encode the requested URL into a unique string.

Definitely lost?

Show solution