Should I use Django-Rest-Framework for performance reasons, despite not needing an externally-consumable API?

92 views
Skip to first unread message

Tyler Lynch

unread,
Oct 25, 2018, 9:44:20 PM10/25/18
to Django users
I have no need for an externally consumable API, but I am interested in using Django-Rest-Framework simply for performance reasons. 

I'm led to believe that by decoupling my front and back end and then simply consuming the DRF api within views, that I can setup a better caching system? Does this make sense? Using DRF from an architectural standpoint (with the goal of optimizing caching & performance) despite not needing an externally used API? Or am I totally off base and confused? Any advice would be much appreciated.

Andréas Kühne

unread,
Oct 26, 2018, 3:30:28 AM10/26/18
to django...@googlegroups.com
Hi,

I really don't get why you would want to do that? If you are doing only a "standard" website - you don't want or NEED the extra complexity of running DRF. It's not that DRF is hard to setup - but for example if you want to present a list of items - in "standard" django, you create the list and add it to the context data. In DRF - you need to create the list, serialize it into json (or xml if you want to go that route), on the frontend you then need to deserialize the list and present it.

You add a lot of complexity and need to write a lot of frontend code.

Working with "standard" django - you can cache a lot of things in different places. You can for example cache an entire response with template, or just cache the database calls and present them in a template. This is not hard to cache or to setup. 

I think I would need to know more about your use case to understand it better :-)

Regards,

Andréas


Den fre 26 okt. 2018 kl 03:44 skrev Tyler Lynch <tyler...@gmail.com>:
I have no need for an externally consumable API, but I am interested in using Django-Rest-Framework simply for performance reasons. 

I'm led to believe that by decoupling my front and back end and then simply consuming the DRF api within views, that I can setup a better caching system? Does this make sense? Using DRF from an architectural standpoint (with the goal of optimizing caching & performance) despite not needing an externally used API? Or am I totally off base and confused? Any advice would be much appreciated.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8b55ff9a-e915-4546-bf3c-1b20a25e4826%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

PASCUAL Eric

unread,
Oct 26, 2018, 4:28:23 AM10/26/18
to django...@googlegroups.com

Hi,


Using DRF can help when there is a need for decoupling the presentation layer from the logic one, for instance if the logic is planned to be used in other scenarios that the interactive Web app.


One can argue that structuring the logic as a Python package can do it, but this will not work if the deployment involves splitting front-end and logic back-end in distinct nodes (f.i. in a Docker multi-container based deployment).


Introducing DRF adds for sure a level of complexity and you'll loose some potential caching benefits, but it lets the path opened if ever the above mentioned evolution of the application appears in the future. You will not have to refactor anything then.


The bottom line is that there is no absolute answer to the question. It depends on what can be the plans for the application evolutions in the future.


Regards


Eric


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of Andréas Kühne <andrea...@hypercode.se>
Sent: Friday, October 26, 2018 9:29:40 AM
To: django...@googlegroups.com
Subject: Re: Should I use Django-Rest-Framework for performance reasons, despite not needing an externally-consumable API?
 

Tyler Lynch

unread,
Oct 26, 2018, 10:14:00 AM10/26/18
to Django users
Hi Andréas,

Thanks for getting back to me!

I guess adding DRF to my project right now would be a case of premature-optimization then. I had a bit of muddied thinking such that I thought DRF might help with caching the database queries, but I guess that is completely independent of DRF?

Is there no performance reason to adopt DRF?

I'm basically just running a crud app. One thing I was hoping DRF might've been able to help me with though is importing/exporting the data as csv's as well. I'm not sure though if it's more expedient to just use the csv reader. 

Thanks again for the response

Tyler Lynch

unread,
Oct 26, 2018, 10:18:49 AM10/26/18
to Django users
Hi Eric,

Thanks for the response

You mention that adding DRF can actually make you lose some potential benefits from caching? Might i ask how that is so? I actually thought it was supposed to be the opposite (shows you what I know tho hah)

