| 1 |
This example shows how to use custom engine implementation based on [Tarantool](https://www.tarantool.io/en/): i.e. it provides `Broker` and `PresenceManager`. |
| 2 |
|
| 3 |
Tarantool provides faster history and presence operations than Redis (up to 10x), while being on pair in subscribe and publish performance. |
| 4 |
|
| 5 |
**Important limitation to know**: Tarantool Broker uses channels that start with `__` (two underscores) for internal needs and does not allow subscribing on them from the outside. |
| 6 |
|
| 7 |
Tarantool Broker PUB/SUB can be customized to work over PUSH or POLL. |
| 8 |
|
| 9 |
**Since this is just an example – we do not guarantee any stability here**. This implementation have not been tested in production environment. |
| 10 |
|
| 11 |
## Single Tarantool instance example |
| 12 |
|
| 13 |
Go to `tntserver` dir and install `indexpiration` rock dependency: |
| 14 |
|
| 15 |
``` |
| 16 |
tarantoolctl rocks install https://raw.githubusercontent.com/moonlibs/indexpiration/master/rockspecs/indexpiration-scm-1.rockspec |
| 17 |
``` |
| 18 |
|
| 19 |
Create a directory for Tarantool `snap` and `xlog` files: |
| 20 |
|
| 21 |
``` |
| 22 |
mkdir tnt1 |
| 23 |
``` |
| 24 |
|
| 25 |
Start Tarantool: |
| 26 |
|
| 27 |
``` |
| 28 |
tarantool init.lua 1 |
| 29 |
``` |
| 30 |
|
| 31 |
In another terminal start chat application running the following command from example directory: |
| 32 |
|
| 33 |
``` |
| 34 |
go run main.go |
| 35 |
``` |
| 36 |
|
| 37 |
Go to http://localhost:8000. You will see simple chat app, try writing a chat message in one browser tab – you should see it appears in another tab. |
| 38 |
|
| 39 |
## Client-side sharding |
| 40 |
|
| 41 |
Go to `tntserver` dir and create directories for Tarantool files: |
| 42 |
|
| 43 |
``` |
| 44 |
mkdir tnt1 |
| 45 |
mkdir tnt2 |
| 46 |
``` |
| 47 |
|
| 48 |
Start first Tarantool instance: |
| 49 |
|
| 50 |
``` |
| 51 |
tarantool init.lua 1 |
| 52 |
``` |
| 53 |
|
| 54 |
Then second one: |
| 55 |
|
| 56 |
``` |
| 57 |
tarantool init.lua 2 |
| 58 |
``` |
| 59 |
|
| 60 |
Now run first application instance on port 8000: |
| 61 |
|
| 62 |
``` |
| 63 |
go run main.go --port 8000 --sharded |
| 64 |
``` |
| 65 |
|
| 66 |
The second one on port 8001: |
| 67 |
|
| 68 |
``` |
| 69 |
go run main.go --port 8001 --sharded |
| 70 |
``` |
| 71 |
|
| 72 |
Go to http://localhost:8000 or http://localhost:8001 – nodes will be connected over Tarantool and data is consistently sharded between 2 different Tarantool instances (by a channel). |
| 73 |
|
| 74 |
## Tarantool high availability example |
| 75 |
|
| 76 |
Run Tarantool with leader-follower setup with Cartridge (using `127.0.0.1:3301` and `127.0.0.1:3302`). Then start application example: |
| 77 |
|
| 78 |
``` |
| 79 |
go run main.go --port 8000 --ha |
| 80 |
``` |
| 81 |
|
| 82 |
## Tarantool Raft high availability example |
| 83 |
|
| 84 |
**Requires Tarantool 2.6.1+ since example uses Raft-based replication**. |
| 85 |
|
| 86 |
Create directories for Tarantool files inside `tntserver` folder: |
| 87 |
|
| 88 |
``` |
| 89 |
mkdir ha_tnt1 ha_tnt2 ha_tnt3 |
| 90 |
``` |
| 91 |
|
| 92 |
Run Tarantool cluster: |
| 93 |
|
| 94 |
``` |
| 95 |
docker-compose up |
| 96 |
``` |
| 97 |
|
| 98 |
Then start application: |
| 99 |
|
| 100 |
``` |
| 101 |
go run main.go --port 8000 --ha --raft |
| 102 |
``` |
| 103 |
|
| 104 |
At this moment you can temporary stop/run one of Tarantool instances using: |
| 105 |
|
| 106 |
``` |
| 107 |
docker-compose pause tnt1 |
| 108 |
docker-compose unpause tnt1 |
| 109 |
``` |
| 110 |
|
| 111 |
See that chat application continues to work after a short downtime. |