# Graph traversal using UDF

**URL:** <https://discuss.aerospike.com/t/graph-traversal-using-udf/2127>\
**Category:** User Defined Functions (UDF)\
**Tags:** lua, feature\
**Created:** [November 15, 2015, 4:28am UTC](https://discuss.aerospike.com/t/graph-traversal-using-udf/2127 "2015-11-15T04:28:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yesteapea](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/yesteapea/32/296_2.png) [@yesteapea](https://discuss.aerospike.com/u/yesteapea)\
**Post date:** [November 15, 2015, 4:28am UTC](https://discuss.aerospike.com/t/graph-traversal-using-udf/2127/1 "2015-11-15T04:28:26Z")

</div>

I have edges of graph stored (to be more specific, the graph is bunch of trees aka forest) in aerospike. I want to do some basic traversal and find the root. This i can do in client level, by getting a parent until i reach a node which does not have a parent. But this incurs some unnecessary network latency.

Is it possible to somehow do this using a udf? Else I think this would be an awesome feature to have. Are there any alternative ways to do this?

---

<div class="post-metadata">

**Author:** ![sunil](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@sunil](https://discuss.aerospike.com/u/sunil)\
**Post date:** [November 20, 2015, 4:01pm UTC](https://discuss.aerospike.com/t/graph-traversal-using-udf/2127/2 "2015-11-20T16:01:25Z")

</div>

Sorry, this is not possible. The UDF can operate on a single record in an invocation. Once its is invoked for a record, it cannot access other records. The basic reason why this is not allowed is that the record may be on a different node. We don’t want the UDF to do distributed transaction.

---

<div class="post-metadata">

**Author:** ![bbulkow](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bbulkow](https://discuss.aerospike.com/u/bbulkow)\
**Post date:** [February 4, 2016, 4:51am UTC](https://discuss.aerospike.com/t/graph-traversal-using-udf/2127/3 "2016-02-04T04:51:04Z")

</div>

Don’t bother using a UDF to do this: simply do it with client side code. Between doing batch reads, and list operations in database ( for pushing edges onto lists efficientlys ), and doing the edge traversal in your app server, you’ll end up with a very efficient system.

If everything you can do fits into a single server’s ram, simply use a local data structure (edges, vertexes). Don’t bother with a database.

If you need multiple servers’ worth of storage, portions of the graph will be on different servers anyway. So making a network request is fine - and networks are faster than you think, if you use batches.

Do make sure you use batch reads. This means you can get the next set of edges with a single network round trip. If you want to build highly scalable ( instead of single-server-limited ) graph traversal, this is as good as it gets.