I guess DRF can be added to the project at any time tho, so there's no need to build the project with DRF built-in from beginning. So if I do need it down the road, it won't be difficult to add it in. 
To post to this group, send email to djang...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to djang...@googlegroups.com.

Andréas Kühne

unread,
Oct 26, 2018, 12:29:10 PM10/26/18
to django...@googlegroups.com
Hi again,

I don't think there is any performance reason to add DRF. You should architect your application in a way that the database is the main speed issue (for example, avoid doing loops in python) - then you can add som caching to the queries that you know can be cached. That would give you the best performance regardless if you are running DRF or not :-)

And regardless the added complexity is probably not worth it - as long as you aren't using it for an external API.

DRF doesn't help importing from / exporting to CSV - it mainly understands json. I would use some form of CSV export lib - there are a lot of them on pypi. Or pandas of course!

Regards,

Andréas


PASCUAL Eric

unread,
Oct 26, 2018, 6:02:43 PM10/26/18
to Django users

Hi,


You mention that adding DRF can actually make you lose some potential benefits from caching?


Django does a lot of job WRT caching data at the ORM level and there is a "direct path" from the UI and the ORM. I've the feeling that putting a REST API in the middle have chances to defeat some of the involved strategies.


I guess DRF can be added to the project at any time


Yes of course, but the views of the UI layer will have to be reworked. When dealing directly with the ORM, they use query sets. When the REST API is added, you'll have to deal with serializers instead. The migration will represent some work.


So if you think that there are 80% of chances the application will evolve to a REST API based architecture in a not too distant future, my advice would be introduced it from the begining.


But once again, this is a matter of appreciation.


Best


Eric PASCUAL

Centre Scientifique et Technique du Bâtiment
290 route des Lucioles - BP 209
06904 SOPHIA ANTIPOLIS CEDEX
http://www.cstb.fr


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of Tyler Lynch <tyler...@gmail.com>
Sent: Friday, October 26, 2018 4:18:49 PM
To: Django users
To post to this group, send email to django...@googlegroups.com.

Tyler Lynch

unread,
Oct 27, 2018, 1:09:22 PM10/27/18
to Django users
Hi Andréas, thanks again for the response.

I've decided to take your advice and not going to be adding DRF because I simply don't need an API, so no need for the added complexity.

It's funny that you suggest using pandas for the csv import/exporting though. I kept seeing pandas mentioned everywhere! So I finally looked into it last night and Wow! Pandas is amazing and dataframes are actually quite a profound data type. 

I'm definitely going the pandas route for converting data types. So thanks for the heads up and last push to investigate it!

Tyler Lynch

unread,
Oct 27, 2018, 1:12:13 PM10/27/18
to Django users
Hey Eric, thanks for getting back to me again!

As I don't want to have an externally-consumable API for this application (would be more of a liability than asset for me right now), I am most likely going to not use DRF.

I know it'll be more time-consuming refactoring if I ever do decide to use DRF, but I'm okay deferring this decision for now.

Do you think there's any reason to use DRF despite not needing an externally-consumable API? Would using an internally-consumed API have different benefits? It seems the consensus is that adding DRF is more likely to limit performance than enhance, as you suggested earlier.

Regards,
Tyler

PASCUAL Eric

unread,
Oct 28, 2018, 5:41:09 AM10/28/18
to Django users

Hi,


Do you think there's any reason to use DRF despite not needing an externally-consumable API?

None that I can think about. But I don't pretend to be an expert on the topic ;)


IMHO it is just a matter of planing for the future. If you are certain that nothing else but your current app will need to play with the models, adding a REST API (be it written on top of DRF or not) is not required at all. To my knowledge it will not bring any benefit WRT performances. Chances are that it will be the opposite (but not in dramatic proportions however if the underlying stack is properly chosen and configured) since the REST API adds a layer of HTTP based exchanges.


Regards


Eric


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of Tyler Lynch <tyler...@gmail.com>
Sent: Saturday, October 27, 2018 7:12:13 PM
To post to this group, send email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages