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
290
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
E2E Test というハーネス — AI時代の自動テスト民主化
daishukojima
1
600
Context Ontology Accelerator 入門 (業務データの意味を、AI エージェントに渡す)
inariku
PRO
3
670
[JAWS Festa 2026]一番いい仕事は、請求書に現れない / jawsfesta2026
sh_fk2
2
520
Sonnetだけで業務はまわる?
kintotechdev
1
110
Claude Codeで新規事業を伸ばせるか? Zennの試行錯誤の記録
wadayusuke
2
170
Bots in Kiro Crew (One point of contact for ongoing work) | Kiro: Zero to Hero 12
inariku
PRO
1
130
Reticulum, le réseau cypherpunk dont vous avez besoin !
rlifchitz
0
140
複数システムが同居した 15 年物の AWS アカウントを解体するための考古学
wa6sn
3
150
Frontier Teams — Amazon は AI で「開発のやり方」をどう変えたのか
kmotohas
0
240
Go初心者のクラウドエンジニアが、デプロイを通して旨味を知っていく話
ryudai
1
200
異夢同船〜異なる夢でも同じ船でなんとかする〜
fortegp05
0
210
Active Directory と Entra ID のむむむな関係
murachiakira
PRO
4
1.8k
Featured
See All Featured
The untapped power of vector embeddings
frankvandijk
2
2k
Into the Great Unknown - MozCon
thekraken
41
2.8k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.7k
SEO for Brand Visibility & Recognition
aleyda
0
4.9k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.9k
WENDY [Excerpt]
tessaabrams
15
40k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.7k
Speed Design
sergeychernyshev
33
2.2k
Side Projects
sachag
456
43k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
610
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.8k