52
javier ramirez @supercoco9 API Analytics with Redis and Google Bigquery

api analytics redis bigquery. Lrug

Embed Size (px)

DESCRIPTION

At teowaki we have a system for API usage analytics, with Redis as a fast intermediate store and bigquery as a big data backend. As a result, we can launch aggregated queries on our traffic/usage data in just a few seconds and we can try and find for usage patterns that wouldn’t be obvious otherwise. In this session I will talk about how we entered the Big Data world, which alternatives we evaluated, and how we are using Redis and Bigquery to solve our problem.

Citation preview

Page 1: api analytics redis bigquery. Lrug

javier ramirez@supercoco9

API Analytics with Redis

and Google Bigquery

Page 2: api analytics redis bigquery. Lrug
Page 3: api analytics redis bigquery. Lrug
Page 4: api analytics redis bigquery. Lrug
Page 5: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

REST API +

AngularJS web as an API client

Page 6: api analytics redis bigquery. Lrug
Page 7: api analytics redis bigquery. Lrug

obvious solution:

use a ready-made service as 3scale or apigee

javier ramirez @supercoco9 https://teowaki.com

Page 8: api analytics redis bigquery. Lrug

1. non intrusive metrics2. keep the history3. avoid vendor lock-in4. interactive queries5. cheap6. extra ball: real time

javier ramirez @supercoco9 https://teowaki.com

Page 9: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

Page 10: api analytics redis bigquery. Lrug

data that’s an order of magnitude greater than data you’re accustomed to

javier ramirez @supercoco9 https://teowaki.com

Doug Laney VP Research, Business Analytics and Performance Management at Gartner

Page 11: api analytics redis bigquery. Lrug

data that exceeds the processing capacity of conventional database systems. The data is too big, moves too fast, or doesn’t fit the structures of your database architectures.

Ed Dumbill program chair for the O’Reilly Strata Conference

javier ramirez @supercoco9 https://teowaki.com

Page 12: api analytics redis bigquery. Lrug

bigdata is doing a fullscan to 330MM rows, matching them against a regexp, and getting the result (223MM rows) in just 5 seconds

javier ramirez @supercoco9 https://teowaki.com

Javier Ramirezimpresionable teowaki founder

Page 13: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

Page 14: api analytics redis bigquery. Lrug

Intel(R) Xeon(R) CPU E5520 @ 2.27GHz (with pipelining)

$ ./redis-benchmark -r 1000000 -n 2000000 -t get,set,lpush,lpop -P 16 -q

SET: 552,028 requests per secondGET: 707,463 requests per secondLPUSH: 767,459 requests per secondLPOP: 770,119 requests per second

Intel(R) Xeon(R) CPU E5520 @ 2.27GHz (without pipelining)$ ./redis-benchmark -r 1000000 -n 2000000 -t get,set,lpush,lpop -q

SET: 122,556 requests per secondGET: 123,601 requests per secondLPUSH: 136,752 requests per secondLPOP: 132,424 requests per second

javier ramirez @supercoco9 https://teowaki.com

Page 15: api analytics redis bigquery. Lrug

open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

http://redis.io

started in 2009 by Salvatore Sanfilippo @antirez

100 contributors at https://github.com/antirez/redis

javier ramirez @supercoco9 https://teowaki.com

Page 16: api analytics redis bigquery. Lrug

what is it used for

javier ramirez @supercoco9 https://teowaki.com

Page 17: api analytics redis bigquery. Lrug

twitter

Every time line (800 tweets per user) is stored in redis

5000 writes per second avg300K reads per second

javier ramirez @supercoco9 https://teowaki.com

Page 18: api analytics redis bigquery. Lrug

nginx + lua + redis

apache + mruby + redis

javier ramirez @supercoco9 https://teowaki.com

Page 19: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

Page 20: api analytics redis bigquery. Lrug

Redis keeps

everything in memory all the time

javier ramirez @supercoco9 https://teowaki.com

Page 21: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

Page 22: api analytics redis bigquery. Lrug

easy: store GZIPPED files into S3/Glacier

javier ramirez @supercoco9 https://teowaki.com

* we are moving to google cloud now

Page 23: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

Page 24: api analytics redis bigquery. Lrug

Hadoop (map/reduce)

javier ramirez @supercoco9 https://teowaki.com

http://hadoop.apache.org/

started in 2005 by Doug Cutting and Mike Cafarella

Page 25: api analytics redis bigquery. Lrug

cassandra

javier ramirez @supercoco9 https://teowaki.com

http://cassandra.apache.org/

released in 2008 by facebook.

Page 26: api analytics redis bigquery. Lrug

other big data solutions:

hadoop+voldemort+kafka

hbase

javier ramirez @supercoco9 https://teowaki.com

http://engineering.linkedin.com/projects

http://hbase.apache.org/

Page 27: api analytics redis bigquery. Lrug

Amazon Redshift

javier ramirez @supercoco9 https://teowaki.com

http://aws.amazon.com/redshift/

Page 28: api analytics redis bigquery. Lrug

Our choice:

google bigquery

Data analysis as a service

http://developers.google.com/bigquery

javier ramirez @supercoco9 https://teowaki.com

