SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Redis and Python

    by Josiah Carlson
        @dr_josiah
 dr-josiah.blogspot.com
  bit.ly/redis-in-action
Redis and Python;
 It's PB & J time
     by Josiah Carlson
         @dr_josiah
  dr-josiah.blogspot.com
   bit.ly/redis-in-action
What will be covered
•   Who am I?
•   What is Redis?
•   Why Redis with Python?
•   Cool stuff you can do by combining them
Who am I?
•   A Python user for 12+ years
•   Former python-dev bike-shedder
•   Former maintainer of Python async sockets libraries
•   Author of a few small OS projects
    o   rpqueue, parse-crontab, async_http, timezone-utils, PyPE

•   Worked at some cool places you've never heard of
    (Networks In Motion, Ad.ly)
•   Cool places you have (Google)
•   And cool places you will (ChowNow)
•   Heavy user of Redis
•   Author of upcoming Redis in Action
What is Redis?
•   In-memory database/data structure server
     o Limited to main memory; vm and diskstore defunct
•   Persistence via snapshot or append-only file
•   Support for master/slave replication (multiple slaves
    and slave chaining supported)
     o No master-master, don't even try
     o Client-side sharding
     o Cluster is in-progress
•   Five data structures + publish/subscribe
     o Strings, Lists, Sets, Hashes, Sorted Sets (ZSETs)
•   Server-side scripting with Lua in Redis 2.6
What is Redis? (compared to other
databases/caches)
• Memcached
  o in-memory, no-persistence, counters, strings, very fast, multi-threaded

• Redis
  o in-memory, optionally persisted, data structures, very fast, server-side
    scripting, single-threaded
• MongoDB
  o on-disk, speed inversely related to data integrity, bson, master/slave,
    sharding, multi-master, server-side mapreduce, database-level locking
• Riak
  o on-disk, pluggable data stores, multi-master sharding, RESTful API,
    server-side map-reduce, (Erlang + C)
• MySQL/PostgreSQL
  o on-disk/in-memory, pluggable data stores, master/slave, sharding,
    stored procedures, ...
What is Redis? (Strings)
•   Really scalars of a few different types
    o   Character strings
          concatenate values to the end
          get/set individual bits
          get/set byte ranges
    o   Integers (platform long int)
          increment/decrement
          auto "casting"
    o   Floats (IEEE 754 FP Double)
          increment/decrement
          auto "casting"
What is Redis? (Lists)
•   Doubly-linked list of character strings
    o   Push/pop from both ends
    o   [Blocking] pop from multiple lists
    o   [Blocking] pop from one list, push on another
    o   Get/set/search for item in a list
    o   Sortable
What is Redis? (Sets)
•   Unique unordered sequence of character
    strings
    o   Backed by a hash table
    o   Add, remove, check membership, pop, random pop
    o   Set intersection, union, difference
    o   Sortable
What is Redis? (Hashes)
•   Key-value mapping inside a key
    o   Get/Set/Delete single/multiple
    o   Increment values by ints/floats
    o   Bulk fetch of Keys/Values/Both
    o   Sort-of like a small version of Redis that only
        supports strings/ints/floats
What is Redis? (Sorted Sets -
ZSETs)
•   Like a Hash, with 'members' and 'scores',
    scores limited to float values
    o   Get, set, delete, increment
    o   Can be accessed by the sorted order of the
        (score,member) pair
          By score
          By index
What is Redis? (Publish/Subscribe)
•   Readers subscribe to "channels" (exact
    strings or patterns)
•   Writers publish to channels, broadcasting to
    all subscribers
•   Messages are transient
Why Redis with Python?
•   The power of Python lies in:
    o   Reasonably sane syntax/semantics
    o   Easy manipulation of data and data structures
    o   Large and growing community
•   Redis also has:
    o   Reasonably sane syntax/semantics
    o   Easy manipulation of data and data structures
    o   Medium-sized and growing community
    o   Available as remote server
         Like a remote IPython, only for data
         So useful, people have asked for a library version
