|
| 1 | +================================================= |
| 2 | +Expire Least Recently Used Data from a Collection |
| 3 | +================================================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. versionadded:: 2.2 |
| 8 | + |
| 9 | +Least recently used, or LRU, data retention patterns are used in simple |
| 10 | +data caches or support for stateless systems. By extending TTL |
| 11 | +collections with application support provides additional features. |
| 12 | + |
| 13 | +This pattern presents some cases and proides a pattern for maintaining |
| 14 | +documents in MongoDB where LRU documents are retained while others are |
| 15 | +removed using the TTL collection feature. |
| 16 | + |
| 17 | +Use Case |
| 18 | +-------- |
| 19 | + |
| 20 | +- Online shopping cart |
| 21 | + |
| 22 | + A database can be used to track shoppers' carts and items while |
| 23 | + they're on the site. If there are any abandoned carts, the database |
| 24 | + will clean them up and can make items available to shoppers again. |
| 25 | + |
| 26 | +- Authentication sessions |
| 27 | + |
| 28 | + An mobile app stores session information in a database, and |
| 29 | + validates current users' session when servicing requests. |
| 30 | + |
| 31 | +- Rendered page cache |
| 32 | + |
| 33 | + Assemble pages from database content and store rendered pages in a |
| 34 | + collection. The page should be stored as long as the page is being |
| 35 | + requested. |
| 36 | + |
| 37 | +- Draft comment |
| 38 | + |
| 39 | + Draft web comments are saved on the server temporarily until the |
| 40 | + visitor submits it. If the visitor does not submit, then the comment |
| 41 | + will be cleared from the server. |
| 42 | + |
| 43 | +- Transcoded media cache |
| 44 | + |
| 45 | + Archival images and videos are stored in a high quality format and |
| 46 | + transcoded to device specific formats for distribution. Keeping |
| 47 | + lists of frequently accessed media in cache and transcoding archive |
| 48 | + materials on demand can provide excellent application response while |
| 49 | + maximizing resources. |
| 50 | + |
| 51 | +Solution |
| 52 | +-------- |
| 53 | + |
| 54 | +Use a MongoDB TTL collection with your application to create a Least |
| 55 | +Recently Used collection. Your application updates current items while |
| 56 | +MongoDB automatically remove expired items. |
| 57 | + |
| 58 | +Requirements: |
| 59 | + |
| 60 | +- MongoDB version 2.2 or greater. |
| 61 | +- Application to connect to MongoDB. |
| 62 | + |
| 63 | +Process: |
| 64 | + |
| 65 | +#. Collection setup. |
| 66 | +#. Set up a MongodB TTL collection. |
| 67 | +#. Set up a application to insert, access, and update MongoDB |
| 68 | + collection. |
| 69 | + |
| 70 | +Pattern |
| 71 | +------- |
| 72 | + |
| 73 | +For an online shopping cart, MongoDB will track the shoppers' items in |
| 74 | +the ``carts`` collection for a limited time. |
| 75 | + |
| 76 | +The store application needs to update the collection when: |
| 77 | + |
| 78 | +- the shopper accesses the site, |
| 79 | +- adds or removes items, or |
| 80 | +- checks out. |
| 81 | + |
| 82 | +If the shopper leaves the site, their cart will be available for a |
| 83 | +limited time, eventually, the cart will be removed. |
| 84 | + |
| 85 | +Collection setup |
| 86 | +~~~~~~~~~~~~~~~~ |
| 87 | + |
| 88 | +The ``carts`` collection may be the following: |
| 89 | + |
| 90 | +.. code-block:: javascript |
| 91 | + |
| 92 | + { "_id" : ObjectId("..."), |
| 93 | + "cartID" : 100, |
| 94 | + "lastChange" : ISODate("2012-08-02T17:47:15.275Z"), |
| 95 | + "products": [ soap, sugar, milk ] } |
| 96 | + |
| 97 | + { "_id" : ObjectId("..."), |
| 98 | + "cartID" : 101, |
| 99 | + "lastChange" : ISODate("2012-08-02T17:47:15.275Z"), |
| 100 | + "products": [ milk, bread, eggs ] } |
| 101 | + |
| 102 | + { "_id" : ObjectId("..."), |
| 103 | + "cartID" : 102, |
| 104 | + "lastChange" : ISODate("2012-08-02T17:47:15.275Z"), |
| 105 | + "products": [ sugar, flour, eggs ] } |
| 106 | + |
| 107 | +The ``carts`` collection can be created by using the following commands: |
| 108 | + |
| 109 | +.. code-block:: javascript |
| 110 | + |
| 111 | + db.carts.insert( { cartID: 100, lastChange: new ISODate(), products: [ "soap", "sugar", "milk" ] }) |
| 112 | + db.carts.insert( { cartID: 101, lastChange: new ISODate(), products: [ "milk", "bread", "eggs" ] } ) |
| 113 | + db.carts.insert( { cartID: 102, lastChange: new ISODate(), products: [ "sugar", "flour", "eggs" ] } ) |
| 114 | + |
| 115 | +Set up a TTL collection |
| 116 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 117 | + |
| 118 | +The following command will set :program:`mongod` to monitor the |
| 119 | +``lastChange`` field and delete any documents that are older than |
| 120 | +86,400 seconds or 24 hours: |
| 121 | + |
| 122 | +.. code-block:: javascript |
| 123 | + |
| 124 | + db.carts.ensureIndex( { lastChange: 1 }, { expireAfterSeconds: 86400 } ) |
| 125 | + |
| 126 | +Application response |
| 127 | +~~~~~~~~~~~~~~~~~~~~ |
| 128 | + |
| 129 | +To keep any documents in the collection, update the ``lastChange`` |
| 130 | +field with a newer date. The following command will update the time of |
| 131 | +``lastChange`` of cart ``101`` and add an item to the ``products`` list. |
| 132 | + |
| 133 | +.. code-block:: javascript |
| 134 | + |
| 135 | + db.carts.update( { cartID: 101 }, { $set: { lastChange: new ISODate(), products: [ "milk", "bread", "eggs", "banana" ] } } ) |
| 136 | + |
| 137 | +Overall result |
| 138 | +~~~~~~~~~~~~~~ |
| 139 | + |
| 140 | +Any documents in a TTL collection that is updated properly will remain |
| 141 | +in the collection, while others will be deleted by |
| 142 | +:program:`mongod`. Your application will only have to perform simple |
| 143 | +updates to individual documents. |
| 144 | + |
| 145 | +With basic application updates, any TTL collection can easily become a |
| 146 | +Least Recently Used collection. |
0 commit comments