Skip to content

Commit c6806a7

Browse files
authored
Add TanStack DB blog post (#447)
1 parent e89e922 commit c6806a7

File tree

6 files changed

+392
-10
lines changed

6 files changed

+392
-10
lines changed

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
pnpm-lock.yaml
55
routeTree.gen.ts
66
convex/_generated
7-
convex/README.md
7+
convex/README.md
8+
src/blog/tanstack-db-0.1-the-embedded-client-database-for-tanstack-query.md
233 KB
Loading
Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
---
2+
title: Stop Re-Rendering — TanStack DB, the Embedded Client Database for TanStack Query
3+
published: 2025-07-30
4+
authors:
5+
- Kyle Mathews
6+
- Sam Willis
7+
---
8+
9+
![Stop rerendering](/blog-assets/tanstack-db-0.1/header.png)
10+
11+
**Your React dashboard shouldn't grind to a halt** just because one TODO turns from ☐ to ☑. Yet every optimistic update still kicks off a cascade of re-renders, filters, useMemos and spinner flashes.
12+
13+
If you’ve ever muttered “**why is this still so hard in 2025?**”—same.
14+
15+
TanStack DB is our answer: a client-side database layer powered by differential dataflow that plugs straight into your existing useQuery calls.
16+
17+
It recomputes only what changed—**0.7 ms to update one row in a sorted 100k collection** on an M1 Pro ([CodeSandbox](https://codesandbox.io/p/sandbox/bold-noyce-jfz9fs))
18+
19+
One early-alpha adopter, building a Linear-like application, swapped out a pile of MobX code for TanStack DB and told us with relief, “everything is now completely instantaneous when clicking around the app, even w/ 1000s of tasks loaded.”
20+
21+
### Why it matters
22+
23+
<style>
24+
.code-comparison {
25+
display: grid;
26+
grid-template-columns: 1fr 1fr;
27+
gap: 16px;
28+
margin: 20px 0;
29+
}
30+
31+
.comparison-column {
32+
min-width: 0; /* Prevent overflow */
33+
overflow-x: auto;
34+
}
35+
36+
.comparison-column pre {
37+
font-size: 14px; /* Slightly smaller font */
38+
}
39+
40+
/* Diff highlighting styles */
41+
.diff-remove {
42+
background-color: rgba(255, 129, 130, 0.2);
43+
position: relative;
44+
}
45+
46+
.diff-remove::before {
47+
content: '-';
48+
position: absolute;
49+
left: -20px;
50+
color: #d73a49;
51+
font-weight: bold;
52+
}
53+
54+
.diff-add {
55+
background-color: rgba(46, 160, 67, 0.2);
56+
position: relative;
57+
}
58+
59+
.diff-add::before {
60+
content: '+';
61+
position: absolute;
62+
left: -20px;
63+
color: #28a745;
64+
font-weight: bold;
65+
}
66+
67+
/* Mobile responsive - stack vertically on small screens */
68+
@media (max-width: 768px) {
69+
.code-comparison {
70+
grid-template-columns: 1fr;
71+
gap: 24px;
72+
}
73+
74+
.comparison-column h3 {
75+
margin-top: 0;
76+
}
77+
}
78+
</style>
79+
80+
Today most teams face an ugly fork in the road:
81+
82+
**Option A. View-specific APIs** (fast render, slow network, endless endpoint sprawl) or
83+
84+
**Option B. Load-everything-and-filter** (simple backend, sluggish client).
85+
86+
Differential dataflow unlocks **Option C—load normalized collections once, let TanStack DB stream millisecond-level incremental joins in the browser**. No rewrites, no spinners, no jitter.
87+
88+
**Live queries, effortless optimistic writes, and a radically simpler architecture**—all incrementally adoptable.
89+
90+
_[Try out the TanStack DB Starter](https://github.com/TanStack/db/tree/main/examples/react/projects)_
91+
92+
## So what’s happening under the hood?
93+
94+
TanStack DB keeps a **normalized collection store** in memory, then uses **differential dataflow** to update query results incrementally. Think of it like Materialize-style streaming SQL—except embedded in the browser and hooked straight into React Query’s cache.
95+
96+
- **Collections** wrap your existing `useQuery` calls (REST, tRPC, GraphQL, WebSocket—doesn’t matter). Do you sync data some other way? [Build a custom collection](https://tanstack.com/db/latest/docs/collection-options-creator).
97+
- **Transactions** let you mutate those collections optimistically; failures roll back automatically.
98+
- **Live queries** declare _what_ data you need; TanStack DB streams only the rows that change, in < 1 ms.
99+
100+
Put differently: **TanStack Query still owns “how do I fetch?”**; **TanStack DB owns “how do I keep everything coherent and lightning-fast once it’s here?”**
101+
102+
And because it’s just another layer on top of `queryClient`, you can adopt it one collection at a time.
103+
104+
## TanStack Query → TanStack DB
105+
106+
Imagine we already have a backend with a REST API that exposes the `/api/todos` endpoint to fetch a list of todos and mutate them.
107+
108+
<div class="code-comparison">
109+
<div class="comparison-column">
110+
111+
### Before: TanStack Query
112+
113+
```typescript
114+
import {
115+
useQuery,
116+
useMutation,
117+
useQueryClient, // ❌ Not needed with DB
118+
} from '@tanstack/react-query'
119+
120+
const Todos = () => {
121+
const queryClient = useQueryClient() //
122+
123+
// Fetch todos
124+
const { data: allTodos = [] } = useQuery({
125+
queryKey: ['todos'],
126+
queryFn: async () =>
127+
api.todos.getAll('/api/todos'),
128+
})
129+
130+
// Filter incomplete todos
131+
// ❌ Runs every render unless memoized
132+
const todos = allTodos.filter(
133+
(todo) => !todo.completed
134+
)
135+
136+
// ❌ Manual optimistic update boilerplate
137+
const addTodoMutation = useMutation({
138+
mutationFn: async (newTodo) =>
139+
api.todos.create(newTodo),
140+
onMutate: async (newTodo) => {
141+
await queryClient.cancelQueries({
142+
queryKey: ['todos'],
143+
})
144+
const previousTodos =
145+
queryClient.getQueryData(['todos'])
146+
queryClient.setQueryData(
147+
['todos'],
148+
(old) => [...(old || []), newTodo]
149+
)
150+
151+
return { previousTodos }
152+
},
153+
onError: (err, newTodo, context) => {
154+
queryClient.setQueryData(
155+
['todos'],
156+
context.previousTodos
157+
)
158+
},
159+
onSettled: () => {
160+
queryClient.invalidateQueries({
161+
queryKey: ['todos'],
162+
})
163+
},
164+
})
165+
166+
return (
167+
<div>
168+
<List items={todos} />
169+
<Button
170+
onClick={() =>
171+
addTodoMutation.mutate({
172+
id: uuid(),
173+
text: '🔥 Make app faster',
174+
completed: false,
175+
})
176+
}
177+
/>
178+
</div>
179+
)
180+
}
181+
```
182+
183+
</div>
184+
<div class="comparison-column">
185+
186+
### After: TanStack DB
187+
188+
```typescript
189+
// ✅ Define a Query Collection
190+
import { createCollection } from '@tanstack/react-db'
191+
import { queryCollectionOptions } from '@tanstack/query-db-collection'
192+
193+
const todoCollection = createCollection(
194+
queryCollectionOptions({
195+
queryKey: ['todos'],
196+
queryFn: async () =>
197+
api.todos.getAll('/api/todos'),
198+
getKey: (item) => item.id, // ✅ New
199+
schema: todoSchema, // ✅ New
200+
onInsert: async ({ transaction }) => {
201+
// ✅ New
202+
await Promise.all(
203+
transaction.mutations.map((mutation) =>
204+
api.todos.create(mutation.modified)
205+
)
206+
)
207+
},
208+
})
209+
)
210+
211+
// ✅ Use live queries in components
212+
import { useLiveQuery } from '@tanstack/react-db'
213+
import { eq } from '@tanstack/db'
214+
215+
const Todos = () => {
216+
// ✅ Live query with automatic updates
217+
const { data: todos } = useLiveQuery((query) =>
218+
query
219+
.from({ todos: todoCollection })
220+
// ✅ Type-safe query builder
221+
// ✅ Incremental computation
222+
.where(({ todos }) =>
223+
eq(todos.completed, false)
224+
)
225+
)
226+
227+
return (
228+
<div>
229+
<List items={todos} />
230+
<Button
231+
onClick={() =>
232+
// ✅ Simple mutation - no boilerplate!
233+
// ✅ Automatic optimistic updates
234+
// ✅ Automatic rollback on error
235+
todoCollection.insert({
236+
id: uuid(),
237+
text: '🔥 Make app faster',
238+
completed: false,
239+
})
240+
}
241+
/>
242+
</div>
243+
)
244+
}
245+
```
246+
247+
</div>
248+
</div>
249+
250+
## Why a new client store?
251+
252+
TanStack Query is incredibly popular with 12m (and counting) downloads per week. So why make something new like TanStack DB?
253+
254+
Query solves the hardest problems of server state management — intelligent caching, background synchronization, request deduplication, optimistic updates, and seamless error handling.
255+
256+
It's become the de facto standard because it eliminates the boilerplate and complexity of managing async data fetching while providing an excellent developer experience with features like automatic background refetching, stale-while-revalidate patterns, and powerful DevTools.
257+
258+
But Query treats data as isolated cache entries. Each query result is independent—there's no concept of relationships, live queries across multiple data sources, or reactive updates when one piece of data affects another. **You can't easily ask "show me all todos where the project status is active"** and watch the list update automatically when a project flips status.
259+
260+
TanStack DB fills this gap. While Query excels at fetching and caching server state, DB provides the missing reactive, relational layer on top. You get the best of both worlds: Query's robust server state management plus TanStack DB’s embedded client database that can join, filter, and reactively update across your entire data graph.
261+
262+
But it doesn’t just improve your current setup — it enables a new radically simplified architecture.
263+
264+
## TanStack DB enables a radically simplified architecture
265+
266+
Let's revisit the three options:
267+
268+
**Option A — View-Specific APIs**: Create view-specific API endpoints that return exactly what each component needs. Clean, fast, zero client-side processing. But now you're drowning in brittle API routes, dealing with network waterfalls when components need related data, and creating tight coupling between your frontend views and backend schemas.
269+
270+
**Option B — Load-everything-and-filter**: Load broader datasets and filter/process them client-side. Fewer API calls, more flexible frontend. But you slam into the performance wall — `todos.filter()`, `users.find()`, `posts.map()`, `useMemo()` everywhere, with cascading re-renders destroying your UX.
271+
272+
Most teams pick Option A to avoid performance problems. You're trading client-side complexity for API proliferation and network dependency.
273+
274+
**TanStack DB enables Option C – Normalized Collections + Incremental Joins:** Load normalized collections through fewer API calls, then perform lightning-fast incremental joins in the client. You get the network efficiency of broad data loading with sub-millisecond query performance that makes Option A unnecessary.
275+
276+
Instead of this:
277+
278+
```typescript
279+
// View-specific API call every time you navigate
280+
const { data: projectTodos } = useQuery(
281+
['project-todos', projectId],
282+
() => fetchProjectTodosWithUsers(projectId)
283+
)
284+
```
285+
286+
You can do this:
287+
288+
```typescript
289+
// Load normalized collections upfront (3 broader calls)
290+
const todoCollection = createQueryCollection({
291+
queryKey: ['todos'],
292+
queryFn: fetchAllTodos,
293+
})
294+
const userCollection = createQueryCollection({
295+
queryKey: ['users'],
296+
queryFn: fetchAllUsers,
297+
})
298+
const projectCollection = createQueryCollection({
299+
queryKey: ['projects'],
300+
queryFn: fetchAllProjects,
301+
})
302+
303+
// Navigation is instant — no new API calls needed
304+
const { data: activeProjectTodos } = useLiveQuery(
305+
(q) =>
306+
q
307+
.from({ t: todoCollection })
308+
.innerJoin(
309+
{ u: userCollection },
310+
({ t, u }) => eq(t.userId, u.id)
311+
)
312+
.innerJoin(
313+
{ p: projectCollection },
314+
({ u, p }) => eq(u.projectId, p.id)
315+
)
316+
.where(({ t }) => eq(t.active, true))
317+
.where(({ p }) =>
318+
eq(p.id, currentProject.id)
319+
)
320+
)
321+
```
322+
323+
Now, clicking between projects, users, or views requires **zero API calls**. All the data is already loaded. New features like **"show user workload across all projects"** work instantly without touching your backend.
324+
325+
Your API becomes simpler. Your network calls drop dramatically. Your frontend gets faster as your dataset grows.
326+
327+
## The 20MB Question
328+
329+
**Your app would be dramatically faster if you just loaded 20MB of normalized data upfront** instead of making hundreds of small API calls.
330+
331+
Companies like Linear, Figma, and Slack load massive datasets into the client and achieve incredible performance through heavy investment in custom indexing, differential updates, and optimized rendering. These solutions are too complex and expensive for most teams to build.
332+
333+
**TanStack DB brings this capability to everyone** through differential dataflow — a technique that only recomputes the parts of queries that actually changed. Instead of choosing between "many fast API calls with network waterfalls" or "few API calls with slow client processing," you get the best of both options: fewer network round-trips AND sub-millisecond client-side queries, even with large datasets.
334+
335+
This isn't just about sync engines like [Electric (though they make this pattern incredibly powerful)](https://electric-sql.com/blog/2025/07/29/local-first-sync-with-tanstack-db). It's about enabling a fundamentally different data loading strategy that works with any backend — REST, GraphQL, or real-time sync.
336+
337+
## Why are sync engines interesting?
338+
339+
While TanStack DB works great with REST and GraphQL, it really shines when paired with sync engines. Here's why sync engines are such a powerful complement:
340+
341+
**Easy real-time** — If you need real-time updates, you know how painful it can be to set up WebSockets, handle reconnections, and wire up event handlers. Many new sync engines are native to your actual data store (e.g., Postgres) so you can simply write to the database directly and know the update will get streamed out to all subscribers in real-time. No more manual WebSocket plumbing.
342+
343+
**Side-effects are pushed automatically** — When you do a backend mutation, there are often cascading updates across multiple tables. Update a todo's status? That might change the project's completion percentage, update team metrics, or trigger workflow automations. With TanStack Query alone, you need manual bookkeeping to track all these potential side-effects and reload the right data. Sync engines eliminate this complexity—any backend change that happens during a mutation is automatically pushed to all clients - without any extra work.
344+
345+
**Load far more data efficiently** — It's far cheaper to update data in the client when using sync engines. Instead of re-loading entire collections after each change, sync engines send only the actual changed items. This makes it practical to load far more data upfront, enabling the "load everything once" pattern that makes apps like Linear feel so fast.
346+
347+
TanStack DB was designed from the ground up to support sync engines. [When you define a collection, you're provided with an API for writing synced transactions](https://tanstack.com/db/latest/docs/collection-options-creator) from the backend into your local collections. Try out collection implementations for [Electric](https://tanstack.com/db/latest/docs/installation#electric-collection), [Trailblaze](https://tanstack.com/db/latest/docs/installation#trailbase-collection), and [(soon) Firebase](https://github.com/TanStack/db/pull/323)!
348+
349+
DB gives you a common interface for your components to query data, which means you can easily switch between data loading strategies as needed without changing client code. Start with REST, switch to a sync engine later as needed—your components don't need to know the difference.
350+
351+
## Our Goals for TanStack DB
352+
353+
We're building TanStack DB to address the client-side data bottlenecks that every team eventually hits. Here's what we're aiming for:
354+
355+
- **True backend flexibility**: Work with any data source through pluggable collection creators. Whether you're using REST APIs, GraphQL, Electric, Firebase, or building something custom, TanStack DB adapts to your stack. Start with what you have, upgrade if needed, mix different approaches in the same app.
356+
- **Incremental adoption that actually works**: Start with one collection, add more as you build new features. No big-bang migrations or development pauses.
357+
- **Query performance at scale**: Sub-millisecond queries across large datasets through differential dataflow, even when your app has thousands of items.
358+
- **Optimistic updates that don't break**: Reliable rollback behavior when network requests fail, without complex custom state management.
359+
- **Type and runtime safety throughout**: Full TypeScript inference from your schema to your components, catching data mismatches at compile and runtime.
360+
361+
We're excited about giving teams a fundamentally better way to handle client-side data—while preserving the freedom to choose whatever backend works best.
362+
363+
## What's Next
364+
365+
TanStack DB 0.1 (first beta) is available now. We're specifically looking for teams who:
366+
367+
- Already use TanStack Query and hit performance/code complexity walls with complex state
368+
- Build collaborative features but struggle with slow optimistic updates
369+
- Have 1000+ item datasets causing rendering performance issues
370+
- Want real-time functionality without rewriting their entire data layer
371+
- First 20 teams get migration office hours
372+
373+
If your team spends more time optimizing React re-renders than building features, or if your collaborative features feel sluggish compared to Linear and Figma, TanStack DB is designed for exactly your situation.
374+
375+
**Get started today:**
376+
377+
- [Documentation & Quick Start](https://tanstack.com/db/latest)
378+
- [Try out the TanStack DB Starter](https://github.com/TanStack/db/tree/main/examples/react/projects)
379+
- [Join the TanStack Discord](https://tlinz.com/discord) - Direct migration support from the team
380+
381+
No more stutters. No more jank. Stop re-rendering—start shipping!

src/components/Doc.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function Doc({
148148
<div
149149
ref={markdownContainerRef}
150150
className={twMerge(
151-
'prose prose-gray prose-sm prose-p:leading-7 dark:prose-invert max-w-none',
151+
'prose prose-gray dark:prose-invert max-w-none',
152152
isTocVisible && 'pr-4 lg:pr-6',
153153
'styled-markdown-content'
154154
)}

0 commit comments

Comments
 (0)