1DB_REDIS Module
2
3Andreas Granig
4
5 <agranig@sipwise.com>
6
7Edited by
8
9Andreas Granig
10
11 <agranig@sipwise.com>
12
13Alex Balashov
14
15 <abalashov@evaristesys.com>
16
17 Copyright © 2018 sipwise.com
18 __________________________________________________________________
19
20 Table of Contents
21
22 1. Admin Guide
23
24 1. Overview
25 2. Limitations
26 3. Dependencies
27
28 3.1. Kamailio Modules
29 3.2. External Libraries or Applications
30
31 4. Parameters
32
33 4.1. schema_path (string)
34 4.2. keys (string)
35 4.3. verbosity (int)
36
37 5. Usage
38 6. Module Specific Considerations
39
40 6.1. usrloc
41
42 List of Examples
43
44 1.1. Setting schema_path module parameter
45 1.2. Setting keys module parameter
46 1.3. Setting verbosity module parameter
47 1.4. Usage
48 1.5. Usage
49
50Chapter 1. Admin Guide
51
52 Table of Contents
53
54 1. Overview
55 2. Limitations
56 3. Dependencies
57
58 3.1. Kamailio Modules
59 3.2. External Libraries or Applications
60
61 4. Parameters
62
63 4.1. schema_path (string)
64 4.2. keys (string)
65 4.3. verbosity (int)
66
67 5. Usage
68 6. Module Specific Considerations
69
70 6.1. usrloc
71
721. Overview
73
74 This module provides a DB APIv1 connector for the Redis server
75 (https://www.redis.io).
76
77 It can be used as a replacement for other database modules such as
78 db_mysql and db_postgres. Not all the specs of DB APIv1 are
79 implemented, thus the usage of this module might be restricted to
80 specific modules. Also, for proper performance, this module needs
81 particular configuration tailored to the modules that make use of it.
82
83 Since Redis does not provide a schema by itself, db_redis ships with
84 schema files. The path to these has to be defined using the module
85 parameter "schema_path". The schema definition is defined in one file
86 per table, such that the file name corresponds to the table name, and
87 each file is composed of a comma-separated list of column definitions
88 in the format <column-name>/<type>[,<column-name>/<type> ...] in one
89 line, followed by a line holding the table version.
90
91 Example definition for the "location" table (from the usrloc module):
92username/string,domain/string,contact/string,received/string,path/string,expires
93/timestamp,q/double,callid/string,cseq/int,last_modified/timestamp,flags/int,cfl
94ags/int,user_agent/string,socket/string,methods/int,ruid/string,reg_id/int,insta
95nce/string,server_id/int,connection_id/int,keepalive/int,partition/int
968
97
98 Because Redis is a key-value store, it requires unique keys. This means
99 that tables and rows from a relational SQL database, e.g. from MySQL,
100 can not be ported one a 1:1 basis to Redis.
101
102 For instance, usrloc relies on a key of "username@domain", but in order
103 to store multiple contacts per AoR, it cannot be constrained to
104 uniqueness. To work around this, db_redis supports mapping sets in such
105 a way as to, in the case of the usrloc module, have a set with a key of
106 "username@domain" and its entries being unique keys per contact based
107 on the ruid of a contact. Thus, one contact in usrloc consists of a
108 unique key "location:entry::example-ruid-1" being a hash with the
109 columns like username, domain, contact, path etc. In addition, this
110 unique key is stored in a set
111 "location:usrdom::exampleuser:exampledomain.org". When usrloc does a
112 lookup based on "username@domain", db_redis figures out via the
113 keys/values the query constructed by usrloc to look for the final entry
114 key in the mapping set first. It then query the actual entries from
115 there, avoiding full table scans. For usrloc, the same holds true for
116 expired contacts, requiring a different kind of mapping. There is a
117 certain balance of read performance vs. write performance to consider,
118 because inserts and deletes also have to maintain the mappings, though
119 this yields much faster selects. The mappings can be freely defined, so
120 even though other kamailio modules don't require a specific mapping to
121 be in place for proper performance, mappings could be defined for
122 external applications to read faster (for instance letting the acc
123 module also write mappings besides the actual records for billing
124 systems to correlate start and stop records faster).
125
126 The key is always prefixed with 'tablename:entry::'. For example the
127 record in 'subscriber' table for user 'alice@sip.com' has the key:
128 'subscriber:entry::alice:sip.com'. If all the records are just loaded
129 at startup (or all reloaded at runtime), the key can just be made
130 unique using whatever values added after 'tablename:entry::' prefix.
131 For example, keys for 'address' table records can be:
132 'address:entry::1', address:entry::2', address:entry::3', ... No 'keys'
133 modparam of 'db_redis' for 'address' table needs to be defined.
134
135 Important Note: at this moment the module requires at least one 'keys'
136 parameter, but it does not need to be related to the table loaded from
137 Redis server -- for example, if used only for permissions module with
138 'address' table, then the 'keys' parameter can be specified for
139 'version' table. However, if it used for a module that inserts or
140 updates the records in database table, the key for entry must be
141 defined for that table.
142
143 The mappings can be freely defined in the "keys" module parameter,
144 which is composed of a semi-colon separated list of definitions in the
145 format
146 <table-name>=<entry>:<column-name>[&<map-name>:<column-name>,<column-na
147 me>...]. Each table must at least have an "entry" key for db_redis to
148 be able to store data.
149
150 Example:
151location=entry:ruid&usrdom:username,domain&timer:partition,keepalive;acc=entry:c
152allid,time_hires&cid:callid
153
154 For readability purposes, definitions of keys per table can span
155 multiple Kamailio config lines by providing multiple "keys" modparams.
156
1572. Limitations
158
159 * This module has implemented equivalent underlying Redis operations
160 for INSERT, UPDATE, DELETE and SELECT. The ORDER BY clause for
161 SELECT is not implemented. Raw querying is not implemented inside
162 this module; for sending literal commands to the Redis server, use
163 ndb_redis.
164
1653. Dependencies
166
167 3.1. Kamailio Modules
168 3.2. External Libraries or Applications
169
1703.1. Kamailio Modules
171
172 The following modules must be loaded before this module:
173 * none.
174
1753.2. External Libraries or Applications
176
177 The following libraries or applications must be installed before
178 running Kamailio with this module loaded:
179 * hiredis - available at https://github.com/redis/hiredis
180
1814. Parameters
182
183 4.1. schema_path (string)
184 4.2. keys (string)
185 4.3. verbosity (int)
186
1874.1. schema_path (string)
188
189 The path to the table schemas.
190
191 Default value: "/usr/share/kamailio/db_redis".
192
193 Example 1.1. Setting schema_path module parameter
194...
195modparam("db_redis", "schema_path", "/usr/local/share/kamailio/db_redis/kamailio
196")
197...
198
1994.2. keys (string)
200
201 The entry and mapping keys of tables.
202
203 Default value: "" (empty).
204
205 Example 1.2. Setting keys module parameter
206...
207modparam("db_redis", "keys", "version=entry:table_name;location=entry:ruid&usrdo
208m:username,domain&timer:partition,keepalive")
209...
210
2114.3. verbosity (int)
212
213 Control the verbosity of debug messages printed by the module. If set
214 to 1, the module prints schema details for all tables on each connect
215 operation to Redis server.
216
217 Default value: 1.
218
219 Example 1.3. Setting verbosity module parameter
220...
221modparam("db_redis", "verbosity", 0)
222...
223
2245. Usage
225
226 Load the module and set the "db_url" modparam for specific modules to:
227 'redis://[username]@host:port/database'. Username is optional. The
228 database portion must be a valid Redis database number.
229
230 Example 1.4. Usage
231...
232loadmodule "db_redis.so"
233...
234#!define DBURL_USRLOC "redis://127.0.0.1:6379/5"
235#!define DBURL_ACC "redis://127.0.0.1:6379/6"
236#!define DBURL_AUTH "redis://127.0.0.1:6379/7"
237#!define DBURL_PERM "redis://127.0.0.1:6379/8"
238#!define DBURL_DLG "redis://127.0.0.1:6379/9"
239...
240modparam("db_redis", "schema_path", "/usr/share/kamailio/db_redis/kamailio")
241modparam("db_redis", "keys", "version=entry:table_name")
242modparam("db_redis", "keys", "location=entry:ruid&usrdom:username,domain&timer:p
243artition,keepalive")
244modparam("db_redis", "keys", "acc=entry:callid,time_hires&cid:callid")
245modparam("db_redis", "keys", "subscriber=entry:username,domain")
246modparam("db_redis", "keys", "dialog=entry:hash_entry,hash_id&cid:callid")
247modparam("db_redis", "keys", "dialog_vars=entry:hash_entry,hash_id,dialog_key&di
248alog:hash_entry,hash_id")
249...
250modparam("usrloc", "db_url", DBURL_USRLOC)
251...
252modparam("acc_db", "db_url", DBURL_ACC)
253...
254modparam("auth_db", "db_url", DBURL_AUTH)
255...
256modparam("permissions", "db_url", DBURL_PERM)
257...
258modparam("dialog", "db_url", DBURL_DLG)
259...
260
261 Samples adding records for address table using 'redis-cli':
262
263 Example 1.5. Usage
264...
265SELECT 8
266HMSET address:entry::1 id 1 grp 1 ip_addr "127.0.0.1" mask 32 port 0
267HMSET address:entry::2 id 2 grp 1 ip_addr "127.0.0.2" mask 32 port 0
268HMSET address:entry::3 id 3 grp 2 ip_addr "127.0.0.3" mask 32 port 0
269HMSET address:entry::4 id 4 grp 2 ip_addr "127.0.0.4" mask 32 port 0 tag "test"
270...
271
272 Note that is some cases, the optional values in database tables can be
273 omitted. For 'address' table, the 'tag' value may be omitted. To avoid
274 any issues, set unused fields to their default values as defined by
275 database schema. When definition allows 'NULL', that field can be
276 unset.
277
2786. Module Specific Considerations
279
280 6.1. usrloc
281
2826.1. usrloc
283
284 If you set "expires_type" to "1" in order to use BIGINT instead of
285 DATETIME, make sure to update your location schema file and change the
286 type of "expires" and "last_modified" from "time" to "int".
287