Sunday, January 20, 2013

OpenSIPS and Cassandra Integrated

Finally back to the blog after a long busy schedule, well its still the same scheule but I've managed some time to take a look at setting up OpenSIPS with Cassandra as it's cache store. In one of my blog post I showed how to setup opensips and redis (which is also now updated for new Hiredis installation github path).

The advanatges can be numerious while we are using Cassandra and Redis individually or together from one opensips which are outlined in a very detailed article by Vlad Paiu - One of the developers and contributors of OpenSIPS project.
Possible use cases from the opensips documentation page are; Password caching for DB authentication
or Simple but distributed billing. With recent new developments going on specially on cacheDB are its now possible to have a distirubuted OpenSIPS cluster with a shared CacheDB backbone and all iternal data is available across all the opensips node for example dialog profiles of callers, loadbalanced resources statuses.
Over all these CacheDB modules enable multiple OpenSIPS instances to share internal data like number of calls, registration, call counters, billing data, IP balcklists etc. Refer to this presentation at Cluecon-2012.
So lets get started to integrate OpenSIPS 1.8 with Cassandra. Installation process for OpenSIPS 1.8 has been changed a little bit. Install all the libraries and packages as mentioned in this post.


Once after the repositiores are installed we need to install thrift-0.6.1 which is the Cassandra's interfcing client used by OpenSIPS.
root@OpenSIPS-CAS:~# wget
root@OpenSIPS-CAS:~# tar zxvf thrift-0.6.1.tar.gz
root@OpenSIPS-CAS:~# cd thrift-0.6.1/
root@OpenSIPS-CAS:~# ./configure
root@OpenSIPS-CAS:~# make
root@OpenSIPS-CAS:~# make install

UPDATE[28-July-2013]: As commented by a respected viewer; we need to install C++ boost libraries for OpenSIPS 1.9 on Debian 6. See comment by RQ in the comments section for further details.

I tried installing the newest version of thrift but OpenSIPS won't compile and give errors. So next, download and extract the 1.8 version of OpenSIPS and execute the 'make menuconfig' command.
root@OpenSIPS-CAS:~#tar zxvf opensips-1.8.2-svn9601_src.tar.gz
root@OpenSIPS-CAS:~#cd opensips-1.8.2-tls/
root@OpenSIPS-CAS:~#make menuconfig
A menu will open up like this.
Press Right arrow key on the first option and goto the following sub menu.
Now pressing right arrow key will take us to the module selection menu where we have to select which modules we need to install. Make sure to select the Cassandra module.
Once the modules' selection is made press left arrow key to go back to the figure 2. Select "Save Changes" and press enter. A list of suggested libraries and packages will show up at the bottom of the screen for help.
Do change installation paths and other flags according to your requirements.
Press left arrow key again to go back to main menu as in figure 1. Select "Compile And Install OpenSIPS" and press Enter. If everything goes well our opensips will be installed successfully. Follow the steps as mentioned in this post to setup the opensips LSB script (/etc/init.d/opensips); default opensips file; and creating the opensips DB. 
The OpenSIPS installation process is complete till here, now take a look at the cassandra installation.
root@CAS-N1:~# vim /etc/apt/sources.list
Add the follwoing lines at the bottom of the apt/sources.list file.
deb 10x main 

deb-src 10x main
Save and exit the file. and run apt-get update.
root@CAS-N1:~# apt-get update
If any keys error is encountered at the update then we need to add the keys using following command. (Make sure to change the '4BD736A82B5C1B00' string with the one you're seeing on your screen)
root@CAS-N1:~#gpg --keyserver --recv-keys 4BD736A82B5C1B00
root@CAS-N1:~#gpg --export --armor 4BD736A82B5C1B00 | sudo apt-key add -
Once the key is successfully added execute the apt-get update once again and after that install cassandra.
root@CAS-N1:~#apt-get install cassandra
I highly recommend to go through the following links to get quick know how about cassandra working and its files.
Planning a Cassandra Cluster Deployment

Cassandra’s data model cheat sheet

Node and Cluster Configuration (cassandra.yaml)

Configuring and Starting a Cassandra Cluster
Edit the /etc/init.d/cassandra.yaml file and change the cluster name and any other property as needed. Start cassandra and verify that it is running in processes.

I edited the following:



rpc_port: 9160
root@CAS-N1:~#vim /etc/cassandra/cassandra.yaml
root@CAS-N1:~#/etc/init.d/cassandra start
root@CAS-N1:~#ps -ef | grep cass
If cassandra has started successfully then connect to cassandra-cli and creake a keyspace for opensips to use.
root@CAS-N1:~# cassandra-cli --host --port 9160
Connected to: "Test Cluster" on
Welcome to Cassandra CLI version 1.0.12

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.
[default@unknown]CREATE KEYSPACE opensips WITH placement_strategy = 'SimpleStrategy' AND strategy_options = {replication_factor:2}; 
Waiting for schema agreement...
... schemas agree across the cluster

Now, everything is ready. Time to open up the opensips configurations file and load the module for cassandra.
root@OpenSIPS-CAS:~# vim /etc/opensips/opensips.cfg
loadmodule ""
modparam("cachedb_cassandra", "cachedb_url","cassandra:group1://")
modparam("cachedb_cassandra", "connection_timeout",1000)
modparam("cachedb_cassandra", "send_timeout",1000)
modparam("cachedb_cassandra", "receive_timeout",1000)
modparam("cachedb_cassandra", "wr_consistency_level",1)
modparam("cachedb_cassandra", "rd_consistency_level",1)
Go throught the OpenSIPS Cassandra module documentation; we can start using Cassandra as our KVP backend by using the following commands in our opensips.cfg main route;
cache_store("cassandra:group1","key","$ru value")
That is all , now its upto users to take advantage from this according to their needs.
Thanks for reading.

Reference URLS:


  1. Hello,

    This is a nice tutorial, and I see you have quite a few other on this blog that are good as well.

    Would you mind if we linked your OpenSIPS tutorials from the website, in the Tutorials section ?

    Best Regards,

    1. This is definitely an honour for me. Yes sure afterall thats why I write these things.

      Btw Vlad your recent MongoDB and Couchbase modules for cacheDB backend are great. Excellent work :)

  2. Hello Gohar, You need to install C++ boost libraries before installing thrift, IF you want to install opensips-1.9 with cassandra.
    cd thrift-*
    ./configure --enable-gen-cpp --enable-gen-c_glib --with-c_glib --with-cpp
    make all
    make install

    and then opensips.

    1. Thats great Hint RQ: I should put an update over eher for user using newer 1.9 OpenSIPS as this tutorial is for ver. 1.8 and I used Ubuntu.

      Please share your OS name,version and anything special to make this integration work on your setup.

      Thanks alot.

    2. I used debian 6 and opensips 1.9.1 (latest stable as of now).

  3. Dear Gohar,
    I have to make a record of current active calls in opensips in am using $DLG_COUNT variable to get the number of active calls. but when opensips get calls with higher cps rate or in other words for simultaneous calls it shows wrong call count e.g if I send 10 calls simultaneously then it shows only one active call. Please guide me what to do to handle the situation.

    Thanks in advance.