Per-hour and Per-day hit counters
from itertools import imap
import redis
def process_lines(prefix, logfile):
   conn = redis.Redis()
   for log in imap(parse_line, open(logfile, 'rb')):
       time = log.timestamp.isoformat()
       hour = time.partition(':')[0]
       day = time.partition('T')[0]
       conn.zincrby(prefix + hour, log.path)
       conn.zincrby(prefix + day, log.path)
       conn.expire(prefix + hour, 7*86400)
       conn.expire(prefix + day, 30*86400)
Per-hour and Per-day hit counters
(with pipelines for speed)
from itertools import imap
import redis
def process_lines(prefix, logfile):
    pipe = redis.Redis().pipeline(False)
    for i, log in enumerate(imap(parse_line, open(logfile, 'rb'))):
        time = log.timestamp.isoformat()
        hour = time.partition(':')[0]
        day = time.partition('T')[0]
        pipe.zincrby(prefix + hour, log.path)
        pipe.zincrby(prefix + day, log.path)
        pipe.expire(prefix + hour, 7*86400)
        pipe.expire(prefix + day, 30*86400)
        if not i % 1000:
               pipe.execute()
    pipe.execute()
Simple task queue - add/run items
import json
import redis


def add_item(queue, name, *args, **kwargs):
   redis.Redis().rpush(queue,
       json.dumps([name, args, kwargs]))


def execute_one(queues):
   item = redis.Redis().blpop(queues, 30)
   name, args, kwargs = json.loads(item)
   REGISTRY[name](*args, **kwargs)
Simple task queue - register tasks
REGISTRY = {}
def task(queue):
    def wrapper(function):
        def defer(*args, **kwargs):
            add_item(queue, name, *args, **kwargs)
        name = function.__name__
        if name in REGISTRY:
            raise Exception(
                   "Duplicate callback %s"%(name,))
        REGISTRY[name] = function
        return defer
    if isinstance(queue, str):
        return wrapper
    function, queue = queue, 'default'
    return wrapper(function)
Simple task queue – register tasks
@task('high')
def do_something(arg):
    pass


@task
def do_something_else(arg):
    pass
Cool stuff to do...
•    Reddit                •   Publish/Subscribe
•    Caching               •   Messaging
•    Cookies               •   Search engines
•    Analytics             •   Ad targeting
•    Configuration         •   Twitter
     management            •   Chat rooms
•    Autocomplete          •   Job search
•    Distributed locks     •   ...
•    Counting Semaphores
•    Task queues
Thank you
      @dr_josiah
dr-josiah.blogspot.com
 bit.ly/redis-in-action



     Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Hypertable
HypertableHypertable
Hypertablebetaisao
 
Hadoop Cluster Configuration and Data Loading - Module 2
Hadoop Cluster Configuration and Data Loading - Module 2Hadoop Cluster Configuration and Data Loading - Module 2
Hadoop Cluster Configuration and Data Loading - Module 2Rohit Agrawal
 
SDEC2011 NoSQL concepts and models
SDEC2011 NoSQL concepts and modelsSDEC2011 NoSQL concepts and models
SDEC2011 NoSQL concepts and modelsKorea Sdec
 
PostgreSQL and Sphinx pgcon 2013
PostgreSQL and Sphinx   pgcon 2013PostgreSQL and Sphinx   pgcon 2013
PostgreSQL and Sphinx pgcon 2013Emanuel Calvo
 
Cassandra and Spark, closing the gap between no sql and analytics codemotio...
Cassandra and Spark, closing the gap between no sql and analytics   codemotio...Cassandra and Spark, closing the gap between no sql and analytics   codemotio...
Cassandra and Spark, closing the gap between no sql and analytics codemotio...Duyhai Doan
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to RedisRizky Abdilah
 
Practical Hadoop using Pig
Practical Hadoop using PigPractical Hadoop using Pig
Practical Hadoop using PigDavid Wellman
 
PG Day'14 Russia, Работа со слабо-структурированными данными в PostgreSQL, Ол...
PG Day'14 Russia, Работа со слабо-структурированными данными в PostgreSQL, Ол...PG Day'14 Russia, Работа со слабо-структурированными данными в PostgreSQL, Ол...
PG Day'14 Russia, Работа со слабо-структурированными данными в PostgreSQL, Ол...pgdayrussia
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redisjimbojsb
 
