Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Redis Hacks
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
David Cramer
May 03, 2014
Technology
270
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
More Decks by David Cramer
See All by David Cramer
Mastering Duct Tape (PyCon Balkan 2018)
zeeg
2
920
Open Source as a Business (PyCon SG 2014)
zeeg
0
410
Angular.js Workshop (PyCon SG 2014)
zeeg
0
280
Architecting a Culture of Quality
zeeg
2
340
Release Faster
zeeg
12
1.5k
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プロダクトをどう導くか?dodaが挑む、ユーザーの『本音』を構造化する評価設計と検証のリアル
techtekt
PRO
0
190
ABEMA の Datadog × OTel 基盤、 中から見るか? 外から見るか?
tetsuya28
0
100
AI-DLCを活用した高品質・安全なAI駆動開発実践 / AI Driven Development
yoshidashingo
1
380
2026.06.13_AI時代に事業会社が「SIer出身エンジニア」を求める理由 / Why Businesses Seek Engineers with a System Integrator Background in the AI Era
jumtech
0
560
Oracle AI Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
6
1.9k
AgentGatewayを試してみたかった
tkikuchi
0
110
GoとSIMDとWasmの今。
askua
3
510
React、まだ楽しくて草
uhyo
7
4.1k
Unlocking the Apps
pimterry
0
240
ルールやカスタム機能、どう使う?理想の出力を引き出すために今知りたいIBM Bob 5つの機能
muehara
1
340
Sony_KMP_Journey_KotlinConf2026
sony
2
210
Claude code Orchestra
ozakiomumkj
3
980
Featured
See All Featured
Speed Design
sergeychernyshev
33
1.8k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
940
How to train your dragon (web standard)
notwaldorf
97
6.7k
Thoughts on Productivity
jonyablonski
76
5.2k
Designing Experiences People Love
moore
143
24k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
770
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
250
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
380
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
390
The Art of Programming - Codeland 2020
erikaheidi
57
14k
How to build a perfect <img>
jonoalderson
1
5.6k
Un-Boring Meetings
codingconduct
0
310
Transcript
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)