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
David Cramer
May 03, 2014
Technology
260
3
Share
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
910
Open Source as a Business (PyCon SG 2014)
zeeg
0
410
Angular.js Workshop (PyCon SG 2014)
zeeg
0
270
Architecting a Culture of Quality
zeeg
2
340
Release Faster
zeeg
12
1.4k
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
GitHub Actions侵害 — 相次ぐ事例を振り返り、次なる脅威に備える
flatt_security
13
7.5k
I ran an automated simulation of fake news spread using OpenClaw.
zzzzico
1
910
Oracle Cloud Infrastructure:2026年3月度サービス・アップデート
oracle4engineer
PRO
0
380
Strands Agents × Amazon Bedrock AgentCoreで パーソナルAIエージェントを作ろう
yokomachi
2
150
ZOZOTOWNリプレイスでのSkills導入までの流れとこれから
zozotech
PRO
4
2.4k
OPENLOGI Company Profile for engineer
hr01
1
62k
Tour of Agent Protocols: MCP, A2A, AG-UI, A2UI with ADK
meteatamel
0
200
パワポ作るマンをMCP Apps化してみた
iwamot
PRO
0
300
建設的な現実逃避のしかた / How to practice constructive escapism
pauli
3
160
AIドリブン開発の実践知 ― AI-DLC Unicorn Gym実施から見えた可能性と課題
mixi_engineers
PRO
0
110
Claude Teamプランの選定と、できること/できないこと
rfdnxbro
1
560
Microsoft Fabricで考える非構造データのAI活用
ryomaru0825
0
650
Featured
See All Featured
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
370
How GitHub (no longer) Works
holman
316
150k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
170
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Google's AI Overviews - The New Search
badams
0
960
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
53k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.1k
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)