OCF.tw's talk about "Introduction to spark"
OCF.tw's talk about "Introduction to spark"OCF.tw's talk about "Introduction to spark"
OCF.tw's talk about "Introduction to spark"Giivee The
 
Why hadoop map reduce needs scala, an introduction to scoobi and scalding
Why hadoop map reduce needs scala, an introduction to scoobi and scaldingWhy hadoop map reduce needs scala, an introduction to scoobi and scalding
Why hadoop map reduce needs scala, an introduction to scoobi and scaldingXebia Nederland BV
 
MongoDB Advanced Topics
MongoDB Advanced TopicsMongoDB Advanced Topics
MongoDB Advanced TopicsCésar Rodas
 
Avro Data | Washington DC HUG
Avro Data | Washington DC HUGAvro Data | Washington DC HUG
Avro Data | Washington DC HUGCloudera, Inc.
 
Intro To Cascading
Intro To CascadingIntro To Cascading
Intro To CascadingNate Murray
 
Parquet and impala overview external
Parquet and impala overview externalParquet and impala overview external
Parquet and impala overview externalmattlieber
 
Fast track to getting started with DSE Max @ ING
Fast track to getting started with DSE Max @ INGFast track to getting started with DSE Max @ ING
Fast track to getting started with DSE Max @ INGDuyhai Doan
 
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAYPostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAYEmanuel Calvo
 

Was ist angesagt? (20)

Hypertable
HypertableHypertable
Hypertable
 
Hadoop Cluster Configuration and Data Loading - Module 2
Hadoop Cluster Configuration and Data Loading - Module 2Hadoop Cluster Configuration and Data Loading - Module 2
Hadoop Cluster Configuration and Data Loading - Module 2
 
SDEC2011 NoSQL concepts and models
SDEC2011 NoSQL concepts and modelsSDEC2011 NoSQL concepts and models
SDEC2011 NoSQL concepts and models
 
Avro intro
Avro introAvro intro
Avro intro
 
PostgreSQL and Sphinx pgcon 2013
PostgreSQL and Sphinx   pgcon 2013PostgreSQL and Sphinx   pgcon 2013
PostgreSQL and Sphinx pgcon 2013
 
Cassandra and Spark, closing the gap between no sql and analytics codemotio...
Cassandra and Spark, closing the gap between no sql and analytics   codemotio...Cassandra and Spark, closing the gap between no sql and analytics   codemotio...
Cassandra and Spark, closing the gap between no sql and analytics codemotio...
 
Hadoop
HadoopHadoop
Hadoop
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Practical Hadoop using Pig
Practical Hadoop using PigPractical Hadoop using Pig
Practical Hadoop using Pig
 
PG Day'14 Russia, Работа со слабо-структурированными данными в PostgreSQL, Ол...
PG Day'14 Russia, Работа со слабо-структурированными данными в PostgreSQL, Ол...PG Day'14 Russia, Работа со слабо-структурированными данными в PostgreSQL, Ол...
PG Day'14 Russia, Работа со слабо-структурированными данными в PostgreSQL, Ол...
 
Ruby on hadoop
Ruby on hadoopRuby on hadoop
Ruby on hadoop
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
OCF.tw's talk about "Introduction to spark"
OCF.tw's talk about "Introduction to spark"OCF.tw's talk about "Introduction to spark"
OCF.tw's talk about "Introduction to spark"
 
Why hadoop map reduce needs scala, an introduction to scoobi and scalding
Why hadoop map reduce needs scala, an introduction to scoobi and scaldingWhy hadoop map reduce needs scala, an introduction to scoobi and scalding
Why hadoop map reduce needs scala, an introduction to scoobi and scalding
 
MongoDB Advanced Topics
MongoDB Advanced TopicsMongoDB Advanced Topics
MongoDB Advanced Topics
 
Avro Data | Washington DC HUG
Avro Data | Washington DC HUGAvro Data | Washington DC HUG
Avro Data | Washington DC HUG
 
Intro To Cascading
Intro To CascadingIntro To Cascading
Intro To Cascading
 
Parquet and impala overview external
Parquet and impala overview externalParquet and impala overview external
Parquet and impala overview external
 
