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
ndb
Search
spicyj
May 28, 2014
Technology
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ndb
spicyj
May 28, 2014
More Decks by spicyj
See All by spicyj
React: What Lies Ahead
spicyj
6
410
Creating interactive learning interfaces at Khan Academy
spicyj
0
130
Understanding state in React
spicyj
1
140
css
spicyj
2
890
Other Decks in Technology
See All in Technology
IoTハンズオンの舞台裏
shirouz
2
140
PM領域でのAI Agentの活用
lycorptech_jp
PRO
0
270
『止めない』を設計する — 制約の中で、事業の根幹を支える判断
hiroyaterui
0
200
スクラムで身についていた動き方を、XPで捉え直してみた
codmoninc
PRO
1
230
GuardDuty 検知対応を DevOps Agent で効率化しようとしている話 / GuardDuty Investigations with DevOps Agent
masahirokawahara
1
310
ペアプロの価値はコードを書くことだけじゃない
codmoninc
PRO
0
100
From Vanilla Kubernetes to a Batteries-Included Platform: Developer Experience at 1,300+ Clusters
yosshi_
0
550
AndroidでHDRメディアを「壊さずに」扱う
chigichan24
0
400
データエンジニアの困りごとをDevinと一緒に解消する
10xinc
2
830
リージョンの壁を越える、 ちょっと変わったAWSサービスの話
falken
PRO
0
260
生成AI時代の クレデンシャルとパーミッション設計
nrinetcom
PRO
3
1.5k
[RSJ26] Building a VLA Model Based on Self-Distilled Classification
keio_smilab
PRO
0
190
Featured
See All Featured
Marketing to machines
jonoalderson
1
5.7k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
The Pragmatic Product Professional
lauravandoore
37
7.4k
The Language of Interfaces
destraynor
162
27k
Navigating Team Friction
lara
192
16k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
520
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
380
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
310
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
600
Speed Design
sergeychernyshev
33
2.1k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
430
Product Roadmaps are Hard
iamctodd
55
13k
Transcript
ndb “NDB is a better datastore API for the Google
App Engine Python runtime.”
Part 1 of 2
Why ndb? 1. Less stupid by default 2. More flexible
queries 3. Tasklets with autobatching
Less stupid by default With db: class UserVideo(db.Model): user_id =
db.StringProperty() video = db.ReferenceProperty(Video) user_video = UserVideo.get_for_video_and_user_data( video, user_data) return jsonify(user_video) # slow
Less stupid by default With ndb: class UserVideo(ndb.Model): user_id =
ndb.StringProperty() video = ndb.KeyProperty(kind=Video) user_video = UserVideo.get_for_video_and_user_data( video, user_data) return jsonify(user_video) # not slow!
More flexible queries ndb lets you build filters using ndb.AND
and ndb.OR: questions = Feedback.query() .filter(Feedback.type == 'question') .filter(Feedback.target == video_key) .filter(ndb.OR( Feedback.is_visible_to_public == True, Feedback.author_user_id == current_id)) .fetch(1000) Magic happens.
Performance The datastore is slow. How can we speed things
up? 4 Batch operations together 4 Do things in parallel 4 Avoid the datastore
Tasklets and autobatching def get_user_exercise_cache(user_data): uec = UEC.get_for_user_data(user_data) if not
uec: user_exercises = UE.get_all(user_data) uec = UEC.build(user_exercises) return uec def get_all_uecs(user_datas): return map(get_user_exercise_cache, user_datas)
Tasklets and autobatching @ndb.tasklet def get_user_exercise_cache_async(user_data): uec = yield UEC.get_for_user_data_async(user_data)
if not uec: user_exercises = yield UE.get_all(user_data) uec = UEC.build(user_exercises) raise ndb.Return(uec) @ndb.synctasklet def get_all_uecs(user_datas): uecs = yield map(get_user_exercise_cache_async, user_datas) raise ndb.Return(uecs)
Moral ndb is awesome. Use it.
Part 2 of 2
The sad truth ndb isn't perfect.
Mysterious errors You heard from Marcia about this gem back
in March: TypeError: '_BaseValue' object is not subscriptable
Q: What's worse than code that doesn't work at all?
A: Code that mostly works but breaks in subtle ways.
Secret slowness #1 Multi-queries, with IN and OR: answers =
Feedback.query() .filter(Feedback.type == 'answer') .filter(Feedback.in_reply_to.IN(question_keys)) .fetch(1000) Doesn't run in parallel!
Secret slowness #1 A not-horribly-slow multi-query: answers = Feedback.query() .filter(Feedback.type
== 'answer') .filter(Feedback.in_reply_to.IN(question_keys)) .order(Feedback.__key__) .fetch(1000)
Secret slowness #2 Query iterators: query = Feedback.query().filter( Feedback.topic_ids ==
'algebra') questions = [] for q in query.iter(batch_size=20): if q.is_visible_to(user_data): questions.append(q) if len(questions) >= 10: break
Secret slowness #2 Solution? Sometimes you have to do it
by hand.
Moral ndb isn't perfect. Pay attention. Profile your code.
The End