Giter VIP home page Giter VIP logo

caches's Introduction

Caching with Redis

Getting started

  • Clone this repo and change directory to the repo.
  • Pull queues virtual machine image which has nodejs and redis:
    bakerx pull CSC-DevOps/Images#Spring2020 queues
    
  • Create a new virtual machine using the queues image:
    bakerx run queues queues --ip 192.168.44.81 --sync
  • Run bakerx ssh queues to connect to the virtual machine.

Basics

Inside the VM, go to the sync folder containing this repo and install npm dependencies:

cd /bakerx/basics
npm install

Task 1: An expiring cache

We create two routes, /get and /set.

When /set is visited (i.e. GET request), set a new key, with the value:

"this message will self-destruct in 10 seconds".

Use the EXPIRE command to make sure this key will expire in 10 seconds.

When /get is visited (i.e. GET request), fetch that key, and send its value back to the client: res.send(value).

Task 2: Recent visited sites

We create a new route, /recent, which will display the most recently visited sites.

We use LPUSH, LTRIM, andLRANGE redis commands to store the most recent 5 sites visited, and return that to the client.

Meow.io

To run the application, perform the following steps:

# Setup app
cd meow.io
npm install 
node data/init.js

# Start server
npm start

You should be able to visit http://192.168.44.81:3000/

Task 3: Cache best facts calculation

The front page will load all cat facts and display the 100 most voted facts on each page load.

Without caching, this can add up with heavier traffic:

$ time ./load.sh 

real	1m53.098s

However, if we cache the results, we greatly reduce the load:

$ time ./load.sh 

real	0m47.018s

Note: This is making an explicit trade-off between availability and consistency, since displayed data will be potentially 10 seconds behind real scores.

Task 4: Cat picture uploads storage

The front page will display the 5 most recently uploaded files (/upload). You can use curl to help you upload files easily for test.

curl -F "image=@./data/morning.jpg" http://localhost:3000/upload

However, this is being read from the database on each page load. You could instead simply store the 5 most recently uploaded files in a cache without reading from the database.

we have modified the meow.io/routes/upload.js file to cache recently uploaded images. Modify the meow.io/routes/index.js to read from the cache instead the database.

We have also modified load.sh to upload some images to meow.io database:

#!/bin/bash
for i in {1..50}
do
    
    seq 1 10 | curl -F "image=@./data/i-scream.jpg" localhost:3000/upload
    seq 5 11 | xargs -I {} -n1 -P4 curl -s localhost:3000/ >/dev/null

done

Response time without caching:

$ time ./load.sh 

real	1m48.405s

After caching:

$ time ./load.sh 

real	0m30.164s

Task 5: Regulate uploads with queue

If the application receives large volume of uploads faster that the database can handle perforance will be very poor.

  • We have modified the meow.io/routes/upload.js to store incoming images in a queue and not the database.
  • We have then modified meow.io/app.js to pop images stored in the queue (consider using LPOP ) and save in the database. This is done every 100ms using setInveral(). This way, we can take advantage of the faster write speed for redis and drain the queue at a steady rate for longer term storage.

Conceptual Questions

1. Describe three desirable properties for infrastructure.

  • Availability: It means that the system or infrastructure should be able to provide services to the user when needed with no or limited iterruption.

  • Isolation: It means that if one component of the infrastructure is affected by some problem, the impact is confined to that component alone and it wouldn't bring the entire infrastructure down.

  • Scalability: It means that the system will be able to handle a growing amount of work by adding more resources to the system. It implies that the system can meet a growing demand from the users.

2. Describe some benefits and issues related to using Load Balancers.

  • Benefits:

    -> They ensure availability.

    -> They mitigate single point of failure.

    -> They ensure scalability.

  • Issues:

    -> Problems could arise if the load balancer itself goes down, but it is generally a low risk.

3. What are some reasons for keeping servers in seperate availability zones?

  • By keeping servers in separate availability zones we ensure isolation. If a particular zone is down then only servers in that region will go down.
  • This in turn increases the robustness of our system and ensures greater availability.

4. Describe the Circuit Breaker and Bulkhead pattern.

  • Circuit Breaker pattern: This pattern suggests that you need to wrap a protected function call in a circuit breaker object. This object will track the number of failures that occur. If that number exceeds a threshold, the circuit breaker will berak the connection to that protected function and all further calls to the curcuit breaker will be returned an error without calling the protected function at all.

  • Bulkhead pattern: They are a way to partition an application. They enable isolation through concrete enforceable resource limits. They effectively isolate components and protect from cascading failures through the enforcement of limits.

Screencast

caches's People

Contributors

raveenaaa avatar chrisparnin avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.