Fast track to getting started with DSE Max @ ING
Fast track to getting started with DSE Max @ INGFast track to getting started with DSE Max @ ING
Fast track to getting started with DSE Max @ ING
 
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAYPostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
 

Andere mochten auch

Andere mochten auch (7)

Redis persistence in practice
Redis persistence in practiceRedis persistence in practice
Redis persistence in practice
 
Multiprocessing with python
Multiprocessing with pythonMultiprocessing with python
Multiprocessing with python
 
Python in Action (Part 2)
Python in Action (Part 2)Python in Action (Part 2)
Python in Action (Part 2)
 
Redis for the Everyday Developer
Redis for the Everyday DeveloperRedis for the Everyday Developer
Redis for the Everyday Developer
 
Python in Action (Part 1)
Python in Action (Part 1)Python in Action (Part 1)
Python in Action (Part 1)
 
Generators: The Final Frontier
Generators: The Final FrontierGenerators: The Final Frontier
Generators: The Final Frontier
 
An Introduction to Python Concurrency
An Introduction to Python ConcurrencyAn Introduction to Python Concurrency
An Introduction to Python Concurrency
 

Ähnlich wie Python redis talk

Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)Itamar Haber
 
Hadoop with Python
Hadoop with PythonHadoop with Python
Hadoop with PythonDonald Miner
 
H2O World - What's New in H2O with Cliff Click
H2O World - What's New in H2O with Cliff ClickH2O World - What's New in H2O with Cliff Click
H2O World - What's New in H2O with Cliff ClickSri Ambati
 
Redis Modules - Redis India Tour - 2017
Redis Modules - Redis India Tour - 2017Redis Modules - Redis India Tour - 2017
Redis Modules - Redis India Tour - 2017HashedIn Technologies
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to pythonActiveState
 
Presto updates to 0.178
Presto updates to 0.178Presto updates to 0.178
Presto updates to 0.178Kai Sasaki
 
Hadoop for the Absolute Beginner
Hadoop for the Absolute BeginnerHadoop for the Absolute Beginner
Hadoop for the Absolute BeginnerIke Ellis
 
Tajolabigdatacamp2014 140618135810-phpapp01 hyunsik-choi
Tajolabigdatacamp2014 140618135810-phpapp01 hyunsik-choiTajolabigdatacamp2014 140618135810-phpapp01 hyunsik-choi
Tajolabigdatacamp2014 140618135810-phpapp01 hyunsik-choiData Con LA
 
Hadoop Overview & Architecture
Hadoop Overview & Architecture  Hadoop Overview & Architecture
Hadoop Overview & Architecture EMC
 
Why databases cry at night
Why databases cry at nightWhy databases cry at night
Why databases cry at nightMichael Yarichuk
 
Типы данных JSONb, соответствующие индексы и модуль jsquery – Олег Бартунов, ...
Типы данных JSONb, соответствующие индексы и модуль jsquery – Олег Бартунов, ...Типы данных JSONb, соответствующие индексы и модуль jsquery – Олег Бартунов, ...
Типы данных JSONb, соответствующие индексы и модуль jsquery – Олег Бартунов, ...Yandex
 
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander KorotkovPostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander KorotkovNikolay Samokhvalov
 
Big data week presentation
Big data week presentationBig data week presentation
Big data week presentationJoseph Adler
 
Big data, just an introduction to Hadoop and Scripting Languages
Big data, just an introduction to Hadoop and Scripting LanguagesBig data, just an introduction to Hadoop and Scripting Languages
Big data, just an introduction to Hadoop and Scripting LanguagesCorley S.r.l.
 
What is MariaDB Server 10.3?
What is MariaDB Server 10.3?What is MariaDB Server 10.3?
What is MariaDB Server 10.3?Colin Charles
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Henry S
 

Ähnlich wie Python redis talk (20)

Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)
 
Hadoop with Python
Hadoop with PythonHadoop with Python
Hadoop with Python
 
H2O World - What's New in H2O with Cliff Click
H2O World - What's New in H2O with Cliff ClickH2O World - What's New in H2O with Cliff Click
H2O World - What's New in H2O with Cliff Click
 
