How to configure a Typesense cluster?
Outline: [Article Title]
Keyword: [Enter Targeted Keyword]
Keyword MSV: [Enter Targeted Keyword’s Monthly Search Volume]
Author: [Enter Author Name]
Due Date: [Enter Due Date]
Publish Date: [Enter Desired Publish Date]
User Persona: [Enter Targeted Reader and/or User Persona]
The user can run a cluster of Typesense nodes for high availability. Typesense employs the Raft consensus algorithm to manage the cluster and recover from node failures. Raft is a consensus algorithm designed to be easy to use. It is comparable to Paxos in terms of fault-tolerance and performance. The distinction is that it is divided into relatively independent subproblems and addresses all of the major components required for practical systems.
Setting up a Typesense cluster
To start a Typesense node as part of a cluster, create a new file with the following format on each cluster node, and use the ‘—nodes’ server configuration to point to the file.
The following format, separated by commas, should be used for each node definition in the file:
<peering_address>:<peering_port>:<api_port>
The values for peering_address
, peering_port
and api_port
should correspond to the Server Configuration Parameters used when starting the Typesense process on each node.
For security reasons, all nodes in the cluster should use the same bootstrap --api-key
.
Nodes File Example
An example of a --nodes
file for a 3-node cluster is shown below:
192.168.12.1:8107:8108,192.168.12.2:8107:8108,192.168.12.3:8107:8108
In the example above
- The
peering_address
the IP address used for cluster operations is192.168.12.x
. - The
peering_port
(the port used for cluster operations) is8107
- The
api_port
(the actual port to which clients connect to) is8108
To start the Typesense process on each node, use the following command:
Node1
# Create nodes file
# This file is identical on all nodes
echo '192.168.12.1:8107:8108,192.168.12.2:8107:8108,192.168.12.3:8107:8108' | sudo tee /etc/typesense/nodes
# Start Typesense Process
# * Notice `peering-address` *
typesense-server \
--data-dir /var/lib/typesense \
--api-key=xyz \
--api-address 0.0.0.0 \
--api-port 8108 \
--peering-address 192.168.12.1 \
--peering-port 8107 \
--nodes=/etc/typesense/nodes
Node2
# Create nodes file
# This file is identical on all nodes
echo '192.168.12.1:8107:8108,192.168.12.2:8107:8108,192.168.12.3:8107:8108' | sudo tee /etc/typesense/nodes
# Start Typesense Process
# ** Notice `peering-address` **
typesense-server \
--data-dir /var/lib/typesense \
--api-key=xyz \
--api-address 0.0.0.0 \
--api-port 8108 \
--peering-address 192.168.12.2 \
--peering-port 8107 \
--nodes=/etc/typesense/nodes
Node3
# Create nodes file
# This file is identical on all nodes
echo '192.168.12.1:8107:8108,192.168.12.2:8107:8108,192.168.12.3:8107:8108' | sudo tee /etc/typesense/nodes
# Start Typesense Process
# *** Notice `peering-address` ***
typesense-server \
--data-dir /var/lib/typesense \
--api-key=xyz \
--api-address 0.0.0.0 \
--api-port 8108 \
--peering-address 192.168.12.3 \
--peering-port 8107 \
--nodes=/etc/typesense/nodes
Important
-
--peering-address
should be a Private IP address, as it is only intended for internal cluster operations and contains unencrypted Raft data exchanged between nodes. -
—api-address
can be either public or private. This is the IP address to which your users/clients will connect in order to interact with the Typesense API. -
In a production environment, it is strongly advised to setup
--api-port
to 443 (HTTPS) and configuring SSL certs with the--ssl-certificate
and--ssl-certificate-key
server parameters.
Important Tip
If you’re using Docker, make sure the Docker network is configured so that the Typesense process within the Docker container can communicate with the other Typesense processes using their IP addresses. Find out more about Docker Networking.
Client configuration
During client initialization, Typesense clients allow you to specify one or more nodes. Client libraries will load balance reads and writes across all nodes and will attempt to recover from transient failures via built-in retries. As a result, when deploying Typesense, you do not require a server-side load balancer.
Here’s an example of a three-node client configuration:
For PHP
use Typesense\Client;
$client = new Client(
[
'nodes' => [
[
'host' => 'x.x.x.x', // Can be an IP or more commonly a hostname mapped to the IP
'port' => 443,
'protocol' => 'https'
],
[
'host' => 'y.y.y.y', // Can be an IP or more commonly a hostname mapped to the IP
'port' => 443,
'protocol' => 'https'
],
[
'host' => 'z.z.z.z', // Can be an IP or more commonly a hostname mapped to the IP
'port' => 443,
'protocol' => 'https'
],
],
'api_key' => '<API_KEY>',
'connection_timeout_seconds' => 2,
]
);
For Ruby
require 'typesense'
client = Typesense::Client.new(
nodes: [
{
host: 'x.x.x.x', # Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https'
},
{
host: 'y.y.y.y', # Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https'
},
{
host: 'z.z.z.z', # Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https'
}
],
api_key: '<API_KEY>',
connection_timeout_seconds: 2
)
For Python
import typesense
client = typesense.Client({
'nodes': [
{
host: 'x.x.x.x', # Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https'
},
{
host: 'y.y.y.y', # Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https'
},
{
host: 'z.z.z.z', # Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https'
}
],
'api_key': '<API_KEY>',
'connection_timeout_seconds': 2
})
For Javascript
let client = new Typesense.Client({
nodes: [
{
host: "x.x.x.x", // Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: "https",
},
{
host: "y.y.y.y", // Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: "https",
},
{
host: "z.z.z.z", // Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: "https",
},
],
apiKey: "<API_KEY>",
connectionTimeoutSeconds: 2,
});
For Dart
import 'package:typesense/typesense.dart';
final config = Configuration(
nodes: {
Node(
host: 'x.x.x.x', // Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https',
),
Node(
host: 'y.y.y.y', // Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https',
),
Node(
host: 'z.z.z.z', // Can be an IP or more commonly a hostname mapped to the IP
port: 443,
protocol: 'https',
),
},
apiKey: '<API_KEY>',
connectionTimeout: Duration(seconds: 2),
);
For any other languages
Typesense has an HTTP REST API. As a result, you can make API calls to it using any HTTP library in your preferred language. The official client libraries are simply thin wrappers around the API that include a retry mechanism.
Closing
Typesense was built with several distinctive features primarily aimed at making the developer’s job easier while also giving customer as well as user the ability to provide a better search experience as possible.This article may have been entertaining as well as instructive in terms of how to install typesense from the ground up on a variety of platforms. Join Aviyel’s community to learn more about the open source project, get tips on how to contribute, and join active dev groups.
Call-to-Action
Aviyel is a collaborative platform that assists open source project communities in monetizing and long-term sustainability. To know more visit Aviyel.com and find great blogs and events, just like this one! Sign up now for early access, and don’t forget to follow us on our socials!