Manage multi-tenancy ArangoDB connection

假装没事ソ 提交于 2019-12-11 04:06:22

问题


I use ArangoDB/Go (using go-driver) and need to implement multi-tenancy, means every customer is going to have his data in a separate DB.

What I'm trying to figure out is how to make this multi-tenancy work. I understand that it's not sustainable to create a new DB connection for each request, means I have to maintain a pool of connections (not a typical connection pool tho). Of course, I can't just assume that I can make limitless, there has to be a limit. However, the more I think about that the more I understand that I need some advice on it. I'm new to Go, coming from the PHP world, and obviously it's a completely different paradigm in PHP.

Some details I have an API (written in Go) which talks to ArangoDb using arangodb/go-driver. A standard way of creating a DB connection is

  • create a connection conn, err := graphHTTP.NewConnection(...)

  • create client c, err := graphDriver.NewClient(...)

  • create DB connection graphDB, err := p.cl.Database(...)

This works if one has only one DB, and DB connection is created on API's boot up. In my case it's many, and, as previously suggested, I need to maintain a DB connections pool.

Where it gets fuzzy for me is how to maintain this pool, keep in mind that pool has to have a limit. Say, my pool is of size 5, abd over time it's been filled up with the connections. A new request comes in, and it needs a connection to a DB which is not in the pool. The way I see it, I have only 2 options:

  1. Kill one of the pooled connections, if it's not used
  2. Wait till #1 can be done, or throw an error if waiting time is too long.

The biggest unknow, and this is mainly because I've never done anything like this, for me is how to track whether connection is being used or not. What makes thing even more complex is that DB connection has it's own pool, it's done on the transport level.

Any recommendations on how to approach this task?


回答1:


I implemented this in a Java proof of concept SaaS application a few months ago.

My approach can be described in a high level as:

  1. Create a Concurrent Queue to hold the Java Driver instances (Java driver has connection pooling build in)
  2. Use subdomain to determine which SaaS client is being used (can use URL param but I don't like that approach)
  3. Reference the correct Connection from Queue based on SaaS client or create a new one if not in the Queue.
  4. Continue with request.

This was fairly trivial by naming each DB to match the subdomain, but a lookup from the _systemdb could also be used.

*Edit The Concurrent Queue holds at most one Driver object per database and hence the size at most will match the number of databases. In my testing I did not manage the size of this Queue at all.

A good server should be able to hold hundreds of these or even thousands depending on memory, and a load balancing strategy can be used to split clients into different server clusters if scaling large enough. A worker thread could also be used to remove objects based on age but that might interfere with throughput.



来源:https://stackoverflow.com/questions/48174019/manage-multi-tenancy-arangodb-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!