Redis Modules - Redis India Tour - 2017
Redis Modules - Redis India Tour - 2017Redis Modules - Redis India Tour - 2017
Redis Modules - Redis India Tour - 2017
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Apache hive
Apache hiveApache hive
Apache hive
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to python
 
Presto updates to 0.178
Presto updates to 0.178Presto updates to 0.178
Presto updates to 0.178
 
Hadoop for the Absolute Beginner
Hadoop for the Absolute BeginnerHadoop for the Absolute Beginner
Hadoop for the Absolute Beginner
 
Tajolabigdatacamp2014 140618135810-phpapp01 hyunsik-choi
Tajolabigdatacamp2014 140618135810-phpapp01 hyunsik-choiTajolabigdatacamp2014 140618135810-phpapp01 hyunsik-choi
Tajolabigdatacamp2014 140618135810-phpapp01 hyunsik-choi
 
04 standard class library c#
04 standard class library c#04 standard class library c#
04 standard class library c#
 
Hadoop Overview & Architecture
Hadoop Overview & Architecture  Hadoop Overview & Architecture
Hadoop Overview & Architecture
 
Why databases cry at night
Why databases cry at nightWhy databases cry at night
Why databases cry at night
 
Типы данных JSONb, соответствующие индексы и модуль jsquery – Олег Бартунов, ...
Типы данных JSONb, соответствующие индексы и модуль jsquery – Олег Бартунов, ...Типы данных JSONb, соответствующие индексы и модуль jsquery – Олег Бартунов, ...
Типы данных JSONb, соответствующие индексы и модуль jsquery – Олег Бартунов, ...
 
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander KorotkovPostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
 
Big data week presentation
Big data week presentationBig data week presentation
Big data week presentation
 
מיכאל
מיכאלמיכאל
מיכאל
 
Big data, just an introduction to Hadoop and Scripting Languages
Big data, just an introduction to Hadoop and Scripting LanguagesBig data, just an introduction to Hadoop and Scripting Languages
Big data, just an introduction to Hadoop and Scripting Languages
 
What is MariaDB Server 10.3?
What is MariaDB Server 10.3?What is MariaDB Server 10.3?
What is MariaDB Server 10.3?
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
 

