Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Redis Hacks
Search
David Cramer
May 03, 2014
Technology
300
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Redis Hacks
Python Nordeste 2014 - Lightning Talk
David Cramer
May 03, 2014
Transcript
15 slides
David Cramer twitter.com/zeeg Redis Hacks (or “How Sentry Scales”)
Buffering Writes
r = Redis() ! def incr(type, id): key = 'pending:{}'.format(type)
! r.zincrby(key, id, 1)
r = Redis() ! def flush(type): key = 'pending:{}'.format(type) result
= r.zrange(key, 0, -1, withscores=True) ! for id, count in result: prms = {'type': type, 'count': count, 'id': id} ! sql(""" update %(type)s set count = count + % (count)d where id = %(id)s """, prms)
Rate Limiting
r = Redis() ! def process_hit(project_id): epoch = time() /
60 key = ‘{}:{}’.format(project_id, epoch) ! pipe = r.pipeline() pipe.incr(key) pipe.expire(key, 60) result = pipe.execute() ! # return current value return int(result[0])
def request(project_id): result = process_hit(project_id) if result > 20: return
Response(status=429) return Response(status=200)
Time Series Data
def count_hits_today(project_id): start = time() end = now + DAY_SECONDS
! pipe = r.pipeline() for epoch in xrange(now, end, 10): key = ‘{}:{}’.format( project_id, epoch) pipe.get(key) results = pipe.execute() ! # remove non-zero results results = filter(bool, results) # coerce remainder to ints results = map(int, results) # return sum of buckets return sum(results)
Good-enough Locks
from contextlib import contextmanager ! r = Redis() ! @contextmanager
def lock(key, nowait=True): while not r.setnx(key, '1'): if nowait: raise Locked('try again soon!') sleep(0.01) ! # limit lock time to 10 seconds r.expire(key, 10) ! # do something crazy yield ! # explicitly unlock r.delete(key)
def do_something_crazy(): with lock('crazy'): print 'Hello World!'
Basic Sharding via Nydus
from nydus.db import create_cluster ! redis = create_cluster({ 'backend': 'nydus.db.backends.redis.Redis',
'hosts': { 0: {'db': 0}, 1: {'db': 1}, }, 'router': 'nydus.db.routers.keyvalue.PartitionRouter', })
def count_hits_today(project_id): start = time() end = now + DAY_SECONDS
! keys = [] for epoch in xrange(now, end, 10): key = '{}:{}'.format(project_id, epoch) keys.append(key) ! with redis.map() as conn: results = map(conn.get, keys) ! # remove non-zero results results = filter(bool, results) # coerce remainder to ints results = map(int, results) # return sum of buckets return sum(results)
More Decks by David Cramer
See All by David Cramer
Mastering Duct Tape (PyCon Balkan 2018)
zeeg
2
940
Open Source as a Business (PyCon SG 2014)
zeeg
0
430
Angular.js Workshop (PyCon SG 2014)
zeeg
0
290
Architecting a Culture of Quality
zeeg
2
350
Release Faster
zeeg
12
1.6k
Open Source as a Business (EuroPython 2013)
zeeg
18
17k
Building to Scale (PyCon TW 2013)
zeeg
18
1.4k
Building to Scale
zeeg
28
24k
Lessons in Testing - DjangoCon 2012
zeeg
8
1.5k
Other Decks in Technology
See All in Technology
AIエージェントのアーキテクチャ設計
nsatoh
0
610
技術書典で本を売る流れ
yoshimura_datam
0
210
AIエージェントの認証がまとめて進化! ~pre:Inventで来た3つのアップデート~
minorun365
PRO
2
300
Metadata - Section 4 : MODS (Metadata Object Description Schema )
mohab1
0
210
LLMごとの文章力を測ってみた話
yoshimura_datam
0
140
Taking back control of your AI development
inesmontani
PRO
0
320
生成AIによる情報システムへのインパクト (2026年11月版)
taka_aki
0
140
AWS Blocksで AIエージェント作れるの知ってます?
kmiya84377
1
170
データ界隈LT祭の運営のふりかえり
yoshimura_datam
0
120
Kiro のリモートワークスペース: IDE は手元のままで、ワークスペースをクラウド、コンテナ、自前のマシンに置く
inariku
PRO
2
280
PR 生成AIガバナンスの全体像がわかる本
yoshimura_datam
0
140
最近のMySQL事情について
yoku0825
2
1.2k
Featured
See All Featured
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
1
250
Balancing Empowerment & Direction
lara
6
1.4k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
2k
The browser strikes back
jonoalderson
0
2k
GraphQLとの向き合い方2022年版
quramy
50
15k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Git: the NoSQL Database
bkeepers
PRO
433
67k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Claude Code のすすめ
schroneko
67
230k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
First, design no harm
axbom
PRO
2
1.4k
Building AI with AI
inesmontani
PRO
1
1.4k