Page 29: api analytics redis bigquery. Lrug

Based on Dremel

Specifically designed for interactive queries over petabytes of real-time data

javier ramirez @supercoco9 https://teowaki.com

Page 30: api analytics redis bigquery. Lrug

Columnar storage

Easy to compress

Convenient for querying long series over a single column

javier ramirez @supercoco9 https://teowaki.com

Page 31: api analytics redis bigquery. Lrug

loading data

You can feed flat CSV files or nested JSON objects

javier ramirez @supercoco9 https://teowaki.com

Page 32: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

bq cli

bq load --nosynchronous_mode --encoding UTF-8 --field_delimiter 'tab' --max_bad_records 100 --source_format CSV api.stats 20131014T11-42-05Z.gz

Page 33: api analytics redis bigquery. Lrug

web console screenshot

javier ramirez @supercoco9 https://teowaki.com

Page 34: api analytics redis bigquery. Lrug

almost SQL

javier ramirez @supercoco9 https://teowaki.com

selectfromjoinwheregroup byhavingorderlimit

avgcountmaxminsum

+-*/%

&|^<<>>~

=!=<>><>= <=INIS NULLBETWEEN

ANDORNOT

Page 35: api analytics redis bigquery. Lrug

Functions overview

javier ramirez @supercoco9 https://teowaki.com

current_datecurrent_timenowdatediffdayday_of_weekday_of_yearhourminutequarteryear...

absacosatanceilfloordegreesloglog2log10PISQRT...

concatcontainsleftlengthlowerupperlpadrpadrightsubstr

Page 36: api analytics redis bigquery. Lrug

analytics specific extensions

javier ramirez @supercoco9 https://teowaki.com

withinflattennest

stddev

topfirstlastnth

variance

var_popvar_samp

covar_popcovar_samp

quantiles

Page 37: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

window functions

Page 38: api analytics redis bigquery. Lrug

correlations

javier ramirez @supercoco9 https://teowaki.com

Page 39: api analytics redis bigquery. Lrug

Things you always wanted to try but were too scare to

javier ramirez @supercoco9 https://teowaki.com

select count(*) from publicdata:samples.wikipedia

where REGEXP_MATCH(title, "[0-9]*") AND wp_namespace = 0;

223,163,387Query complete (5.6s elapsed, 9.13 GB processed, Cost: 32¢)

Page 40: api analytics redis bigquery. Lrug

SELECT repository_name, repository_language, repository_description, COUNT(repository_name) as cnt,repository_urlFROM github.timelineWHERE type="WatchEvent"AND PARSE_UTC_USEC(created_at) >= PARSE_UTC_USEC("#{yesterday} 20:00:00")AND repository_url IN (

SELECT repository_urlFROM github.timelineWHERE type="CreateEvent"AND PARSE_UTC_USEC(repository_created_at) >= PARSE_UTC_USEC('#{yesterday} 20:00:00')AND repository_fork = "false"AND payload_ref_type = "repository"GROUP BY repository_url

)GROUP BY repository_name, repository_language, repository_description, repository_urlHAVING cnt >= 5ORDER BY cnt DESCLIMIT 25

Page 41: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

country segmented traffic

Page 42: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

our most active user

Page 43: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 https://teowaki.com

10 request we should be caching

Page 44: api analytics redis bigquery. Lrug

javier ramirez @supercoco9 http://teowaki.com

5 most created resources

Page 45: api analytics redis bigquery. Lrug

redis pricing

2* machines (master/slave) at digital ocean

$10 monthly

* we were already using these instances for a lot of redis use cases

javier ramirez @supercoco9 https://teowaki.com

Page 46: api analytics redis bigquery. Lrug

s3 pricing

$0.095 per GB

a gzipped 1.6 MB file stores 300K rows

$0.0001541698 / monthly

javier ramirez @supercoco9 https://teowaki.com

Page 47: api analytics redis bigquery. Lrug

glacier pricing

$0.01 per GB

$0.000016 / monthly

javier ramirez @supercoco9 https://teowaki.com

Page 48: api analytics redis bigquery. Lrug

bigquery pricing

$80 per stored TB300000 rows => $0.007629392 / month

$35 per processed TB1 full scan = 84 MB1 count = 0 MB1 full scan over 1 column = 5.4 MB10 GB => $0.35 / month

javier ramirez @supercoco9 https://teowaki.com

Page 49: api analytics redis bigquery. Lrug

redis $10.0000000000s3 storage $00.0001541698s3 transfer $00.0050000000

glacier transfer $00.0500000000glacier storage $00.0000160000bigquery storage $00.0076293920bigquery queries $00.3500000000

$10.41 / monthfor our first 330000 rows

javier ramirez @supercoco9 https://teowaki.com

Page 50: api analytics redis bigquery. Lrug

1. non intrusive metrics2. keep the history3. avoid vendor lock-in4. interactive queries5. cheap6. extra ball: real time

javier ramirez @supercoco9 https://teowaki.com

Page 51: api analytics redis bigquery. Lrug
Page 52: api analytics redis bigquery. Lrug

Find related links at

https://teowaki.com/teams/javier-community/link-categories/bigquery-talk

Cheers!

Javier Ramírez@supercoco9