Kürzlich hochgeladen

Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Kürzlich hochgeladen (20)

Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Python redis talk

  • 1. Redis and Python by Josiah Carlson @dr_josiah dr-josiah.blogspot.com bit.ly/redis-in-action
  • 2. Redis and Python; It's PB & J time by Josiah Carlson @dr_josiah dr-josiah.blogspot.com bit.ly/redis-in-action
  • 3. What will be covered • Who am I? • What is Redis? • Why Redis with Python? • Cool stuff you can do by combining them
  • 4. Who am I? • A Python user for 12+ years • Former python-dev bike-shedder • Former maintainer of Python async sockets libraries • Author of a few small OS projects o rpqueue, parse-crontab, async_http, timezone-utils, PyPE • Worked at some cool places you've never heard of (Networks In Motion, Ad.ly) • Cool places you have (Google) • And cool places you will (ChowNow) • Heavy user of Redis • Author of upcoming Redis in Action
  • 5. What is Redis? • In-memory database/data structure server o Limited to main memory; vm and diskstore defunct • Persistence via snapshot or append-only file • Support for master/slave replication (multiple slaves and slave chaining supported) o No master-master, don't even try o Client-side sharding o Cluster is in-progress • Five data structures + publish/subscribe o Strings, Lists, Sets, Hashes, Sorted Sets (ZSETs) • Server-side scripting with Lua in Redis 2.6
  • 6. What is Redis? (compared to other databases/caches) • Memcached o in-memory, no-persistence, counters, strings, very fast, multi-threaded • Redis o in-memory, optionally persisted, data structures, very fast, server-side scripting, single-threaded • MongoDB o on-disk, speed inversely related to data integrity, bson, master/slave, sharding, multi-master, server-side mapreduce, database-level locking • Riak o on-disk, pluggable data stores, multi-master sharding, RESTful API, server-side map-reduce, (Erlang + C) • MySQL/PostgreSQL o on-disk/in-memory, pluggable data stores, master/slave, sharding, stored procedures, ...
  • 7. What is Redis? (Strings) • Really scalars of a few different types o Character strings  concatenate values to the end  get/set individual bits  get/set byte ranges o Integers (platform long int)  increment/decrement  auto "casting" o Floats (IEEE 754 FP Double)  increment/decrement  auto "casting"
  • 8. What is Redis? (Lists) • Doubly-linked list of character strings o Push/pop from both ends o [Blocking] pop from multiple lists o [Blocking] pop from one list, push on another o Get/set/search for item in a list o Sortable
  • 9. What is Redis? (Sets) • Unique unordered sequence of character strings o Backed by a hash table o Add, remove, check membership, pop, random pop o Set intersection, union, difference o Sortable
  • 10. What is Redis? (Hashes) • Key-value mapping inside a key o Get/Set/Delete single/multiple o Increment values by ints/floats o Bulk fetch of Keys/Values/Both o Sort-of like a small version of Redis that only supports strings/ints/floats
  • 11. What is Redis? (Sorted Sets - ZSETs) • Like a Hash, with 'members' and 'scores', scores limited to float values o Get, set, delete, increment o Can be accessed by the sorted order of the (score,member) pair  By score  By index
  • 12. What is Redis? (Publish/Subscribe) • Readers subscribe to "channels" (exact strings or patterns) • Writers publish to channels, broadcasting to all subscribers • Messages are transient
  • 13. Why Redis with Python? • The power of Python lies in: o Reasonably sane syntax/semantics o Easy manipulation of data and data structures o Large and growing community • Redis also has: o Reasonably sane syntax/semantics o Easy manipulation of data and data structures o Medium-sized and growing community o Available as remote server  Like a remote IPython, only for data  So useful, people have asked for a library version
  • 14. Per-hour and Per-day hit counters from itertools import imap import redis def process_lines(prefix, logfile): conn = redis.Redis() for log in imap(parse_line, open(logfile, 'rb')): time = log.timestamp.isoformat() hour = time.partition(':')[0] day = time.partition('T')[0] conn.zincrby(prefix + hour, log.path) conn.zincrby(prefix + day, log.path) conn.expire(prefix + hour, 7*86400) conn.expire(prefix + day, 30*86400)
  • 15. Per-hour and Per-day hit counters (with pipelines for speed) from itertools import imap import redis def process_lines(prefix, logfile): pipe = redis.Redis().pipeline(False) for i, log in enumerate(imap(parse_line, open(logfile, 'rb'))): time = log.timestamp.isoformat() hour = time.partition(':')[0] day = time.partition('T')[0] pipe.zincrby(prefix + hour, log.path) pipe.zincrby(prefix + day, log.path) pipe.expire(prefix + hour, 7*86400) pipe.expire(prefix + day, 30*86400) if not i % 1000: pipe.execute() pipe.execute()
  • 16. Simple task queue - add/run items import json import redis def add_item(queue, name, *args, **kwargs): redis.Redis().rpush(queue, json.dumps([name, args, kwargs])) def execute_one(queues): item = redis.Redis().blpop(queues, 30) name, args, kwargs = json.loads(item) REGISTRY[name](*args, **kwargs)
  • 17. Simple task queue - register tasks REGISTRY = {} def task(queue): def wrapper(function): def defer(*args, **kwargs): add_item(queue, name, *args, **kwargs) name = function.__name__ if name in REGISTRY: raise Exception( "Duplicate callback %s"%(name,)) REGISTRY[name] = function return defer if isinstance(queue, str): return wrapper function, queue = queue, 'default' return wrapper(function)
  • 18. Simple task queue – register tasks @task('high') def do_something(arg): pass @task def do_something_else(arg): pass
  • 19. Cool stuff to do... • Reddit • Publish/Subscribe • Caching • Messaging • Cookies • Search engines • Analytics • Ad targeting • Configuration • Twitter management • Chat rooms • Autocomplete • Job search • Distributed locks • ... • Counting Semaphores • Task queues
  • 20. Thank you @dr_josiah dr-josiah.blogspot.com bit.ly/redis-in-action Questions?