search
HomeWeb Front-endJS TutorialReplicache Big Picture

Replicache

This is a framework that helps implement Local First Software. Git similarly helps organize synchronized tasks through push and pull.

Replicache synchronizes server data asynchronously behind the scenes, eliminating server round trips and enabling immediate UI changes.

Replica Parts

Replicache consists of several elements.

Replicache

Replicache can be viewed as an in-browser Key-Value store that includes git-like operations internally. Write to memory first and synchronize later.

Your application

This is an application we created, like a web application. It is the entity that stores the state in Replicache. Mutator and Subscription are implemented to change and respond to state.

Your server

It exists to store the most reliable data. The state stored in the database connected to the server takes priority over the state in the application.

The server must implement push (upstream) and pull (downstream) to communicate with the client's Replicache.

  • push(upstream): Replicache sends changes to the push endpoint. Mutators are implemented on servers as well as applications, and this push endpoint executes this mutator to change the state of the database.

  • pull(downstream): When periodically or explicitly requested, Replicache sends a pull request to the server. The server returns the changes necessary for the client to become identical to the server's state.

  • poke: Although the client periodically sends a pull request, in order to display it in more real time, when there is a change on the server, it is a signal that the server gives a hint to the client to make a pull request. It does not carry any data.

Sync

The application and server are synchronized to the latest state. The picture below clearly shows this process. It shows the process of periodically pulling state changes from the server and updating the UI, and how state changes on the client first update the UI and are then pushed to the server.

Replicache Big Picture
Source

Clients, ClientGroups, Caches

Replicache in memory is called Client.

import {Replicache} from "replicache";

const rep = new Replicache({
  name: userID,
  ...
});

console.log(rep.clientID);

There is usually one client per tap. The client is volatile and follows the life cycle of the tab. Has a unique clientID.

Client Group is a set of clients that share local data. Clients within this client group share state even when offline.

Client Group uses an on-disk persistent cache that is distinguished by the name parameter of the Replicache constructor. All clients belonging to a Client Group with the same name share the same cache.

The Client View

Client has an ordered map of key value pairs in persistent cache, which is called Client View. Client View is application data and is synchronized with server data. The reason it is called Client View is because different clients may have server data in different Client Views. This means that each client sees the status of the server differently.

Accessing the Client View is very fast. Read latency is less than 1ms and most devices have a throughput of 500MB/s.

It is recommended that you read and use it directly from the Client View rather than copying the Client View separately using useState from a place like React and uploading it to memory. When the mutator changes the Client View, the subscription is triggered so that the UI is updated.

Subscriptions

The Subscribe function receives the ReadTransaction argument and implements reading from Replicache. Whenever this subscription goes out of date due to a change in replicache data, the subscribe function is performed again. If this result changes, the value is updated and the UI is also updated.

If you configure the UI through Subscription, you can always keep it up to date.

import {Replicache} from "replicache";

const rep = new Replicache({
  name: userID,
  ...
});

console.log(rep.clientID);

Mutations

Mutation refers to the task of changing Replicache data. The subject that receives mutations and actually changes data is called a mutator.

At startup, several Mutators are registered in Replicache, but they are actually just named functions. Both createTodo and markTodoComplete below are mutators that change Replicache data through WriteTransaction.

const todos = useSubscribe(rep, async tx => {
  return await tx.scan({prefix: 'todo/'}).toArray();
});
return (
  
    {todos.map(todo => (
  • {todo.text}
  • ))}
);

Mutator works as follows. When the mutator operates, the data changes, and the subscriptions related to it are triggered, and the UI also changes.

const rep = new Replicache({
  ...
  mutators: {
    createTodo,
    markTodoComplete,
  },
});

async function createTodo(tx: WriteTransaction, todo: Todo) {
  await tx.set(`/todo/${todo.id}`, todo);
}

async function markTodoComplete(tx: WriteTransaction,
    {id, complete}: {id: string, complete: boolean}) {
  const key = `/todo/${id}`;
  const todo = await tx.get(key);
  if (!todo) {
    return;
  }
  todo.complete = complete;
  await tx.set(key, todo);
}

Internally, Mutator creates something called mutation. It's like an execution record, but Replicache creates the following mutation.

await rep.mutate.createTodo({id: nanoid(), text: "take out the trash"});

