|
| 1 | +# Built-in Proxy Quickstart |
| 2 | + |
| 3 | +This page helps you get familiar with the Memcached built-in proxy by guiding you through the following tasks: |
| 4 | + |
| 5 | +* Build Memcached with its proxy features enabled. |
| 6 | +* Download the Lua library that the proxy requires for common tasks. |
| 7 | +* Set up a minimal proxy configuration file that lets the proxy use two Memcached servers as its backend. |
| 8 | +* Run this proxy-based Memcached architecture, and observe the proxy at work by connecting to it and querying it. |
| 9 | + |
| 10 | +For detailed documentation about the Memcached built-in proxy, see [Proxy](/Proxy). |
| 11 | + |
| 12 | +## Before you begin |
| 13 | + |
| 14 | +Make sure that your machine has the necessary development tools and libraries |
| 15 | +to build Memcached from source. These requirements are probably available from your operating system's package manager. |
| 16 | + |
| 17 | +For example, on Debian, you can satisfy these requirements by having the following packages installed: |
| 18 | + |
| 19 | +* `gcc` |
| 20 | +* `make` |
| 21 | +* `libevent-dev` |
| 22 | + |
| 23 | +The steps on this page guide you though launching three Memcached servers on TCP ports `11211`, `11212`, and `11213`. If you already have Memcached services running on those ports and you don't want to disrupt them, then you should take either of these additional steps: |
| 24 | + |
| 25 | +* Substitute different port numbers for this tutorial. |
| 26 | +* Run this tutorial as written on a different machine that isn't running Memcached. |
| 27 | + |
| 28 | +## Download and build the proxy |
| 29 | + |
| 30 | +Because the Memcached built-in proxy is a non-default, compile-time configuration option, the proxy is available as a Memcached feature only if you build Memcached from source. To do this, follow these steps: |
| 31 | + |
| 32 | +1. Download the latest Memcached release tarball: |
| 33 | + |
| 34 | + ``` |
| 35 | + wget https://memcached.org/latest |
| 36 | + ``` |
| 37 | +
|
| 38 | +1. Unzip and untar the tarball, then change your working directory to the source directory: |
| 39 | +
|
| 40 | + ```posix-terminal |
| 41 | + tar xzvf memcached-VERSION.tar.gz |
| 42 | + |
| 43 | + cd memcached-VERSION |
| 44 | + ``` |
| 45 | + |
| 46 | + Replace <var>`VERSION`</var> with the version number in the tarball that you downloaded in the previous step. |
| 47 | + |
| 48 | +1. Configure the Memcached build process to enable the built-in proxy: |
| 49 | +
|
| 50 | + ``` |
| 51 | + ./configure --with-proxy |
| 52 | + ``` |
| 53 | + |
| 54 | +1. Build and test Memcached: |
| 55 | +
|
| 56 | + ``` |
| 57 | + make |
| 58 | + |
| 59 | + make test |
| 60 | + ``` |
| 61 | + |
| 62 | + If you encounter errors related to missing tools or libraries, make sure that your system has the development packages that you need to build Memcached from source. For more information, see "Before you begin", earlier on this page. |
| 63 | +
|
| 64 | + Note: For the sake of simplicity and easier cleanup, this quick-start guide doesn't include a `make install` step. If you want to install the proxy-enabled Memcached on this machine, then you can do as a final step at the end of this guide. |
| 65 | +
|
| 66 | +## Download the route library |
| 67 | +
|
| 68 | +Run the following command: |
| 69 | +
|
| 70 | +``` |
| 71 | +wget https://raw.githubusercontent.com/memcached/memcached-proxylibs/main/lib/routelib/routelib.lua |
| 72 | +``` |
| 73 | +
|
| 74 | +## Configure the proxy |
| 75 | + |
| 76 | +To create a minimal proxy configuration file named `config.lua`, run the following command: |
| 77 | +
|
| 78 | +``` |
| 79 | +echo 'pools{ |
| 80 | + quickstart_pool = { |
| 81 | + backends = { |
| 82 | + "127.0.0.1:11212", |
| 83 | + } |
| 84 | + }, |
| 85 | + fallback = { |
| 86 | + backends = { |
| 87 | + "127.0.0.1:11213", |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +routes{ |
| 93 | + map = { |
| 94 | + quickstart_route = route_direct{ |
| 95 | + child = "quickstart_pool", |
| 96 | + }, |
| 97 | + }, |
| 98 | + default = route_direct{ child = "fallback" } |
| 99 | +}' > config.lua |
| 100 | +``` |
| 101 | +
|
| 102 | +## Set up and run the proxied Memcached servers |
| 103 | +
|
| 104 | +1. Using the binary that you built in a previous step, launch two ordinary Memcached cache servers, running on TCP ports `11212` and `11213`: |
| 105 | +
|
| 106 | + ``` |
| 107 | + ./memcached -l localhost -p 11212 & |
| 108 | + |
| 109 | + ./memcached -l localhost -p 11213 & |
| 110 | + ``` |
| 111 | + |
| 112 | +1. Launch a third Memcached server in proxy mode, by specifying the proxy-specific configuration files that you downloaded and created, respectively, in previous steps: |
| 113 | +
|
| 114 | + ``` |
| 115 | + ./memcached -l localhost -p 11211 -o proxy_config=routelib.lua,proxy_arg=config.lua & |
| 116 | + ``` |
| 117 | +
|
| 118 | +## Connect to and use the proxy |
| 119 | +
|
| 120 | +1. Connect to the proxy and set a variable: |
| 121 | +
|
| 122 | + ``` |
| 123 | + telnet localhost 11211 |
| 124 | + set quickstart-data 0 0 6 |
| 125 | + Hello! |
| 126 | + ``` |
| 127 | + |
| 128 | + The Memcached proxy responds with `STORED`. |
| 129 | + |
| 130 | +1. Fetch the data again: |
| 131 | +
|
| 132 | + ``` |
| 133 | + get quickstart-data |
| 134 | + ``` |
| 135 | + |
| 136 | + The Memcached proxy responds with the following: |
| 137 | + |
| 138 | + ``` |
| 139 | + Hello! |
| 140 | + END |
| 141 | + ``` |
| 142 | +
|
| 143 | +1. Terminate the Telnet session: |
| 144 | +
|
| 145 | + 1. Press <kbd>Ctrl</kbd> + <kbd>]</kbd>. |
| 146 | + |
| 147 | + 1. At the `telnet>` prompt, enter `quit`. |
| 148 | +
|
| 149 | +At this point, you can optionally connect to the two Memcached backend servers by connecting to ports `11212` and `11213` and running the command `get quickstart-data` on both. One of the two servers returns the value `Hello!`. That server is the one that the proxy chose as the backend to store the data under the `quickstart-data` key. |
| 150 | +
|
| 151 | +## Clean up |
| 152 | +
|
| 153 | +When you are finished with this tutorial, you can stop the three Memcached processes that you launched in the previous steps. |
| 154 | +
|
| 155 | +Because this quickstart didn't have you install the Memcached software that you built, you can clean up by deleting the source directory that you downloaded. |
| 156 | +
|
| 157 | +If you want to keep the Memcached software that you built with the proxy features enabled, then you can install it through one of these methods: |
| 158 | +
|
| 159 | +* Install Memcached in the default location on your file system, such as `/usr/local/bin`, by running `sudo make install`. |
| 160 | +
|
| 161 | +* Manually copy or move the `memcached` binary to the location of your choice. |
| 162 | +
|
| 163 | +You also have the option of rebuilding the `memcached` binary with different configuration options. For more information, run the command `./configure --help` from within the Memcached source directory. |
| 164 | +
|
| 165 | +## Next steps |
| 166 | +
|
| 167 | +For more information about running the proxy, including all ways that you can define its behavior using a Lua-based configuration file, see [Proxy](/Proxy). |
0 commit comments