These mutations are marked as pending until they are pushed to the server and completely synced.

Sync Details

Now, here are the details of Sync, which can be said to be the core of Replicache. Sync is done on the server.

The Replica Sync Model

(From now on, the expression ‘state’ refers to the state of data (key value space) consisting of several key and value pairs.)

The Sync problem that Replicache is trying to solve occurs when multiple clients change the same state at the same time and the following conditions exist.

  1. The state that the server has is the source of truth. It is expressed as canonical.
  2. Changes in the client’s local state are reflected immediately. This is called speculative.
  3. The server must apply changes exactly once and the results must be predictable. Changes applied to the server must be able to reasonably merge with local changes on the client.

The last item among these is 'reasonably merging' server changes with local state, which is an interesting topic. For ‘rational merger’, the following situations must be considered.

  • If local changes have not yet been applied to the server. In this case, you need to ensure that local changes do not disappear from the app's UI even if new state is retrieved from the server. After receiving the new state from the server, any existing local changes must be re-executed on top of the server state.

  • When local changes made on the client have already been sent to the server and reflected in the server state. In this case, be careful not to apply local changes twice. Local changes should not be reapplied

  • If there are other clients that have changed the server state for the same state. In this case, as in the first case, local changes must be redone based on the status received from the server. However, because conflicts may occur over the same resource, the merge logic must be carefully planned. Write this logic inside the Mutator.

Let’s follow the Mutator’s operation process.

Local execution

The mutator operates locally and the value of the replicache changes according to the mutator logic. At the same time, this client creates a mutation with a mutationId that increases sequentially. Mutations are queued as pending mutations.

Push

Pending mutations are sent to the push endpoint (replicache-push) implemented on the server.

mutation changes the canonical state by executing the mutator implemented on the server. While applying the mutation, the last mutation id of this client is updated, and becomes a value that allows you to know which mutation to reapply from when this client does the next pull.

A pending mutation applied locally generates a speculative result, and a mutation applied to the server generates a canonical result. Mutations applied to the server are confirmed and will not be executed locally again. Even if the same mutation returns a different result, the server's canonical result takes precedence, so the client's result changes.

Pull

Replicache periodically sends a request to the pull endpoint (replicache-pull) to retrieve the latest state and update the UI.

Pull request includes cookie and clientGroupId, and returns new cookie, patch, and lastMutationIDChanges.

Cookie is used to distinguish the server status held by the client. Any value that can track how much the server and client states differ is sufficient. You can think of it as a global 'version' that changes whenever the state of the database changes. Alternatively, you can use a cookie strategy to track a more specific range of data.

lastMutationIdChanges is a value representing the mutation ID last applied by the server for each client. All mutations with a mutationID smaller than this value should no longer be considered pending but confirmed.

Rebase

When a client receives a pull, the patch must be applied to the local state. However, because the pending mutation would have affected the current local state, the patch cannot be applied directly to the local state. Instead, revert the local pending mutation, apply the patch received as a pull first, and then apply the local pending mutation again.

To enable this kind of undo and reapplication, Replicache was designed similarly to Git. You can think of the state of the server as the main branch, and the state changed by a locally pending mutation as the develop branch, receive a pull from the server to main, and rebase develop to main.

Conflicts that may arise during rebase will be discussed separately below.

Poke

Poke, as explained above, is a hint message that the server tells the client to pull.

Conflict Resolution

Merge conflicts are unavoidable in distributed systems such as Replicache. Merging is necessary during the pull and push process. Merging should be done in a way that makes the merge result predictable and fits the purpose of the app.

If it is a conference room reservation app, only one request should be approved when a conflict occurs. Therefore, you must adopt a merge method that only approves clients who have made reservations first.

On the other hand, if it is a Todo app, the purpose of the Todo list is that both changes are approved even if additions occur at the same time.

Merge Conflict occurs in the following two situations.

  1. When local changes will be applied to the server. This is because the status when applied locally and the status when applied on the server may be different.

  2. When rebasing. This is because the status may be different when applied.

Replicache recognizes that the merge method must be implemented differently depending on the purpose of the app, so it allows developers to implement it. Developers can implement this logic through Mutator.

The above is the detailed content of Replicache Big Picture. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment